Numpy_ ndaray element calculation function
Element calculation function ceil (): upward nearest integer, argument is number or arrayfloor (): downward nearest integer, parameter is number or arrayrint (): rounded, parameter is number or arrayisnan (): determine whether the element is NaN (Not a Number), parameter is number or arraymultiply (): element multiplication, parameter is number or arraydivide (): element division, parameter is number or arrayabs (): absolute value of the element The argument is number or arraywhere (condition, x, y): tricolor operator, x if condition else y
Sample code (1x7)
# randn () returns the sequence arr with standard normal distribution arr = np.random.randn (2,3) print (arr) print (np.ceil (arr)) print (np.floor (arr)) print (np.rint (arr)) print (np.isnan (arr) print (np.multiply (arr, arr)) print (np.divide (arr, arr)) print (np.where (arr > 0,1,-1))
Running result:
# print (arr) [[- 0.75803752 0.0314314 1.15323032] [1.17567832 0.43641395 0.26288021]] # print (np.ceil (arr)) [[- 0. 1. 2.] [2. 1. 1.] # print (np.floor (arr)) [[- 1. 0. 1.] [1. 0. 0.] # print (np.rint (arr)) [[- 1. 0. 1.] [1. 0. 0.]] # print (np.isnan (arr)) [[False False False] [False False False]] # print (np.multiply (arr, arr)) [[5.16284053e+00 1.77170104e+00 3.04027254e-02] [5.11465231e-03 3.46109263e+00 1.37512421e-02]] # print (np.divide (arr, arr)) [[1. 1. 1.] [1. 1.]] # print (np.where (arr > 0,1 -1)) >? [[- 11 1] [1 11] element statistics function np.mean (), np.sum (): the average of all elements The sum of all elements, parameter is number or arraynp.max (), np.min (): the maximum value of all elements, the minimum value of all elements, the parameter is number or arraynp.std (), np.var (): the standard deviation of all elements, the variance of all elements, the parameter is number or arraynp.argmax (), np.argmin (): the subscript index value of the maximum value, the subscript index value of the minimum value Parameter is number or arraynp.cumsum (), np.cumprod (): returns an one-dimensional array. Each element is the cumulative sum and product of all previous elements. The parameter is number or array multi-dimensional array, which counts all dimensions by default. The axis parameter can be counted by specified axis, 0 by column, and 1 by row.
Sample code:
Arr = np.arange (12) .reshape (3,4) print (arr) print (np.cumsum (arr)) # returns an one-dimensional array with each element being the sum of all previous elements and print (np.sum (arr)) # all elements and print (np.sum (arr, axis=0)) # array by column statistics and print (np.sum (arr, axis=1)) # array by row statistics
Running result:
# print (arr) [[0 12 3] [45 6 7] [8 9 10 11]] # print (np.cumsum (arr)) [0 1 36 10 15 21 28 36 45 55 66] # print (np.sum (arr)) # sum of all elements 6 print (np.sum (arr, axis=0)) # 0 represents statistics for each column of the array and [12 15 18 21] # print (np.sum (arr) Axis=1)) # 1 indicates the statistics of each row of the array and the [6 22 38] element judgment function np.any (): at least one element meets the specified condition Return Truenp.all (): all elements meet the specified conditions, return True
Sample code:
Arr = np.random.randn (2,3) print (arr) print (np.any (arr > 0)) print (np.all (arr > 0))
Running result:
[[0.05075769-1.31919688-1.80636984] [- 1.29317016-1.3336612-0.19316432]] TrueFalse element dereordering function
Np.unique (): finds a unique value and returns the sort result, similar to the set collection of Python
Sample code:
Arr = np.array ([[1,2,1], [2,3,4]]) print (arr) print (np.unique (arr))
Running result:
[[1 2 1] [2 3 4]] [1 2 3 4]