Article Directory
Element calculation function
ceil()
: Up to the nearest integer, number or array parametersfloor()
: Down to the nearest integer, number or array parametersrint()
: Rounding, or array parameter is the numberisnan()
: Determining whether the element is a NaN (Not a Number), a number or parameter arraymultiply()
: Multiplication element, number or array parametersdivide()
: Division elements, or array parameter is the numberabs()
: The absolute value of the element, the parameter is a number or arraywhere(condition, x, y)
: Ternary operator, x if condition else y
Sample Code (1,2,3,4,5,6,7):
# randn() 返回具有标准正态分布的序列。
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))
operation 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. 1.]]
# print(np.where(arr > 0, 1, -1))
[[ 1 1 -1]
[-1 1 1]]
Elements of statistical functions
np.mean()
,np.sum()
: Average value of all elements, all elements and parameters are number or arraynp.max()
,np.min()
: The maximum value of all elements, all the elements of the minimum value, the parameter is the number or arraynp.std()
,np.var()
: All standard deviation of the elements, all the elements of the variance, or array parameter is the numbernp.argmax()
,np.argmin()
: The maximum index value index, the index value of minimum index, number or array parametersnp.cumsum()
,np.cumprod()
: Returns a one-dimensional array, each element and all previous accumulated elements and tired product, number or array parameters- Multidimensional array default statistics for all dimensions,
axis
parameters can be specified axis of statistics, the value of0
press column statistics, the value of1
press row statistics.
Sample code:
arr = np.arange(12).reshape(3,4)
print(arr)
print(np.cumsum(arr)) # 返回一个一维数组,每个元素都是之前所有元素的 累加和
print(np.sum(arr)) # 所有元素的和
print(np.sum(arr, axis=0)) # 数组的按列统计和
print(np.sum(arr, axis=1)) # 数组的按行统计和
operation result:
# print(arr)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
# print(np.cumsum(arr))
[ 0 1 3 6 10 15 21 28 36 45 55 66]
# print(np.sum(arr)) # 所有元素的和
66
# print(np.sum(arr, axis=0)) # 0表示对数组的每一列的统计和
[12 15 18 21]
# print(np.sum(arr, axis=1)) # 1表示数组的每一行的统计和
[ 6 22 38]
Analyzing the function element
np.any()
: There is at least one element specified conditions are met, returns Truenp.all()
: All elements meet specified criteria, returns True
Sample code:
arr = np.random.randn(2,3)
print(arr)
print(np.any(arr > 0))
print(np.all(arr > 0))
operation result:
[[ 0.05075769 -1.31919688 -1.80636984]
[-1.29317016 -1.3336612 -0.19316432]]
True
False
Elements to reorder function
np.unique()
: Find unique values and returns the result of the sort, similar to the set of a collection of Python
Sample code:
arr = np.array([[1, 2, 1], [2, 3, 4]])
print(arr)
print(np.unique(arr))
operation result:
[[1 2 1]
[2 3 4]]
[1 2 3 4]
Copyright © BigCat all right reserved,powered by Gitbook「Revision Time: 2017-03-12 23:55:41」