python运算学习之Numpy ------ 算术运算
数组间的加减乘除:
1 a = np.array([1, 2, 3, 4])
2 b = np.array([4, 2, 8, 4])
3 print(a + b, '\n', a - b, '\n', a * b, '\n', a / b)
4 Out[1]:
5 [ 5 4 11 8]
6 [-3 0 -5 0]
7 [ 4 4 24 16]
8 [0.25 1. 0.375 1. ]
|
数组与数值相加减乘除,实际是对每个元素都进行加减乘除:
print(a + 2, '\n', a - 2, '\n', a * 2, '\n', a / 2)
Out[2]:
[3 4 5 6]
[-1 0 1 2]
[2 4 6 8]
[0.5 1. 1.5 2. ]
矩阵间的加减乘除:
1 A = np.arange(0, 16).reshape((4, 4))
2 B = np.ones_like(A)
3 print(A*B) # 对应元素相乘,加减乘除一样
4 # 矩阵运算
5 print(np.dot(A, B)) # 矩阵乘法
6 print(A.dot(B)) # 矩阵乘法
7 Out[3]:
8 [[ 0 1 2 3]
9 [ 4 5 6 7]
10 [ 8 9 10 11]
11 [12 13 14 15]]
12 [[ 6 6 6 6]
13 [22 22 22 22]
14 [38 38 38 38]
15 [54 54 54 54]]
16 [[ 6 6 6 6]
17 [22 22 22 22]
18 [38 38 38 38]
19 [54 54 54 54]]
|
数组的自增自减:
1 a = np.array(
2 [
3 [1, 2, 3, 4],
4 [2, 6, 9, 2]
5 ]
6 )
7 a += 1
8 print(a)
9 a *= 2
10 print(a)
11 a -= 1
12 print(a)
13 a = a / 2 # a /= 2是错的
14 print(a)
15 Out[4]:
16 [[ 2 3 4 5]
17 [ 3 7 10 3]]
18 [[ 4 6 8 10]
19 [ 6 14 20 6]]
20 [[ 3 5 7 9]
21 [ 5 13 19 5]]
22 [[1.5 2.5 3.5 4.5]
23 [2.5 6.5 9.5 2.5]]
|
通用函数: 是一种对ndararry中的数据执行元素级的运算函数
一元函数:abs, sqrt,exp,log,log10,log2,log1p,ceil(向上取整),floor(向下取整),rint(四舍五入)
round(四舍五入),trunc,modf,isnan,isinf,cos,sin,tan,logical_not
a = np.array([1.3256, 2.6971358])
print(np.rint(a))
print(np.round(a))
print(np.modf(a)) # np.modf()将数组元素的整数和小数部分以两个不同的数组返回
b = np.array([np.nan, 0, 5, np.inf, 1]) #
print(np.isnan(b))
print(np.isinf(b))
print(np.logical_not(b)) # 计算not b中元素的真值
Out[5]:
[1. 3.]
[1. 3.]
(array([0.3256 , 0.6971358]), array([1., 2.]))
[ True False False False False]
[False False False True False]
[False True False False False]
|
二元函数:add,substract,multiply,divide,power,mod,maximum,mininum,fmin,
fmax(将忽略nan),copysign(复制数组二中的符号给数组一),greater、greater_equal、
less、less_equal、equal、not_equal,logical_and、logical_or、logical_xor
1 a = np.array([1, 2, 3, 4.5])
2 b = np.array([9, 8, 20, 52])
3 print(np.add(a, b)) # 对应元素相加
4 print(np.subtract(a, b)) # 对应元素相减
5 print(np.multiply(a, b)) # 对应元素相乘
6 print(np.divide(a, b)) # 对应元素相除, 相当于 a / b
7 c = np.array([1, 2, 3, 4])
8 d = np.array([1, 2, 3, 4])
9 print(np.power(c, d)) # 对应元素的幂指函数
10 print(np.mod(b, d)) # 对应元素的幂指函数
11 print(np.copysign(a, [-1, 2, -5, 3]), "标识: np.copysign")
12 print(np.greater(a, d)) # 比较对应元素的大小
13 print(np.logical_and([1, 0.3], [0, 0.5])) # 逻辑与
14 Out[6]:
15 [10. 10. 23. 56.5]
16 [ -8. -6. -17. -47.5]
17 [ 9. 16. 60. 234.]
18 [0.11111111 0.25 0.15 0.08653846]
19 [ 1 4 27 256]
20 [0 0 2 0]
21 [-1. 2. -3. 4.5] 标识: np.copysign
22 [False False False True]
23 [False True]
|
数组的排序:
1 a = np.random.rand(12).reshape(3, 4)
2 print(a)
3 print(np.sort(a)) # sort(a, axis=-1, kind='quicksort', order=None)默认按行排序
4 print(np.sort(a, axis=0)) # axis=1是按行
5 print(np.argsort(a, axis=0)) # 对a数组排序后对应之前的索引
6 Out[7]:
7 [[0.56745389 0.56311812 0.3090698 0.25593579]
8 [0.97982438 0.56580598 0.74484888 0.32317628]
9 [0.3334283 0.53780294 0.44998125 0.67227849]]
10 [[0.25593579 0.3090698 0.56311812 0.56745389]
11 [0.32317628 0.56580598 0.74484888 0.97982438]
12 [0.3334283 0.44998125 0.53780294 0.67227849]]
13 [[0.3334283 0.53780294 0.3090698 0.25593579]
14 [0.56745389 0.56311812 0.44998125 0.32317628]
15 [0.97982438 0.56580598 0.74484888 0.67227849]]
16 [[2 2 0 0]
17 [0 0 2 1]
18 [1 1 1 2]]
|
数组的聚合函数:
1 a = np.array([1.3, 2.5, 6., 8.5])
2 print(a.sum())
3 print(a.min())
4 print(a.std())
5 print(a.mean())
6 Out:
7 18.3
8 1.3
9 2.849012986983387
10 4.575
|
清澈的爱,只为中国