参考链接: Python中的numpy.not_equal
numpy数学函数和逻辑函数
算术运算numpy.add()numpy.subtract()numpy.multiply()numpy.divide()numpy.floor_divide(x1, x2)numpy.power()numpy.sqrt(x, *args, **kwargs)numpy.square(x, *args, **kwargs)
三角函数numpy.sin()numpy.cos()numpy.tan()numpy.arcsin()numpy.arccos()numpy.arctan()
指数和对数numpy.exp()numpy.log()numpy.exp2()numpy.log2()numpy.log10()
加法函数、乘法函数numpy.sumnumpy.cumsumnumpy.prod 乘积numpy.cumprod 累乘numpy.diff 差值
四舍五入numpy.aroundnumpy.ceilnumpy.floor
杂项numpy.clipnumpy.absnumpy.sign
真值测试numpy.allnumpy.any
数组内容numpy.isnan
逻辑运算numpy.logical_notnumpy.logical_andnumpy.logical_ornumpy.logical_xor
对照numpy.greaternumpy.greater_equalnumpy.equalnumpy.not_equalnumpy.lessnumpy.less_equal
算术运算
numpy.add()
numpy.subtract()
numpy.multiply()
numpy.divide()
numpy.floor_divide(x1, x2)
Return the largest integer smaller or equal to the division of the inputs.
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [[ 5 6 6 7 7]
# [ 8 8 9 9 10]
# [10 11 11 12 12]
# [13 13 14 14 15]
# [15 16 16 17 17]]
numpy.power()
numpy.sqrt(x, *args, **kwargs)
Return the non-negative square-root of an array, element-wise.
numpy.square(x, *args, **kwargs)
Return the element-wise square of the input.
x = np.arange(1, 5)
print(x) # [1 2 3 4]
y = np.sqrt(x)
print(y)
# [1. 1.41421356 1.73205081 2. ]
print(np.power(x, 0.5))
# [1. 1.41421356 1.73205081 2. ]
y = np.square(x)
print(y)
# [ 1 4 9 16]
print(np.power(x, 2))
# [ 1 4 9 16]
三角函数
numpy.sin()
numpy.cos()
numpy.tan()
numpy.arcsin()
numpy.arccos()
numpy.arctan()
指数和对数
numpy.exp()
numpy.log()
numpy.exp2()
numpy.log2()
numpy.log10()
加法函数、乘法函数
numpy.sum
numpy.sum(a[, axis=None, dtype=None, out=None, …]) Sum of array elements over a given axis.
通过不同的 axis,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作。但这只是简单的二位数组,如果是多维的呢?可以总结为一句话:设axis=i,则 numpy 沿着第i个下标变化的方向进行操作。
import numpy as np
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.sum(x)
print(y) # 575
y = np.sum(x, axis=0)
print(y) # [105 110 115 120 125]
y = np.sum(x, axis=1)
print(y) # [ 65 90 115 140 165]
numpy.cumsum
numpy.cumsum(a, axis=None, dtype=None, out=None) Return the cumulative sum of the elements along a given axis.
聚合函数 是指对一组值(比如一个数组)进行操作,返回一个单一值作为结果的函数。因而,求数组所有元素之和的函数就是聚合函数。ndarray类实现了多个这样的函数。
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.cumsum(x)
print(y)
# [ 11 23 36 50 65 81 98 116 135 155 176 198 221 245 270 296 323 351
# 380 410 441 473 506 540 575]
y = np.cumsum(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 27 29 31 33 35]
# [ 48 51 54 57 60]
# [ 74 78 82 86 90]
# [105 110 115 120 125]]
y = np.cumsum(x, axis=1)
print(y)
# [[ 11 23 36 50 65]
# [ 16 33 51 70 90]
# [ 21 43 66 90 115]
# [ 26 53 81 110 140]
# [ 31 63 96 130 165]]
numpy.prod 乘积
numpy.prod(a[, axis=None, dtype=None, out=None, …]) Return the product of array elements over a given axis.
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.prod(x)
print(y) # 788529152
y = np.prod(x, axis=0)
print(y)
# [2978976 3877632 4972968 6294624 7875000]
y = np.prod(x, axis=1)
print(y)
# [ 360360 1860480 6375600 17100720 38955840]
numpy.cumprod 累乘
numpy.cumprod(a, axis=None, dtype=None, out=None) Return the cumulative product of elements along a given axis.
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.cumprod(x)
print(y)
# [ 11 132 1716 24024 360360 5765760
# 98017920 1764322560 -837609728 427674624 391232512 17180672
# 395155456 893796352 870072320 1147043840 905412608 -418250752
# 755630080 1194065920 -1638662144 -897581056 444596224 -2063597568
# 788529152]
y = np.cumprod(x, axis=0)
print(y)
# [[ 11 12 13 14 15]
# [ 176 204 234 266 300]
# [ 3696 4488 5382 6384 7500]
# [ 96096 121176 150696 185136 225000]
# [2978976 3877632 4972968 6294624 7875000]]
y = np.cumprod(x, axis=1)
print(y)
# [[ 11 132 1716 24024 360360]
# [ 16 272 4896 93024 1860480]
# [ 21 462 10626 255024 6375600]
# [ 26 702 19656 570024 17100720]
# [ 31 992 32736 1113024 38955840]]
numpy.diff 差值
numpy.diff(a, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue) Calculate the n-th discrete difference along the given axis. a:输入矩阵 n:可选,代表要执行几次差值 axis:默认是最后一个
The first difference is given by out[i] = a[i 1] - a[i] along the given axis, higher differences are calculated by using diff recursively.
A = np.arange(2, 14).reshape((3, 4))
A[1, 1] = 8
print(A)
# [[ 2 3 4 5]
# [ 6 8 8 9]
# [10 11 12 13]]
print(np.diff(A))
# [[1 1 1]
# [2 0 1]
# [1 1 1]]
print(np.diff(A, axis=0))
# [[4 5 4 4]
# [4 3 4 4]]
四舍五入
numpy.around
numpy.around(a, decimals=0, out=None) Evenly round to the given number of decimals.
x = np.random.rand(3, 3) * 10
print(x)
# [[6.59144457 3.78566113 8.15321227]
# [1.68241475 3.78753332 7.68886328]
# [2.84255822 9.58106727 7.86678037]]
y = np.around(x)
print(y)
# [[ 7. 4. 8.]
# [ 2. 4. 8.]
# [ 3. 10. 8.]]
y = np.around(x, decimals=2)
print(y)
# [[6.59 3.79 8.15]
# [1.68 3.79 7.69]
# [2.84 9.58 7.87]]
numpy.ceil
上限 numpy.ceil(x, *args, **kwargs) Return the ceiling of the input, element-wise.
numpy.floor
下限 numpy.floor(x, *args, **kwargs) Return the floor of the input, element-wise.
x = np.random.rand(3, 3) * 10
print(x)
# [[0.67847795 1.33073923 4.53920122]
# [7.55724676 5.88854047 2.65502046]
# [8.67640444 8.80110812 5.97528726]]
y = np.ceil(x)
print(y)
# [[1. 2. 5.]
# [8. 6. 3.]
# [9. 9. 6.]]
y = np.floor(x)
print(y)
# [[0. 1. 4.]
# [7. 5. 2.]
# [8. 8. 5.]]
杂项
numpy.clip
裁剪 numpy.clip(a, a_min, a_max, out=None, **kwargs): Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1.
x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.clip(x, a_min=20, a_max=30)
print(y)
# [[20 20 20 20 20]
# [20 20 20 20 20]
# [21 22 23 24 25]
# [26 27 28 29 30]
# [30 30 30 30 30]]
numpy.abs
numpy.sign
返回数字符号的逐元素指示
x = np.arange(-5, 5)
print(x)
#[-5 -4 -3 -2 -1 0 1 2 3 4]
print(np.sign(x))
#[-1 -1 -1 -1 -1 0 1 1 1 1]
真值测试
numpy.all
numpy.all(a, axis=None, out=None, keepdims=np._NoValue) Test whether all array elements along a given axis evaluate to True.
numpy.any
numpy.any(a, axis=None, out=None, keepdims=np._NoValue) Test whether any array element along a given axis evaluates to True.
a = np.array([0, 4, 5])
b = np.copy(a)
print(np.all(a == b)) # True
print(np.any(a == b)) # True
b[0] = 1
print(np.all(a == b)) # False
print(np.any(a == b)) # True
print(np.all([1.0, np.nan])) # True
print(np.any([1.0, np.nan])) # True
a = np.eye(3)
print(np.all(a, axis=0)) # [False False False]
print(np.any(a, axis=0)) # [ True True True]
数组内容
numpy.isnan
numpy.isnan(x, *args, **kwargs) Test element-wise for NaN and return result as a boolean array.
a=np.array([1,2,np.nan])
print(np.isnan(a))
#[False False True]
逻辑运算
numpy.logical_not
print(np.logical_not(3))
# False
print(np.logical_not([True, False, 0, 1]))
# [False True True False]
x = np.arange(5)
print(np.logical_not(x < 3))
# [False False False True True]
numpy.logical_and
print(np.logical_and(True, False))
# False
print(np.logical_and([True, False], [True, False]))
# [ True False]
numpy.logical_or
print(np.logical_or(True, False))
# True
print(np.logical_or([True, False], [False, False]))
# [ True False]
numpy.logical_xor
print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False True True False]
对照
numpy.greater
numpy.greater_equal
numpy.equal
numpy.not_equal
numpy.less
numpy.less_equal