Arithmetic operations on arrays are all element-based operations:
a = array([20, 30, 40, 50])
b = arange(4)
print("a: ", a, "\nb: ", b)
c = a - b # 元素相减, 元素数目应一致
print("c = a - b: ", c)
print("b**2: ", b**2)
print("10*sin(a): ", 10*sin(a))
print("a < 35: ", a < 35) # 元素变成bool值
a: [20 30 40 50]
b: [0 1 2 3]
c = a - b: [20 29 38 47]
b**2: [0 1 4 9]
10*sin(a): [ 9.12945251 -9.88031624 7.4511316 -2.62374854]
a < 35: [ True True False False]
The multiplication operation* is also calculated element-wise, and matrix multiplication can be implemented using the dot function
a = arange(0, 12, 2).reshape(3, 2)
b = arange(1, 13, 2).reshape(3, 2)
print("a: ", a)
print("b: ", b)
print("a*b: ", a*b)
a: [[ 0 2]
[ 4 6]
[ 8 10]]
b: [[ 1 3]
[ 5 7]
[ 9 11]]
a*b: [[ 0 6]
[ 20 42]
[ 72 110]]
Some operators like += and *= are used to modify an existing array without creating a new one.
a = ones((2, 3), dtype=int)
b = random.random((2, 3)) # randmom 返回0-1的随机数
print("a: ", a)
print("b: ", b)
a *= 3
print("a: ", a)
b += a # a 被强制类型转换位 float, a += b则不行
print("b: ", b)
a: [[1 1 1]
[1 1 1]]
b: [[ 0.4824609 0.07585688 0.01264149]
[ 0.8571839 0.07949205 0.04539534]]
a: [[3 3 3]
[3 3 3]]
b: [[ 3.4824609 3.07585688 3.01264149]
[ 3.8571839 3.07949205 3.04539534]]
When operating on arrays of different types, the resulting array is known (this behavior is called upcast).
a = ones(3, dtype=int)
b = linspace(0, pi, 3) # 在指定的间隔内返回均匀间隔的数字。
print("a: ", a.dtype)
print("b: ", b.dtype)
c = a + b
print("c: ", c.dtype)
print("c: ", c)
print(c*1j) # 转为虚数
d = exp(c*1j)
print("d: ", d.dtype)
print("d: ", d)
a = random.random((2, 3))
print("a: ", a)
print(a.sum())
print(a.min())
print(a.max())
a: int32
b: float64
c: float64
c: [ 1. 2.57079633 4.14159265]
[ 0.+1.j 0.+2.57079633j 0.+4.14159265j]
d: complex128
d: [ 0.54030231+0.84147098j -0.84147098+0.54030231j -0.54030231-0.84147098j]
a: [[ 0.26592259 0.52108908 0.60666573]
[ 0.84378524 0.52214234 0.17239377]]
2.93199873833
0.172393770257
0.843785235592
Specify the axis parameter to apply the operation to the axis specified by the array
b = arange(12).reshape(3, 4)
print("b: ", b)
print("所有数据加到0轴(横)上:", b.sum(axis=0))
print("1(纵)轴上的最小值: ", b.min(axis=1))
print("加到1(纵)轴:", b.cumsum(axis=1)) # 计算轴向元素累加和,返回由中间结果组成的数组
b: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
所有数据加到0轴(横)上: [12 15 18 21]
1(纵)轴上的最小值: [0 4 8]
加到1(纵)轴: [[ 0 1 3 6]
[ 4 9 15 22]
[ 8 17 27 38]]
C umsum(): Calculate the cumulative sum of the axial elements, and return an array composed of intermediate results, focusing on "an array composed of intermediate results "
Look at the code below, define a 2*2*3 array, so its shape is 2, 2, 3, and the indices are 0, 1, 2 respectively
shape | index |
2 | 0 |
2 | 1 |
3 | 2 |
arr = array([[[1,2,3],[8,9,12]],[[1,2,4],[2,4,5]]])
print(arr.cumsum(0))
print(arr.cumsum(1))
print(arr.cumsum(2))
<strong>输出结果:</strong>
#cumsum(0)
[[[ 1 2 3]
[ 8 9 12]]
[[ 2 4 7]
[10 13 17]]]
#cumsum(1)
[[[ 1 2 3]
[ 9 11 15]]
[[ 1 2 4]
[ 3 6 9]]]
#cumsum(2)
[[[ 1 3 6]
[ 8 17 29]]
[[ 1 3 7]
[ 2 6 11]]]