Index and Slice of Array
Index of multidimensional arrays
1 import numpy as np 2 arr=np.arange(1,25).reshape(2,3,4) 3 arr 4 # output array([[[ 1, 2, 3, 4], 5 [ 5, 6, 7, 8], 6 [ 9, 10, 11, 12]], 7 8 [[13, 14, 15, 16], 9 [17, 18, 19, 20], 10 [21, 22, 23, 24]]]) 11 arr[1][2][3] 12 # Output 24 13 arr[1,2,3] # and arr[1][2][3]The result is the same. 14 # Output 24 15 16 arr[0,0:2,1:3] 17 # output array([[2, 3], 18 [6, 7]]) 19 arr[0][0:2][1:3] # and arr[0,0:2,1:3]The results are different. 20 # output array([[5, 6, 7, 8]]) 21 22 arr[0,1:2,1:3] 23 # output array([[6, 7]]) 24 arr[0][0:2][1][1:3] # In this way and arr[0,1:2,1:3]Consensus can only be achieved. 25 # output array([6, 7])
2. Slices of arrays in NumPy
3. Boolean Index
1 # Add a code 2 arr>6 3 # output array([[[False, False, False, False], 4 [False, False, True, True], 5 [ True, True, True, True]], 6 7 [[ True, True, True, True], 8 [ True, True, True, True], 9 [ True, True, True, True]]]) 10 11 arr[arr>6] 12 # output array([ 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
4. Fancy Index
1 import numpy as np 2 arr1 = np.arange(32).reshape(8,4) 3 # output array([[ 0, 1, 2, 3], 4 [ 4, 5, 6, 7], 5 [ 8, 9, 10, 11], 6 [12, 13, 14, 15], 7 [16, 17, 18, 19], 8 [20, 21, 22, 23], 9 [24, 25, 26, 27], 10 [28, 29, 30, 31]]) 11 12 arr1[[0,3,5],[0,3,2]] # The former takes out the row, and the latter takes out the corresponding column bit by bit. 13 # output array([ 0, 15, 22]) 14 15 arr1[[0,3,5]] 16 # output array([[ 0, 1, 2, 3], 17 [12, 13, 14, 15], 18 [20, 21, 22, 23]]) 19 20 arr1[np.ix_([0,3,5],[0,2,1,3])] # [0,2,1,3]You can change the location of columns 21 # output array([[ 0, 2, 1, 3], 22 [12, 14, 13, 15], 23 [20, 22, 21, 23]]) 24
Array transposition and axis transposition
1. transpose function is used for array transpose, which is row-column interchange for two-dimensional arrays.
2. The T attribute of an array is also a transpose.
arr1 = arr.T works the same as arr2=arr.transpose().
General Function: Fast Elemental Series Array Function
ufunc: A function that performs element-level operations on data in ndarray. It can also be viewed as a vectorized package of simple functions (accepting one or more scalar values and generating one or more scalar values).
Univariate ufunc |
Explain |
abs,fabs |
Calculate the absolute values of integers, floating-point numbers, or complex numbers. For non-complex values, faster fabs can be used |
sqrt |
Calculate the square root of each element, equal to arr**0.5 |
square |
Calculate the square of each element, equal to arr**2 |
exp |
Calculating the x-Power of the exponent e of each element |
log,log10log2 log1p |
Logs with natural logarithm and base number 10, logs with base number 2 and logs (1 + x) |
sign |
Calculate the positive and negative sign of each element: 1 positive, 0 zero, and -1 negative. |
cell |
Calculate the ceiling value of each element, i.e. the smallest integer greater than or equal to that value |
floor |
Calculate the floor value of each element, i.e. the largest integer less than or equal to that value |
rint |
Round the values of each element to the nearest integer, preserving dtype |
modf |
Returns the decimal and integer parts of an array as two separate arrays |
isnan |
Returns an array of Boolean types indicating "Which values are NaN (not a number)" |
isfinite,isinf |
Returns a Boolean array indicating "which elements are finite (non-inf, non-NaN)" or "which elements are infinite" |
cos,cosh,sin sinh,tan,tanh |
Trigonometric Functions of Common Type and Hyperbolic Type |
arccos,arccosh, arcsin,arctan, arctanh |
Inverse trigonometric function |
logical_not |
Calculate the true value of each element not x, which corresponds to ~and-arr |
add |
Add the elements corresponding to the same position in the array |
substract |
Subtract elements from the first array from the second array |
multiply |
Multiplication of Array Elements |
divide,floor_divive |
Dividing or downward circular dividing (discarding residues) |
pow |
For element A in the first array, the B power of A is calculated based on the corresponding element B in the second array. |
maximum,fmax |
Maximum element level, fmax ignores NaN |
minimum,fmin |
The minimum value at the element level, fmin ignores NaN |
mod |
Modulus of element level (remainder of division) |
copysign |
Copy the symbol of the value in the second array to the value at the corresponding position in the first array |
greater, greater_equal,less less_equal,equal not_equal |
Perform element level comparison operations, resulting in Boolean arrays |
logical_and, logical_or, logical_xor |
Perform element-level Boolean logic operations, equivalent to the infix operators &, |,,.^ |
Aggregation function
1. An aggregate function is a function that operates on a set of values (such as an array) and returns a single value as a result. So the function of summing all elements of an array, finding the maximum and minimum values of all elements and the standard deviation of all elements is the aggregation function.
arr.max() arr.min() arr.mean()
Standard deviation of arr.std(), equivalent to np.sqrt(np.power(arr-arr.mean(),2).sum()/arr.size)
2. Aggregation functions can specify operations on an axis element of a value
Ar. mean (axis = 0) takes the mean for each column; arr.mean(axis=1) takes the mean for each row
Aggregate elements on the same column when axis=0. Aggregate elements on the same row when axis=1
np.where function
1. np.where function is a vector version of ternary expression x if condition else y
1 import numpy as np 2 xarr=np.array([1.1,1.2,1.3,1.4,1.5]) 3 yarr=np.array([2.1,2.2,2.3,2.4,2.5]) 4 condition=np.array([True,False,True,True,False]) 5 result=[(x if c else y) for x,y,c in zip(xarr,yarr,condition)] 6 result 7 # output [1.1, 2.2, 1.3, 1.4, 2.5] 8 result2=np.where(condition,xarr,yarr) 9 result2 10 # output array([1.1, 2.2, 1.3, 1.4, 2.5])
2. Case: Replace all NaN missing values in the array with 0
1 import numpy as np 2 arr=np.array([[1,2,np.NaN,4],[3,4,5,np.NaN]]) 3 arr 4 # output array([[ 1., 2., nan, 4.], 5 [ 3., 4., 5., nan]]) 6 np.isnan(arr) 7 # output array([[False, False, True, False], 8 [False, False, False, True]]) 9 np.where(np.isnan(arr),0,arr) 10 # output array([[1., 2., 0., 4.], 11 [3., 4., 5., 0.]])
np.unique function
Find non-repetitive elements in an array
1 import numpy as np 2 pd=np.array(['books','Digital','snack','Delicious food','Men's wear','Delicious food','Women's wear','snack']) 3 np.unique(pd) 4 # output array(['books', 'Women's wear', 'snack', 'Digital', 'Men's wear', 'Delicious food'], dtype='<U2')
Array Data File Reading and Writing
1. Save arrays to disk in binary format
1 import numpy as np 2 data = np.array([[1,2,3,4],[2,3,4,5],[6,7,8,9],[2,3,4,6]]) 3 data 4 # output array([[1, 2, 3, 4], 5 [2, 3, 4, 5], 6 [6, 7, 8, 9], 7 [2, 3, 4, 6]]) 8 np.save('data',data) # Store multidimensional arrays in files and automatically add suffixes.npy (Binary file) 9 np.load('data.npy') # To read a file, you need to add the corresponding suffix 10 # output array([[1, 2, 3, 4], 11 [2, 3, 4, 5], 12 [6, 7, 8, 9], 13 [2, 3, 4, 6]])
2. Accessing text files
1 import numpy as np 2 exp = np.loadtxt('example.csv',delimiter=',') 3 exp 4 # output array([[1., 2., 3., 4.], 5 [2., 4., 5., 7.], 6 [4., 1., 5., 9.]]) 7 8 np.genfromtxt('example.csv',delimiter=',') 9 # output array([[1., 2., 3., 4.], 10 [2., 4., 5., 7.], 11 [4., 1., 5., 9.]]) 12
3. Data writing to text files
1 import numpy as np 2 np.savetxt('arr.csv',exp.reshape((2,6)),delimiter=',',fmt='%.2f') 3 exp2=np.random.random((2,3,4)) 4 # If the array is more than two-dimensional, it must be converted to two-dimensional array for storage, otherwise there will be errors. 5 np.savetxt('arr1.csv',arr3.reshape((4,6)),delimiter=',')