// 导入numpy包 起个别名为 np
import numpy as np
world_alcohol = np.genfromtxt("world_alcohol.txt", delimiter=",", dtype=str)print(type(world_alcohol))print(world_alcohol)# ndarrayprint(help(np.genfromtxt))<class'np.ndarray'>[['Year''WHO region''Country''Beverage Types''Display Value']['1986''Western Pacific''Viet Nam''Wine''0']['1986''Americas''Uruguay''Other''0.5']...['1987''Africa''Malawi''Other''0.75']['1989''Americas''Bahamas''Wine''1.5']['1985''Africa''Malawi''Spirits''0.31']]
Help on function genfromtxt in module numpy.lib.npyio:
genfromtxt(fname, dtype=<class'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None, encoding='bytes')
Load data from a text file,with missing values handled as specified.
Each line past the first `skip_header` lines is split at the `delimiter`
character,and characters following the `comments` character are discarded.
Parameters
----------
fname :file,str, pathlib.Path,list of str, generator
File, filename,list,or generator to read. If the filename
extension is `.gz` or `.bz2`, the fileis first decompressed. Note
that generators must return byte strings in Python 3k. The strings
in a listor produced by a generator are treated as lines.
dtype : dtype, optional
Data type of the resulting array.
If None, the dtypes will be determined by the contents of each
column, individually.
comments :str, optional
The character used to indicate the start of a comment.
All the characters occurring on a line after a comment are discarded
delimiter :str,int,or sequence, optional
The string used to separate values. By default,any consecutive
whitespaces act as delimiter. An integer or sequence of integers
can also be provided as width(s) of each field.
skiprows :int, optional
`skiprows` was removed in numpy 1.10. Please use `skip_header` instead.
skip_header :int, optional
The number of lines to skip at the beginning of the file.
skip_footer :int, optional
The number of lines to skip at the end of the file.
converters : variable, optional
The set of functions that convert the data of a column to a value.
The converters can also be used to provide a default value
for missing data: ``converters ={3:lambda s:float(s or0)}``.
missing : variable, optional
`missing` was removed in numpy 1.10. Please use `missing_values`
instead.
missing_values : variable, optional
The set of strings corresponding to missing data.
filling_values : variable, optional
The set of values to be used as default when the data are missing.
usecols : sequence, optional
Which columns to read,with0 being the first. For example,
``usecols =(1,4,5)`` will extract the 2nd, 5th and 6th columns.
names :{None,True,str, sequence}, optional
If `names` isTrue, the field names are read from the first line after
the first `skip_header` lines. This line can optionally be proceeded
by a comment delimeter. If `names` is a sequence or a single-string of
comma-separated names, the names will be used to define the field names
in a structured dtype. If `names` isNone, the names of the dtype
fields will be used,ifany.
excludelist : sequence, optional
A list of names to exclude. This listis appended to the default list['return','file','print']. Excluded names are appended an underscore:for example, `file` would become `file_`.
deletechars :str, optional
A string combining invalid characters that must be deleted from the
names.
defaultfmt :str, optional
A format used to define default field names, such as"f%i"or"f_%02i".
autostrip :bool, optional
Whether to automatically strip white spaces from the variables.
replace_space : char, optional
Character(s) used in replacement of white spaces in the variables
names. By default, use a '_'.
case_sensitive :{True,False,'upper','lower'}, optional
If True, field names are case sensitive.
If Falseor'upper', field names are converted to upper case.
If 'lower', field names are converted to lower case.
unpack :bool, optional
If True, the returned array is transposed, so that arguments may be
unpacked using ``x, y, z = loadtxt(...)``
usemask :bool, optional
If True,return a masked array.
If False,return a regular array.
loose :bool, optional
If True, do notraise errors for invalid values.
invalid_raise :bool, optional
If True, an exception is raised if an inconsistency is detected in the
number of columns.
If False, a warning is emitted and the offending lines are skipped.
max_rows :int, optional
The maximum number of rows to read. Must not be used with skip_footer
at the same time. If given, the value must be at least 1. Default is
to read the entire file... versionadded::1.10.0
encoding :str, optional
Encoding used to decode the inputfile. Does notapply when `fname` is
a fileobject. The special value 'bytes' enables backward compatibility
workarounds that ensure that you receive byte arrays when possible
and passes latin1 encoded strings to converters. Override this value to
receive unicode arrays andpass strings asinput to converters. If set
to None the system default is used. The default value is'bytes'... versionadded::1.14.0
Returns
-------
out : ndarray
Data read from the text file. If `usemask` isTrue, this is a
masked array.
See Also
--------
numpy.loadtxt : equivalent function when no data is missing.
Notes
-----* When spaces are used as delimiters,or when no delimiter has been given
asinput, there should not be any missing data between two fields.* When the variables are named (either by a flexible dtype orwith `names`,
there must not be any header in the file(else a ValueError
exception is raised).* Individual values are not stripped of spaces by default.
When using a custom converter, make sure the function does remove spaces.
References
----------..[1] NumPy User Guide, section `I/O with NumPy
<http://docs.scipy.org/doc/numpy/user/basics.io.genfromtxt.html>`_.
Examples
--------->>>from io import StringIO
>>>import numpy as np
Comma delimited filewith mixed dtype
>>> s = StringIO("1,1.3,abcde")>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),...('mystring','S5')], delimiter=",")>>> data
array((1,1.3,'abcde'),
dtype=[('myint','<i8'),('myfloat','<f8'),('mystring','|S5')])
Using dtype =None>>> s.seek(0)# needed for StringIO example only>>> data = np.genfromtxt(s, dtype=None,... names =['myint','myfloat','mystring'], delimiter=",")>>> data
array((1,1.3,'abcde'),
dtype=[('myint','<i8'),('myfloat','<f8'),('mystring','|S5')])
Specifying dtype and names
>>> s.seek(0)>>> data = np.genfromtxt(s, dtype="i8,f8,S5",... names=['myint','myfloat','mystring'], delimiter=",")>>> data
array((1,1.3,'abcde'),
dtype=[('myint','<i8'),('myfloat','<f8'),('mystring','|S5')])
An example with fixed-width columns
>>> s = StringIO("11.3abcde")>>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],... delimiter=[1,3,5])>>> data
array((1,1.3,'abcde'),
dtype=[('intvar','<i8'),('fltvar','<f8'),('strvar','|S5')])
# The np.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
vector = np.array([5,10,15,20])# list->ndarray# When we input a list of lists, we get a matrix as a result:
matrix = np.array([[5,10,15],[20,25,30],[35,40,45]])
triple_matrix = np.array([[[1,2,3],[4,5,6],[7,8,9],[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2],[0,1,2]]])print(vector)print(matrix)print(triple_matrix)[5101520][[51015][202530][354045]][[[123][456][789][012][012][012][012][012][012]]]
# We can use the ndarray.shape property to figure out how many elements are in the array
vector = np.array([1,2,3,4])# 一位数组-->行向量print(vector.shape)# 表达向量或者矩阵的结构 在神经网络的测试环节很有用处 方便理解 debug# For matrices, the shape property contains a tuple with 2 elements.
matrix = np.array([[5,10,15],[20,25,30]])# 二维数组->矩阵 2行3列print(matrix.shape)(4,)(2,3)
#Each value in a NumPy array has to have the same data type#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays. #You can check the data type of a NumPy array using the dtype property.
numbers = np.array([1,2,3,4])print(numbers)print(type(numbers))
numbers.dtype
[1234]<class'numpy.ndarray'>
dtype('int32')
# When NumPy can't convert a value to a numeric data type like float or integer, it uses a special nan value that stands for Not a Number# NAN is the missing data# 1.98600000e+03 is actually 1.986 * 10 ^ 3
world_alcohol
array([['Year','WHO region','Country','Beverage Types','Display Value'],['1986','Western Pacific','Viet Nam','Wine','0'],['1986','Americas','Uruguay','Other','0.5'],...,['1987','Africa','Malawi','Other','0.75'],['1989','Americas','Bahamas','Wine','1.5'],['1985','Africa','Malawi','Spirits','0.31']], dtype='<U52')
uruguay_other_1986 = world_alcohol[1,4]# index begins from zero
third_country = world_alcohol[2,2]print(uruguay_other_1986)print(third_country)0.5
Cte d'Ivoire
# it will compare the second value to each element in the vector# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = np.array([5,10,15,20])
vector ==10# 对其中每个元素都进行操作
array([False,True,False,False])
#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
vector = np.array([5,10,15,20])
equal_to_ten =(vector ==10)print(equal_to_ten)print(vector[equal_to_ten])# 把bool值当做一个index(索引)传到当前numpy.array之中# vector[index]index即为bool类型的数组 value为false不用返回 为true将会返回[FalseTrueFalseFalse][10]
# We can also perform comparisons with multiple conditions
vector = np.array([5,10,15,20])
equal_to_ten_and_five =(vector ==10)&(vector ==5)# And "&"的判断print(equal_to_ten_and_five)[FalseFalseFalseFalse]
# We can convert the data type of an array with the ndarray.astype() method.
vector = np.array(["1","2","3"])print(vector.dtype)print(vector)
vector = vector.astype(float)print(vector.dtype)print(vector)|S1
['1''2''3']
float64
[1.2.3.]
# The axis dictates which dimension we perform the operation on# 1 means that we want to perform the operation on each row, and 0 means on each column
matrix = np.array([[5,10,15],[20,25,30],[35,40,45]])
matrix.sum(axis=1)# 按行求和axix=1 或按列求和axis0 axix"维度"
array([30,75,120])