numpy Library - basic operations (Part I)
Introduction of numpy Library:
import numpy as np
1. Build array
1.1 creating an array with array
1.1.1 establishment of one-dimensional array
>>> import numpy as np >>> a1 = np.array([1,2,3,4,5,6]) #Parameter is list >>> a1 array([1, 2, 3, 4, 5, 6]) >>> type(a1) <class 'numpy.ndarray'>
>>> a2 = np.array((1,2,3,4,5,6)) >>> a2 array([1, 2, 3, 4, 5, 6]) >>> type(a2) <class 'numpy.ndarray'>
1.1.2 establishment of two-dimensional array
>>> b1 = ([[1,2,3],[4,5,6]]) >>> b1 [[1, 2, 3], [4, 5, 6]]
>>> b2 = ([(0,0,0),(1,2,3),(4,5,6)]) >>> b2 [(0, 0, 0), (1, 2, 3), (4, 5, 6)]
1.1.3 establishment of 3D array
>>> c1 = np.array([[[1,2,3], [4,5,6]], [[1,1,1], [2,2,2]], [[4,4,4], [5,5,6]]]) >>> c1 array([[[1, 2, 3], [4, 5, 6]], [[1, 1, 1], [2, 2, 2]], [[4, 4, 4], [5, 5, 6]]]) >>> type(c1) <class 'numpy.ndarray'>
1.1.4 precautions
- In addition to integer, the types of array elements include string, Boolean, floating-point, complex, etc.
import numpy as np d1 = np.array(['b', 'China', '100003600']) d2 = np.array([True, False]) d3 = np.array([-1, 2, 10]) d4 = np.array([10.5, 6.2222, 7.248888]) d5 = np.array([10+2j, 8j, 2.1+3j]) print('d1:', d1,'\n','d2:', d2,'\n', 'd3:', d3,'\n','d4:', d4,'\n','d5:', d5) print('d1.dtype:',d1.dtype) #Viewing array element types using dtytpe print('d2.dtype:',d2.dtype) print('d3.dtype:',d3.dtype) print('d4.dtype:',d4.dtype) print('d5.dtype:',d5.dtype) print('d2.alltrue:', np.alltrue(d2)) print('d3.alltrue:', np.alltrue(d3)) #Use alltrue to test whether all array elements are True >>> output: d1: ['b' 'China' '100003600'] d2: [ True False] d3: [-1 2 10] d4: [10.5 6.2222 7.248888] d5: [10. +2.j 0. +8.j 2.1+3.j] d1.dtype: <U9 d2.dtype: bool d3.dtype: int32 d4.dtype: float64 d5.dtype: complex128 d2.alltrue: False d3.alltrue: True
- The array elements of numpy must be of the same type, that is, they cannot be both integer and string. If you accidentally enter elements of different types, the array function will automatically convert elements of other types.
import numpy as np e1 = np.array(['OK?', 10, 'year', 0.3, False]) #The input value type is inconsistent print('e1:', e1) print('e1.dtype:', e1.dtype) e2 = np.array([1, 0.2, 2.3333333]) print('e2:', e2) print('e2.dtype:', e2.dtype) e3 = np.array([1, 0.2, 2.3333333, 1+8j]) print('e3:', e3) print('e3.dtype:', e3.dtype) >>> output: e1: ['OK?' '10' 'year' '0.3' 'False'] e1.dtype: <U32 e2: [1. 0.2 2.3333333] e2.dtype: float64 e3: [1. +0.j 0.2 +0.j 2.3333333+0.j 1. +8.j] e3.dtype: complex128
1.2 establishment methods of other common arrays
1.2.1 arange() function
- Accumulates at the specified step size to produce an array of ordered elements in the specified range.
- The function uses the format: numpy. Orange ([start,] stop [, step,], dtype = none), where start specifies the start number, stop specifies the end number, step is the incremental step size, and dtype can specify the numerical type of generating array elements.
import numpy as np f1 = np.arange(1, 10, 2, dtype = 'float64') # From 1 to 10 (excluding 10), take a number every 2, and convert all elements to 'float64'. f2 = np.arange(5) f3 = np.arange(0, 5) f4 = np.arange(0, 5, 0.5) f5 = np.arange(5, 0, -1) print('f1:', f1) print('f2:', f2) print('f3:', f3) print('f4:', f4) print('f5:', f5) >>> output: f1: [1. 3. 5. 7. 9.] f2: [0 1 2 3 4] f3: [0 1 2 3 4] f4: [0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5] f5: [5 4 3 2 1]
1.2.2 linspace() function
- Returns a sample array of uniform steps within the specified range.
- Function format: numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0), where start specifies the start number and stop specifies the end number. These two parameters must be provided and others are optional. endpoint=True indicates that the specified stop is also included, and retstep=True indicates the number of steps in the returned array.
import numpy as np g1 = np.linspace(0,1,2) g2 = np.linspace(0,4,4) g3 = np.linspace(0,4,10) print('g1:', g1) print('g2:', g2) print('g3:', g3) g4 = np.linspace(0,4,4,endpoint = False) print('g4:', g4) g5 = np.linspace(0,4,4,retstep = True) print('g5:', g5) g6 = np.linspace(0,4,4,endpoint = False,retstep = True) print('g6:', g6) >>> output: g1: [0. 1.] g2: [0. 1.33333333 2.66666667 4. ] g3: [0. 0.44444444 0.88888889 1.33333333 1.77777778 2.22222222 2.66666667 3.11111111 3.55555556 4. ] g4: [0. 1. 2. 3.] g5: (array([0. , 1.33333333, 2.66666667, 4. ]), 1.3333333333333333) g6: (array([0., 1., 2., 3.]), 1.0)
1.2.3 zeros() function
- Generate an array with a value of 0.
- The function uses the format: numpy.zeros(shape, dtype =float, order = 'C'), where order specifies whether the storage format is C language style (c) or F language style (F).
import numpy as np z1 = np.zeros(5) z2 = np.zeros((3,3)) print('z1:',z1) print('z2:',z2) >>> output: z1: [0. 0. 0. 0. 0.] z2: [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]
1.2.4 ones() function
- Generate an array with a value of 1.
- The function uses the format: numpy.oneos(shape, dtype =float, order = 'C').
import numpy as np o1 = np.ones(5) o2 = np.ones((2,3)) print('o1:',o1) print('o2:',o2) >>> output: o1: [1. 1. 1. 1. 1.] o2: [[1. 1. 1.] [1. 1. 1.]]
1.2.5 empty() function
- Generates an array with no specified value, and the values in the array are not specified.
- The function uses the format: numpy.empty(shape, dtype =float, order = 'C').
import numpy as np e1 = np.empty(5) e2 = np.empty((4,4)) print('e1:',e1) print('e2:',e2) >>> output1: e1: [9.90263869e+067 8.01304531e+262 2.60799828e-310 0.00000000e+000 0.00000000e+000] e2: [[6.23042070e-307 4.67296746e-307 1.69121096e-306 1.29061074e-306] [1.89146896e-307 7.56571288e-307 3.11525958e-307 1.24610723e-306] [1.37962320e-306 1.29060871e-306 2.22518251e-306 1.33511969e-306] [1.78022342e-306 1.05700345e-307 1.11261027e-306 4.84184333e-322]] >>> output2: e1: [ 6.23042070e-307 1.42417221e-306 1.37961641e-306 -2.65972661e-207 7.22947795e+223] e2: [[6.23042070e-307 4.67296746e-307 1.69121096e-306 1.29061074e-306] [1.89146896e-307 7.56571288e-307 3.11525958e-307 1.24610723e-306] [1.37962320e-306 1.29060871e-306 2.22518251e-306 1.33511969e-306] [1.78022342e-306 1.05700345e-307 3.11521884e-307 3.72363246e-317]]
1.2.6 logspace() function
- Returns numbers evenly spaced on a logarithmic scale.
- Function format: numpy.linspace(start, stop, num=50, endpoint=True, base=10, dtype=None, axis=0), where the base parameter is the base of the specified logarithm, and the default is 10
import numpy as np h1 = np.logspace(2.0, 3.0, 5) print('h1:', h1) >>> output: h1: [ 100. 177.827941 316.22776602 562.34132519 1000. ]
1.2.7 full() function
- Returns an array of specified values.
- The function uses the format: numpy.full(shape, fill_value, dtype=None, order='C '), where, fill_ Value specifies the value to fill in.
import numpy as np f1 = np.full(5,10) f2 = np.full((3,3), 8) f3 = np.full((3,3), np.inf) # inf is positive infinity f4 = np.full((3,3), 'China') print('f1:', f1) print('f2:', f2) print('f3:', f3) print('f4:', f4) >>> output: f1: [10 10 10 10 10] f2: [[8 8 8] [8 8 8] [8 8 8]] f3: [[inf inf inf] [inf inf inf] [inf inf inf]] f4: [['China' 'China' 'China'] ['China' 'China' 'China'] ['China' 'China' 'China']]
1.2.8 eye() function
- Returns a two-dimensional array with a diagonal of 1 and others of 0.
- The function uses the format: numpy. Eye (N, M = none, k = 0, dtype = < class' float '>, order ='c'), where N specifies the number of rows returned from the array, m specifies the number of columns returned from the array (M=N by default), K is used to specify the diagonal position, 0 is the main diagonal, a positive number is the upper diagonal, and a negative number is the lower diagonal.
import numpy as np e1 = np.eye(4) e2 = np.eye(4,4, 2) e3 = np.eye(4,4, -2) print('e1:', e1) print('e2:', e2) print('e3:', e3) >>> output: e1: [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]] e2: [[0. 0. 1. 0.] [0. 0. 0. 1.] [0. 0. 0. 0.] [0. 0. 0. 0.]] e3: [[0. 0. 0. 0.] [0. 0. 0. 0.] [1. 0. 0. 0.] [0. 1. 0. 0.]]
1.2.9 repeat() function
- Create an array in which each element is repeated N times.
- The function uses the format: numpy.repeat(a,repeats,axis=None). A is the collection object, and repeats is the specified number of element repetitions. In the case of multi-dimensional array, axis can specify the direction of the repeated dimension.
import numpy as np r1 = np.repeat([0,1,0], 5) print('r1:', r1) >>> output: r1: [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0]
1.3 use of array attributes
- ndim attribute: returns the dimension of the array.
- Shape property: returns the shape size of the array.
- size attribute: returns the number of array elements.
- dtype property: returns the array element type.
- itemsize property: returns the byte size of an array element.
import numpy as np t1 = np.array([['a','b','c'],['d','e','f'],['g','h','i']]) print('t1:', t1) print('t1.ndim:', t1.ndim) print('t1.shape:', t1.shape) print('t1.size:', t1.size) print('t1.dtype:', t1.dtype) print('t1.itemsize:', t1.itemsize) >>> output: t1: [['a' 'b' 'c'] ['d' 'e' 'f'] ['g' 'h' 'i']] t1.ndim: 2 t1.shape: (3, 3) t1.size: 9 t1.dtype: dtype('<U1') t1.itemsize: 4
1.4 use of array method
- reshape method: change the shape of the array.
- all method: to judge whether the specified array elements are non-0, return True; otherwise, return False.
- any method: judge that if the array element has a non-0 value, it returns True; otherwise, it returns False.
- Copy method: copies a copy of the array.
- astype method: change the type of array element.
import numpy as np t1 = np.arange(9) # reshape method t2 = t1.reshape(3,3) #Convert from a one-dimensional array to a two-dimensional array of 3 rows and 3 columns. print('t1:', t1) print('t2:', t2) # all method t3 = np.ones(9).all() t4 = np.array([1,0,2]) t5 = np.array([[1,0,2],[1,2,3]]) print('t3:',t3) print('t4.all:', t4.all()) print('t5.all(axis=1):', t5.all(axis=1)) # Judge whether each row is 0 from the row direction. # The parameter axis points to the dimension direction, and 0 indicates the second dimension and column direction; 1 indicates the first dimension, row direction, and so on. # any method t6 = np.array([[1,0,2],[0,0,2]]) print('t6.any(axis=1):', t6.any(axis=1)) print('t6.any(axis=0):', t6.any(axis=0)) # copy method t7 = np.array([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]) t8 = t7 t9 = np.copy(t7) print('id(t7):', id(t7)) print('id(t8):', id(t8)) print('id(t9):', id(t9)) # astype method t10 = np.ones(9, dtype = int) print('t10:', t10) print('t10.astype(float):', t10.astype(float)) >>> output: t1: [0 1 2 3 4 5 6 7 8] t2: [[0 1 2] [3 4 5] [6 7 8]] t3: True t4.all: False t5.all(axis=1): [False True] t6.any(axis=1): [ True True] t6.any(axis=0): [ True False True] id(t7): 2182816274448 id(t8): 2182816274448 id(t9): 2182816274544 t10: [1 1 1 1 1 1 1 1 1] t10.astype(float): [1. 1. 1. 1. 1. 1. 1. 1. 1.]
2. Indexing and slicing
2.1 basic index
The basic index usage of Numpy array is the same as that of Python list object. Square brackets [] are also used to index a group of values.
import numpy as np # Reading and writing of single element of one-dimensional array n1=np.arange(10) print('n1:', n1) print('n1[9]:', n1[9]) print('n1[-1]:', n1[-1]) n1[0]=10 # Modify elements according to subscript (elements with subscript 0) print('n1:', n1) # Reading and writing of single element of two-dimensional array n2=n1.reshape(2,5) print('n2:', n2) print('n2[1,0]:', n2[1,0]) n2[1,1]=-1 # Modify element according to subscript (row 2, column 2) print('n2[1,1]:', n2[1,1]) # Reading and writing of single element of three-dimensional array n3=np.arange(12).reshape(2,2,3) print('n3:', n3) print('n3[1,0,0]:', n3[1,0,0]) #Subscript 1 is the 2nd row of the 3rd dimension, middle 0 is the 1st row of the 2nd dimension, and rightmost 0 is the 1st column of the 1st dimension. print('n3[1,1,1]:', n3[1,1,1]) n3[1,0,2]=-1 # Modify elements according to subscripts (3D: Row 2, 2D: row 1, 1D: column 3) print('n3:', n3) >>> output: n1: [0 1 2 3 4 5 6 7 8 9] n1[9]: 9 n1[-1]: 9 n1: [10 1 2 3 4 5 6 7 8 9] n2: [[10 1 2 3 4] [ 5 6 7 8 9]] n2[1,0]: 5 n2[1,1]: -1 n3: [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] #The number 8 is the second row of the third dimension, the first row of the second dimension, and the third column of the first dimension. n3[1,0,0]: 6 n3[1,1,1]: 10 n3: [[[ 0 1 2] [ 3 4 5]] [[ 6 7 -1] [ 9 10 11]]]
2.1.1 index omission usage
- When the dimension cannot be determined, you can read the array through the ellipsis to the right of the subscript or omit the subscript number directly.
- Omitting from the left of the subscript can only be in the form of "··", not in the form of n3[,2], otherwise an error will be reported.
- From the subscript middle dimension, only "··" can be used, and the subscript number in the middle cannot be directly omitted, otherwise an error will be reported.
import numpy as np n3=np.arange(12).reshape(2,2,3) print('n3:', n3) print('n3[1,...]:',n3[1,...]) # Omit right print('n3[1,]:',n3[1,]) print('n3[...,2]:',n3[...,2]) # Omit left print('n3[1,...,2]:',n3[1,...,2]) # Omit middle >>> output: n3: [[[ 0 1 2] [ 3 4 5]] [[ 6 7 8] [ 9 10 11]]] #The number 8 is the second row of the third dimension, the first row of the second dimension, and the third column of the first dimension. n3[1,...]: [[ 6 7 8] [ 9 10 11]] n3[1,]: [[ 6 7 8] [ 9 10 11]] n3[...,2]: [[ 2 5] [ 8 11]] n3[1,...,2]: [ 8 11]
2.1.2 generate array index
import numpy as np n4=np.arange(4).reshape(2,2) print('n4:', n4) print('n4[1][1]:', n4[1][1]) # Equivalent to n4[1,1] print('n4[1,1]:', n4[1,1]) >>> output: n4: [[0 1] [2 3]] n4[1][1]: 3 n4[1,1]: 3
2.2 slicing
The basic format of subscript slice is [b:e:s], B is the subscript start number, e is the subscript end number (the subscript of the number itself is not included, that is, it corresponds to the mathematically right open interval), s is the step size, and its default value is 1. b. E and s can be omitted arbitrarily.
import numpy as np # One dimensional array slice s1=np.arange(1,10) print('s1:', s1) print('s1[1:4]:', s1[1:4]) # Take the elements of one-dimensional array subscripts 1 ~ 3 print('s1[:5]:', s1[:5]) # Take the elements of one-dimensional array subscripts 0 ~ 4 print('s1[5:]:', s1[5:]) # Take the subscript 5 of one-dimensional array to the last element print('s1[:-1]:', s1[:-1]) # Take all the elements from the penultimate subscript of the one-dimensional array print('s1[:]:', s1[:]) # Take all subscript elements of a one-dimensional array print('s1[::2]:', s1[::2]) #Step size is 2 # 2D array slice s2=np.arange(9).reshape(3,3) print('s2:', s2) # 2D array row slicing print('s2[1:3]:', s2[1:3]) # Take 2 and 3 rows of subarray values, which is equivalent to s2[1:3,] or s2[1:3,:] print('s2[1:3,]:', s2[1:3,]) print('s2[1:3,:]:', s2[1:3,:]) print('s2[:2]:', s2[:2]) # Take 1 and 2 rows of subarray print('s2[2:]:', s2[2:]) # Take the sub array in line 3, which is equivalent to s2[2] # Row slice, column assignment print('s2[:,2]:', s2[:,2]) # Take all rows, column 3, subarray # 2D array column slicing print('s2[:,:2]:', s2[:,:2]) # Take all rows, column 1 and column 2 subarray print('s2[...,:]:', s2[...,:]) # Get all rows, all columns print('s2[1,2:]:', s2[1,2:]) # Take row 2 and column 3 # 3D array slice s3=np.array([[['Tom',10,'boy'],['John',11,'girl']],[['Alice',12,'girl'],['Kite',11,'boy']]]) print('s3:', s3) print('s3[1,1,:]:', s3[1,1,:]) #Get the second row of the third dimension, the first row of the second dimension, and the subarray of all columns of the first dimension print('s3[0,:,:2]:', s3[0,:,:2]) #Get the subarray of the first row, the first row and the second row in the third dimension, and the first and second columns in the first dimension >>> output: s1: [1 2 3 4 5 6 7 8 9] s1[1:4]: [2 3 4] s1[:5]: [1 2 3 4 5] s1[5:]: [6 7 8 9] s1[:-1]: [1 2 3 4 5 6 7 8] s1[:]: [1 2 3 4 5 6 7 8 9] s1[::2]: [1 3 5 7 9] s2: [[0 1 2] [3 4 5] [6 7 8]] s2[1:3]: [[3 4 5] [6 7 8]] s2[1:3,]: [[3 4 5] [6 7 8]] s2[1:3,:]: [[3 4 5] [6 7 8]] s2[:2]: [[0 1 2] [3 4 5]] s2[2:]: [[6 7 8]] s2[:,2]: [2 5 8] s2[:,:2]: [[0 1] [3 4] [6 7]] s2[...,:]: [[0 1 2] [3 4 5] [6 7 8]] s2[1,2:]: [5] s3: [[['Tom' '10' 'boy'] ['John' '11' 'girl']] [['Alice' '12' 'girl'] ['Kite' '11' 'boy']]] s3[1,1,:]: ['Kite' '11' 'boy'] s3[0,:,:2]: [['Tom' '10'] ['John' '11']]
2.3 fancy index
Fancy indexing uses all elements of an integer array as subscript values for indexing, also known as array index.
2.3.1 integer array index
import numpy as np # One dimensional array index fi1=np.array(['Tom cat','Garfield','Persian cat','black cat','British short faced cat','Pastoral cat']) f1=np.array([1,2,4,5]) print('fi1[f1]:', fi1[f1]) #Remove cats that are not cat breeds # Two dimensional array index # Use a one-dimensional entire array as the array index to generate a sub array of the specified row fi2=np.array([['Tom cat',1,200],['Garfield',10,1000],['Persian cat',5,2000],['black cat',2,180],['British short faced cat',8,1800],['Pastoral cat',20,100]]) f2=np.array([1,2,3]) # Specify rows 2, 3, and 4 with a one-dimensional array print('fi2[f2]:', fi2[f2]) # Specify the array of X and Y coordinates, and find the elements corresponding to the coordinates of the specified array to form a sub array. fi3=np.array([[0,-1,9],[8,1,10],[-2,8,3]]) print('fi3:', fi3) x=np.array([[0,1,2]]) #Specify the x coordinate value y=np.array([0,1,1]) #Specify the y coordinate value print('fi3[x,y]:', fi3[x,y]) #Find all elements corresponding to X and Y coordinates >>> output: fi1[f1]: ['Garfield' 'Persian cat' 'British short faced cat' 'Pastoral cat'] fi2[f2]: [['Garfield' '10' '1000'] ['Persian cat' '5' '2000'] ['black cat' '2' '180']] fi3: [[ 0 -1 9] [ 8 1 10] [-2 8 3]] fi3[x,y]: [[0 1 8]]
2.3.2 Boolean array index
-
Boolean index requires the Boolean array to keep the same shape as the quoted array, and the index result generates a new one-dimensional array.
-
In addition, Boolean indexes can be carried out in behavioral units.
import numpy as np # Boolean index s4=np.arange(9).reshape(3,3) print('s4:', s4) b1=np.array([[True,True,False],[False,True,False],[False,False,True]]) print('b1:', b1) print('s4[b1]:', s4[b1]) b2=b1[:,1] print('b2:', b2) print('s4[b2]:', s4[b2]) # Boolean index in behavioral units b3=np.array([True,False,False]) print('s4[b3]:', s4[b3]) >>> output: s4: [[0 1 2] [3 4 5] [6 7 8]] b1: [[ True True False] [False True False] [False False True]] s4[b1]: [0 1 4 8] b2: [ True True False] s4[b2]: [[0 1 2] [3 4 5]] s4[b3]: [[0 1 2]]
2.4 iteration
As a collection of elements, the array can be iterated to read the corresponding elements.
import numpy as np # Iteration of one-dimensional array print('g:') d1=np.arange(3) for g in d1: print(g) # Iteration of two-dimensional array print('------------------') print('g1:') d2=np.array([['Tom',1,10],['John',2,100],['Mike',3,200]]) for g1 in d2: print(g1) >>> output: g: 0 1 2 ------------------ g1: ['Tom' '1' '10'] ['John' '2' '100'] ['Mike' '3' '200']
END
Edit | sxlib
- Previous Catalog:
Python series 01: of data types - numbers
Python series 01: of data types - list + tuple
Python series 01: Dictionary of data types
Python series 01: collection of data types
Python series 01: of data types -- strings
Python series 02: the basis of syntax -- variables and branch structures
Python series 02: the basis of syntax - loop structure
Python series 02: Fundamentals of syntax - functions
Python series 03: file operation - basic operation
Python series 03: file operations - file attributes
Python series 03: file operation -- file and folder operation
Python series 04: xlrd Library
Python series 04: xlwt library and openpyxl Library