NumPy
( Numerical Python
) Is Python
a language extension library that supports a large number of dimensions of the array and matrix operations, in addition, provide a lot of math library for array operations. Are learning machine learning, we should have a very basic and practical depth before learning Python
library.
Import library, create an array
Import numpy NP AS A = np.arraya np.array = ([0,. 1, 2,. 3,. 4]) # using the array function A = np.array ([[. 11, 12 is, 13 is, 14, 15 ], [ 16, 17, 18, 19, 20 ], [ 21, 22, 23, 24, 25 ], [ 26, 27, 28, 29, 30 ], [ 31, 32, 33, 34, 35]]) # Create multidimensional arrays a = np.zeros ((2,. 3)) # Create two rows and three columns filled matrix 0, ones (shape) is to create a filled, np.full ((m, n) 8) m rows n-th column are all the parameters 8 a np.linspace = (. 1., 4.,. 6) # Create between 1 and 4, the equivalent spacing of 6-element array a = np.arange (play, stop, step size) #To create from play, arranged in the array in steps A np.indices = ((3,3)) # create a stacked array of higher dimension A = np.mat () # create a matrix, array only from the list is generated, and the mat may be generated from a string or list, such as mat ( "1,2; 3,4") , the array ([1,2,3,4]), mat is a matrix, array is an array ( false matrix)
Basic operator
Matrix arithmetic np between corresponding elements +, -, *, / , [Note] an array with an integer, each element of the array is the integer of the addition, the process became broadcast array, if the order different numbers of each row is multiplied per row. If the use of the matrix mat * is matrix multiplication, instead of multiplying the corresponding elements of the other calculation function: Multiply (), the position corresponding to the array or matrix multiplication DOT () function, a.dot (b) indicates matrix multiplication ab , multiplied on mathematics. SUM () # summing defined axis direction may be used, 0 is a longitudinal, a transverse. [[...], [...], [...]] sideways so he would seek to put the default is a two-dimensional matrix, the final result is [...] min () # find the minimum elements max () # to find the largest element of mean () # returns mean std () # returns the standard variance var () # returns the variance cumprod () # original array elements of the first few positions multiplied (multiplier tired group) You may be used to specify the direction of axis, a longitudinal 0, 1 represents a lateral, transverse default cumsum () # original first few elements of the array and the location (count array) PTP () # Returns the maximum value minus the minimum
np indexing and sliced
Import numpy NP AS Data = np.arange (12 is) .reshape ((. 3,. 4 )) Print (Data) # # index into the array and slice # Data 1. taking a first row Print (Data [0]) # 2. take the first column of data Print (data.T [0]) Print (data [:,. 1 ]) # 3. Get multi-line Print (data [: 2 ]) # 4. Get multiple ranks Print (data .T [: 2 ]) Print (Data [:,: 2 ]) # 5. the retrieval of the first row of the specified columns; Print (Data) Print (Data [[0,2],: 2 ]) Print (Data [ : 2, [0,2]])