广播的规则有三个:
如果两个数组的维度数dim不相同,那么小维度数组的形状将会在左边补1。
如果shape维度不匹配,但是有维度是1,那么可以扩展维度是1的维度匹配另一个数组;
如果shape维度不匹配,但是没有任何一个维度是1,则匹配引发错误;
示例:
x = np.arange(4)
y = np.ones((3, 4))
print(x.shape) # (4,)
print(y.shape) # (3, 4)
print((x + y).shape) # (3, 4)
print(x + y)
# [[1. 2. 3. 4.]
# [1. 2. 3. 4.]
# [1. 2. 3. 4.]]
shape维度不匹配,但是有维度是1:
x = np.arange(4).reshape(4, 1)
y = np.ones(5)
print(x.shape) # (4, 1)
print(y.shape) # (5,)
print((x + y).shape) # (4, 5)
print(x + y)
# [[1. 1. 1. 1. 1.]
# [2. 2. 2. 2. 2.]
# [3. 3. 3. 3. 3.]
# [4. 4. 4. 4. 4.]]
x = np.array([0.0, 10.0, 20.0, 30.0])
y = np.array([1.0, 2.0, 3.0])
z = x[:, np.newaxis] + y
print(z)
# [[ 1. 2. 3.]
# [11. 12. 13.]
# [21. 22. 23.]
# [31. 32. 33.]]
匹配失败:
x = np.arange(4)
y = np.ones(5)
print(x.shape) # (4,)
print(y.shape) # (5,)
print(x + y)
# ValueError: operands could not be broadcast together with shapes (4,) (5,)
算符为 元素级。也就是说,它们只用于位置相同的元素之间,所得到的运算结果组成一个新的数组。
Add arguments element-wise.
Subtract arguments element-wise
Multiply arguments element-wise.
Returns a true division of the inputs, element-wise.
Return the largest integer smaller or equal to the division of the inputs.
First array elements raised to powers from second array, element-wise.
Return the non-negative square-root of an array, element-wise.
Return the element-wise square of the input.
矩阵与数字
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x + 1
print(y)
print(np.add(x, 1))
# [2 3 4 5 6 7 8 9]
y = x - 1
print(y)
print(np.subtract(x, 1))
# [0 1 2 3 4 5 6 7]
y = x * 2
print(y)
print(np.multiply(x, 2))
# [ 2 4 6 8 10 12 14 16]
y = x / 2
print(y)
print(np.divide(x, 2))
# [0.5 1. 1.5 2. 2.5 3. 3.5 4. ]
y = x // 2
print(y)
print(np.floor_divide(x, 2))
# [0 1 1 2 2 3 3 4]
y = x ** 2
print(y)
print(np.power(x, 2))
# [ 1 4 9 16 25 36 49 64]
非相同形状矩阵运算(广播)
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.arange(1, 6)
print(y)
# [1 2 3 4 5]
z = x + y
print(z)
print(np.add(x, y))
# [[12 14 16 18 20]
# [17 19 21 23 25]
# [22 24 26 28 30]
# [27 29 31 33 35]
# [32 34 36 38 40]]
z = x - y
print(z)
print(np.subtract(x, y))
# [[10 10 10 10 10]
# [15 15 15 15 15]
# [20 20 20 20 20]
# [25 25 25 25 25]
# [30 30 30 30 30]]
z = x * y
print(z)
print(np.multiply(x, y))
# [[ 11 24 39 56 75]
# [ 16 34 54 76 100]
# [ 21 44 69 96 125]
# [ 26 54 84 116 150]
# [ 31 64 99 136 175]]
z = x / y
print(z)
print(np.divide(x, y))
# [[11. 6. 4.33333333 3.5 3. ]
# [16. 8.5 6. 4.75 4. ]
# [21. 11. 7.66666667 6. 5. ]
# [26. 13.5 9.33333333 7.25 6. ]
# [31. 16. 11. 8.5 7. ]]
z = x // y
print(z)
print(np.floor_divide(x, y))
# [[11 6 4 3 3]
# [16 8 6 4 4]
# [21 11 7 6 5]
# [26 13 9 7 6]
# [31 16 11 8 7]]
z = x ** np.full([1, 5], 2)
print(z)
print(np.power(x, np.full([5, 5], 2)))
# [[ 121 144 169 196 225]
# [ 256 289 324 361 400]
# [ 441 484 529 576 625]
# [ 676 729 784 841 900]
# [ 961 1024 1089 1156 1225]]
相同形状代码运算
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.arange(1, 26).reshape([5, 5])
print(y)
# [[ 1 2 3 4 5]
# [ 6 7 8 9 10]
# [11 12 13 14 15]
# [16 17 18 19 20]
# [21 22 23 24 25]]
z = x + y
print(z)
print(np.add(x, y))
# [[12 14 16 18 20]
# [22 24 26 28 30]
# [32 34 36 38 40]
# [42 44 46 48 50]
# [52 54 56 58 60]]
z = x - y
print(z)
print(np.subtract(x, y))
# [[10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]
# [10 10 10 10 10]]
z = x * y
print(z)
print(np.multiply(x, y))
# [[ 11 24 39 56 75]
# [ 96 119 144 171 200]
# [231 264 299 336 375]
# [416 459 504 551 600]
# [651 704 759 816 875]]
z = x / y
print(z)
print(np.divide(x, y))
# [[11. 6. 4.33333333 3.5 3. ]
# [ 2.66666667 2.42857143 2.25 2.11111111 2. ]
# [ 1.90909091 1.83333333 1.76923077 1.71428571 1.66666667]
# [ 1.625 1.58823529 1.55555556 1.52631579 1.5 ]
# [ 1.47619048 1.45454545 1.43478261 1.41666667 1.4 ]]
z = x // y
print(z)
print(np.floor_divide(x, y))
# [[11 6 4 3 3]
# [ 2 2 2 2 2]
# [ 1 1 1 1 1]
# [ 1 1 1 1 1]
# [ 1 1 1 1 1]]
z = x ** np.full([5, 5], 2)
print(z)
print(np.power(x, np.full([5, 5], 2)))
# [[ 121 144 169 196 225]
# [ 256 289 324 361 400]
# [ 441 484 529 576 625]
# [ 676 729 784 841 900]
# [ 961 1024 1089 1156 1225]]
对每个元素开平方
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]
Trigonometric sine, element-wise.
Cosine element-wise.
Compute tangent element-wise.
Inverse sine, element-wise.
Trigonometric inverse cosine, element-wise.
Trigonometric inverse tangent, element-wise.
x = np.linspace(start=0, stop=np.pi / 2, num=10)
print(x)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.sin(x)
print(y)
# [0. 0.17364818 0.34202014 0.5 0.64278761 0.76604444
# 0.8660254 0.93969262 0.98480775 1. ]
z = np.arcsin(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.cos(x)
print(y)
# [1.00000000e+00 9.84807753e-01 9.39692621e-01 8.66025404e-01
# 7.66044443e-01 6.42787610e-01 5.00000000e-01 3.42020143e-01
# 1.73648178e-01 6.12323400e-17]
z = np.arccos(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
y = np.tan(x)
print(y)
# [0.00000000e+00 1.76326981e-01 3.63970234e-01 5.77350269e-01
# 8.39099631e-01 1.19175359e+00 1.73205081e+00 2.74747742e+00
# 5.67128182e+00 1.63312394e+16]
z = np.arctan(y)
print(z)
# [0. 0.17453293 0.34906585 0.52359878 0.6981317 0.87266463
# 1.04719755 1.22173048 1.3962634 1.57079633]
Calculate the exponential of all elements in the input array.
Natural logarithm, element-wise.
Calculate 2**p for all p in the input array.
Base-2 logarithm of x.
Return the base 10 logarithm of the input array, element-wise.
x = np.arange(1, 5)
print(x)
# [1 2 3 4]
y = np.exp(x)
print(y)
# [ 2.71828183 7.3890561 20.08553692 54.59815003]
z = np.log(y)
print(z)
# [1. 2. 3. 4.]
Sum of array elements over a given axis.
返回给定轴上的数组元素的总和。
通过不同的 axis,numpy 会沿着不同的方向进行操作:如果不设置,那么对所有的元素操作;如果axis=0,则沿着纵轴进行操作;axis=1,则沿着横轴进行操作;axis=i,则 numpy 沿着第i个下标变化的方向进行操作。
示例:
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]
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]]
Return the product of array elements over a given axis.
参数用法与numpy.sum()相同
示例:
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]
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]]
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.
Calculate the n-th discrete difference along the given axis
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]]
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]]
Return the ceiling 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.]]
Return the floor of the input, element-wise
y = np.floor(x)
print(y)
# [[0. 1. 4.]
# [7. 5. 2.]
# [8. 8. 5.]]
Clip (limit) the values in an array
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]]
abs是对absolute的简写形势
x = np.arange(-5, 5)
print(x)
# [-5 -4 -3 -2 -1 0 1 2 3 4]
y = np.abs(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]
y = np.absolute(x)
print(y)
# [5 4 3 2 1 0 1 2 3 4]
Returns an element-wise indication of the sign of a number.
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]
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]
Compute the truth value of x1 AND x2 element-wise.
Compute the truth value of x1 OR x2 element-wise.
Compute the truth value of NOT x element-wise.
Compute the truth value of x1 XOR x2, element-wise.
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]
【例】计算x1 AND x2元素的真值。
print(np.logical_and(True, False))
# False
print(np.logical_and([True, False], [True, False]))
# [ True False]
print(np.logical_and(x > 1, x < 4))
# [False False True True False]
【例】逐元素计算x1 OR x2的真值。
print(np.logical_or(True, False))
# True
print(np.logical_or([True, False], [False, False]))
# [ True False]
print(np.logical_or(x < 1, x > 3))
# [ True False False False True]
【例】计算x1 XOR x2的真值,按元素计算。
print(np.logical_xor(True, False))
# True
print(np.logical_xor([True, True, False, False], [True, False, True, False]))
# [False True True False]
print(np.logical_xor(x < 1, x > 3))
# [ True False False False True]
print(np.logical_xor(0, np.eye(2)))
# [[ True False]
# [False True]]
Return the truth value of (x1 > x2) element-wise.
Return the truth value of (x1 >= x2) element-wise.
Return (x1 == x2) element-wise.
Return (x1 != x2) element-wise.
Return the truth value of (x1 < x2) element-wise.
Return the truth value of (x1 =< x2) element-wise.
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = x > 2
print(y)
print(np.greater(x, 2))
# [False False True True True True True True]
y = x >= 2
print(y)
print(np.greater_equal(x, 2))
# [False True True True True True True True]
y = x == 2
print(y)
print(np.equal(x, 2))
# [False True False False False False False False]
y = x != 2
print(y)
print(np.not_equal(x, 2))
# [ True False True True True True True True]
y = x < 2
print(y)
print(np.less(x, 2))
# [ True False False False False False False False]
y = x <= 2
print(y)
print(np.less_equal(x, 2))
# [ True True False False False False False False]
比较时可以存在广播
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]])
np.random.seed(20200611)
y = np.random.randint(10, 50, 5)
print(y)
# [32 37 30 24 10]
z = x > y
print(z)
print(np.greater(x, y))
# [[False False False False True]
# [False False False False True]
# [False False False False True]
# [False False False True True]
# [False False True True True]]
z = x >= y
print(z)
print(np.greater_equal(x, y))
# [[False False False False True]
# [False False False False True]
# [False False False True True]
# [False False False True True]
# [False False True True True]]
z = x == y
print(z)
print(np.equal(x, y))
# [[False False False False False]
# [False False False False False]
# [False False False True False]
# [False False False False False]
# [False False False False False]]
z = x != y
print(z)
print(np.not_equal(x, y))
# [[ True True True True True]
# [ True True True True True]
# [ True True True False True]
# [ True True True True True]
# [ True True True True True]]
z = x < y
print(z)
print(np.less(x, y))
# [[ True True True True False]
# [ True True True True False]
# [ True True True False False]
# [ True True True False False]
# [ True True False False False]]
z = x <= y
print(z)
print(np.less_equal(x, y))
# [[ True True True True False]
# [ True True True True False]
# [ True True True True False]
# [ True True True False False]
# [ True True False False False]]