Disclaimer: This article is a large number of reference Https://www.dataquest.io/mission/6/getting-started-with-numpy (suggested reading the original)
Read file
There is a file named World_alcohol.csv, the file format is as follows
year,who region,country,beverage Types,display Value 1986,western Pacific,viet nam,wine,0 1986,americas,uruguay,other,0.5 1985,africa,cte d ' ivoire,wine,1.62 |
The meaning of the contents of the document: (This is a global beverage consumption record table, the first column means the year of record, the second column refers to the place of the beverage, the third column refers to the consumption of drinks, the fourth column refers to the type of beverage, the fifth column refers to the average consumption of beverages per person)
Now use NumPy's Genfromtxt () function to read the file, and the delimiter parameter is used to specify the delimiter for each row to separate the data
Import= numpy.genfromtxt ('world_alcohol.csv', delimiter=', ')print(world_alcohol)
This reads the following:
| [[] Nan nan nan nan Nan [1.98600000e+03 nan nan nan 0.00000000E+00] [1.98600000e+03 nan nan nan 5.00000000E-01] ..., [1.98600000e+03 nan nan nan 2.54000000E+00] [1.98700000e+03 nan nan nan 0.00000000E+00] [1.98600000e+03 nan nan nan 5.15000000E+00]] |
This is because numpy reads the element by default in the float format, and the data that cannot be converted to float is read as Nan (not a number), and for the data that is left blank, it is shown as NA (not available), in order to read the data correctly, You can add parameters by adding:
- The Dtype parameter is used to specify the format of the read data, where the U75 means that each data is read as a Unicode data format of 75 bytes
- The Skip_header parameter is used to skip the first line of the file
- The delimiter parameter is used to specify the delimiter for each row of data
Import= numpy.genfromtxt ('world_alcohol.csv', dtype='U75 ', skip_header=true, delimiter=',')print( World_alcohol)
Array
An array can be created by using the array () function, where the vector refers to a variable with only one list, and the matrix refers to a list with multiple lists
Create a vector: vector = Numpy.array ([10, 20, 30])
Create a matrix: Matrix = Numpy.array ([[5,10,15],[20,25,30],[35,40,45]])
Properties of the array:
- The Shape property describes the structure of the array
Print (Vector.shape) Output: (3,) this is a tuple that indicates that the vector variable is a vector of only one row, with 3 elements Print (Matrix.shape) Output: (3,3) indicates that the matrix variable is a 3x3 matrix with 3 rows and 3 columns with a total of 9 elements |
- The Dtype property describes the data type of the element
Print (Vector.shape) Output Result: Int64 |
The data types are roughly the following:
- BOOL--Boolean type, True or False
- INT--integral type, divided into Int16, Int32, Int64, followed by numbers indicating the length of the value
- float-floating-point type, divided into Float16, float32, float64, followed by numbers indicating the length of the value
- String-The type of strings, broken into string or Unicode, that differ in how characters are stored
Indexes and Shards
Keep in mind that the index of the array starts at 0
Matrix = Numpy.array ( [[5,10,15], [20,25,30], [35,40,45] ]) Print (Matrix[1][1]) # two ways to index the data, the output is 25, note that here is the second row of the second Print (matrix[1,1])
Can be similar to using slices to manipulate data (slice operator: understood as ' all ')
Print # output all rows, data for the first column [5] Print # output First row, all columns of data [5] Print # output All rows, the first 2 columns of data [ [5, ten], [[+], [+], [+ ] ]print# Output data for all columns on lines 2nd and 3rd [ [[+] , [+], [+],]print# output 2nd, 3 rows 1th column data [[ Ten], [ +]]
Array comparison
When you compare an array to a value, you actually compare each value in the array to that value, and then return a list of Boolean values
vector = Numpy.array ([5, ten, += ) returns: [False, True, False, false]
The same is true for matrices
Matrix = Numpy.array ([ [5, ten, + ], [+], [+ ] The results are as follows: [ [False, False, false], [False, True, false], [False, False, false]]
Multiple conditions can also be used in array comparisons
vector = Numpy.array ([5, ten, += (vector = =) & (vector = = 5= (vector = = 10) | (Vector = = 5) Output: [True, True, False, false]
The greatest use of array comparisons is
One, used to select elements in an array or matrix
Matrix = Numpy.array ([ [5, ten, +], [+, +], [+], []] ) = (matrix[:,1] = =)print(matrix[second_column_25,:]) Function: Extracts all rows in the second column of matrix equal to 25, resulting in [20, 25, 30]
Second, replace the element
vector = Numpy.array ([5, ten, += (vector = = 10) | (vector = =5 =print(vector) output: [50, 50, 15, 20]
The principle is as follows:
Often used to replace empty elements
For example, replace the empty data in column fifth in World_alcohol with string 0:
' ' '0'
Data type conversions
Transform the data type of an array by using the Astype () function
vector = Numpy.array (["1""2""3" = vector.astype (float)print(vector) Results: [1.0, 2.0, 3.0]
A simple operation
Manual for reference numpy: http://docs.scipy.org/doc/numpy-1.10.1/index.html
Pick out a few important arithmetic functions:
- L sum ()-Calculates the sum of all the elements in a vector, or the sum of one dimension in a matrix
- L mean ()--ibid., calculated as average
- L Max ()--ditto, calculates the maximum value
vector = Numpy.array ([5, ten, +]) vector.sum () The result is:= Numpy.array ([ [5, ten,], [A], [+, +] ]) matrix.sum (axis=1) Results: [+], for matrices, you need to specify the axis parameter, This parameter equals 1, which means that each row is computed, or equal to 0, the sum of each column is calculated.
Practice
Use the World_alcohol.csv file to calculate beverage consumption in each country for 1986 years
ImportNumpyworld_alcohol= Numpy.genfromtxt ('World_alcohol.csv', dtype='U75', Skip_header=true, delimiter=',') Totals={}year= world_alcohol[world_alcohol[:, 0] = ='1989', :]#Select a data set of 1989 yearscountries= Set (world_alcohol[:,2])#Select all countries foreachinchCountries:#calculate each country separatelyconsumption= year[year[:,2] = =Each ,:] consumption[consumption[:,4] = ="', 4] ='0'Temp= Consumption[:,4].astype (float)#Convert empty data to floating point number 0 participating operationscountry_consumption=temp.sum () Totals[each]= Country_consumptionSummarize
Using NumPy is more convenient than directly working with a list set, and is better than the following:
- Easier to calculate data
- Data indexing and sharding can be done quickly
- Data types can be converted quickly
However, NumPy has some shortcomings:
- Data in the same dataset must have the same data type, which can become difficult when working with multiple datasets
- Rows and columns need to be indexed using numbers, but not aliases, which can easily cause confusion
And pandas solved a few shortcomings of numpy
NumPy Simple Introduction