Numpy入门试手

引用

1
import numpy as np

数组

np.array()

np.array().reshape()

1
2
3
4
5
6
7
# 创建array数组-----------------
arr1 = np.array([[[10], [11], [12]], [[20], [21], [22]]])
arr1_res = arr1.rehape(3, 2, 1)
print(arr1.shape) # 两个三元素数组的数组(2, 3, 1)
print(arr1.ndim) # 拥有几维3
print(arr1.size) # 元素总个数2 * 3 * 1 = 6
print(arr1.dtype) # 数据类型int32

序列

np.arrage()

np.arrage().reshape()

np.linspace()

1
2
3
4
5
6
7
8
# 创建arange序列-----------------
ara1 = np.arange(10) # 0~n,步长1的序列
ara2 = np.arange(5, 15) # 5~15,步长1的序列
ara3 = np.arange(5, 10, 0.5) # 5~10,步长0.5的序列
ara1_res = ara1.reshape(2, 5)

# 创建linspace线性等分序列-----------------
line = np.linspace(1, 10, 20) # 1~10,平分20份,等差数列

数值转换

.astype()

1
2
3
ara = np.arange(10)
print(ara.dtype)
print(ara.astype('uint8').dtype)

特值数组

np.ones()

np.ones_like()

np.zeros()

np.zeros_like()

np.empty()

np.empty_like()

np.full()

np.full_like()

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 创建ones()-----------------
# 全是1.的/长为10的/一维数组,类型是float64
one1 = np.ones(10)
# 全是1.的/5行8列/二维数组,类型是float64
one2 = np.ones((5, 8))
# 全是1的/5行10列/二维数组,类型是int32
one3 = np.ones((5, 10), dtype=np.int32)

# 创建ones_like()-----------------
temp1 = np.array([[10, 11, 12], [20, 21, 22]])
# 和输入数组同样大小的全是1的数组
oneslike1 = np.ones_like(temp1, dtype=np.int32)

# 创建zeros()-----------------
zeros1 = np.zeros(10)
zeros2 = np.zeros((5, 8))
zeros3 = np.zeros((5, 10), dtype=np.int32)

# 创建zeros_like()-----------------
temp2 = np.array([[10, 11, 12], [20, 21, 22]])
zeroslike1 = np.zeros_like(temp2, dtype=np.int32)

# 创建empty()-----------------
# 未初始化数字,都是随机的
empty1 = np.empty(10)
empty2 = np.empty((5, 8))
empty3 = np.empty((5, 10), dtype=np.int32)

# 创建empty_like()-----------------
temp3 = np.array([[10, 11, 12], [20, 21, 22]])
emptylike1 = np.empty_like(temp3, dtype=np.int32)

# 创建full()-----------------
full1 = np.full(10, fill_value=11)
full2 = np.full((5, 8), fill_value=11)
full3 = np.full((5, 10), fill_value=11, dtype=np.int32)

# 创建full_like()-----------------
temp4 = np.array([[10, 11, 12], [20, 21, 22]])
fulllike1 = np.full_like(temp4, fill_value=11, dtype=np.int32)

快速计算

.copy()

1
2
3
4
5
6
7
# 形状相同的数组的对应元素的快速计算
arr1 = np.arange(10).reshape(2, 5)
arr1 = arr1 + 5 # arr1里每个数都加上5
arr1 = np.sin(arr1) # arr1里每个数都进行计算
arr2 = arr1.copy() # 创建实例
arr2 = arr2 / 5
arr2 = arr1 - arr2

精度

np.round()

np.around()

np.rint()

np.ceil()

np.floor()

np.modf()

np.trunc()

np.fix()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
lst1 = np.random.rand(4, 5)
# np.round()四舍五入与精度
print(np.round(lst1, decimals=2))
# np.around()四舍五入与精度
print(np.around(lst1, decimals=2))
# np.rint()四舍五入至整数
print(np.rint(lst1))
# np.ceil()五入
print(np.ceil(lst1))
# np.floor()四舍
print(np.floor(lst1))
# np.modf()小数整数分离
print(np.modf(lst1))
# np.trunc()去除小数
print(np.trunc(lst1))
# np.fix()近0整数
print(np.fix(lst1))

索引

.argsort()

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 索引
arr2 = np.arange(20).reshape(2, 2, 5)
# [
# [ [ 0 1 2 3 4]
# [ 5 6 7 8 9] ]
#
# [ [10 11 12 13 14]
# [15 16 17 18 19] ]
# ]
print(arr2)
print(arr2[0]) # [[0 1 2 3 4] [5 6 7 8 9]]
print(arr2[0][1]) # [5 6 7 8 9]
print(arr2[0, 1]) # [5 6 7 8 9]
print(arr2[0][1][3]) # 8
print(arr2[0, 1, 3]) # 8
print(arr2[:, 1, :3]) # [[ 5 6 7] [15 16 17]]
print(arr2[:, 1, -3::-1]) # [[ 7 6 5] [17 16 15]]
# --------------------------------
# 花式索引
arr3 = np.arange(20)
print(arr3[[5, 10, 15]]) # [ 5 10 15]
print(arr3[np.array([[3, 4], [5, 6]])]) # [[3 4] [5 6]]
arr4 = np.arange(20).reshape(2, 2, 5)
print(arr4[[0], [1]]) # [[5 6 7 8 9]]
# 获取最大前N数
# argsort()
arr5 = np.random.randint(1, 101, [5])
print(arr5)
print(arr5.argsort()[-3:])
print(arr5[arr5.argsort()[-3:]])
# --------------------------------
# 比较索引(布尔索引)
arr6 = np.arange(5)
print(arr6 < 3) # [ True True True False False]
print(arr6[arr6 < 3]) # [0 1 2]
arr7 = np.arange(10).reshape(2, 5)
print(arr7[arr7[:, 3] > 5]) # [[5 6 7 8 9]]
arr7[arr7 >= 5] = 6
print(arr7) # [[0 1 2 3 4] [6 6 6 6 6]]
# --------------------------------
# 逻辑索引(布尔逻辑索引)
arr8 = np.arange(10).reshape(2, 5)
print(arr8[(arr8 < 3) | (arr8 > 6)]) # [0 1 2 7 8 9]
print(arr8[(arr8 >= 3) & (arr8 <= 6)]) # [3 4 5 6]
print(arr8[arr8 != 5]) # [0 1 2 3 4 6 7 8 9]

随机

np.random.seed()

np.random.rand()

np.random.randn()

np.random.randint()

np.random.random()

np.random.choise()

np.random.shuffle()

np.random.permutation()

np.random.normal()

np.random.uniform()

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# random随机模块
# seed()种子随机----------------------
# np.random.seed(10) # 设置种子

# rand()0~1随机----------------------

print(np.random.rand(5)) # 生成5个0~1随机数
print(np.random.rand(2, 3)) # 生成2行3列的0~1随机数

# randn()标准正态随机----------------------
print(np.random.randn(5)) # 生成5个标准正态随机数
print(np.random.randn(2, 3)) # 生成2行3列的标准正态随机数

# randint()整数随机----------------------
print(np.random.randint(5)) # 生成1个0~5整数随机数
print(np.random.randint(2, 8)) # 生成1个2~8整数随机数
print(np.random.randint(2, 8, (3,))) # 生成3个2~8整数随机数
print(np.random.randint(2, 8, (3, 4))) # 生成3行4列的2~8整数随机数

# random()0~1随机----------------------
print(np.random.random(5)) # 生成5个0~1随机数
print(np.random.random((2, 3))) # 生成2行3列的0~1随机数

# choise()数组随机选择----------------------
print(np.random.choice(5, 3)) # 在0~5之间选3个数可重复
print(np.random.choice(5, (2, 3))) # 在0~5之间选2行3列数可重复
print(np.random.choice([10, 11, 12], (2, 3))) # 在数组里选2行3列数可重复

# shuffle()数组随机排列无返回值----------------------
arr1 = np.arange(10)
np.random.shuffle(arr1)
print(arr1)
arr2 = np.arange(20).reshape(4, 5)
np.random.shuffle(arr2)
print(arr2)

# permutation()数组随机排列有返回值----------------------
print(np.random.permutation(10))
arr4 = np.arange(20).reshape(4, 5)
print(np.random.permutation(arr4))

# normal()平均值与方差的高斯分布----------------------
print(np.random.normal(0, 1, 10)) # 均值0方差1的10个离散数

# uniform()均匀数----------------------
print(np.random.uniform(1, 10, 5)) # 在1~10之间生成5个均匀分布离散数

统计

np.sum()

np.prod()

np.cumsum()

np.cumprod()

np.min()

np.max()

np.percentile()

np.quantile()

np.median()

np.average()

np.mean()

np.std()

np.var()

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# axis=0计算列, axis=1计算行, axis=2以此类推
arr1 = np.arange(1, 13).reshape(3, 4)
print(arr1)
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]

# sum()总和----------------------
print(np.sum(arr1)) # 78
print(np.sum(arr1, axis=0)) # [15 18 21 24]
print(np.sum(arr1, axis=1)) # [10 26 42]

# prod()总积----------------------
print(np.prod(arr1)) # 479001600
print(np.prod(arr1, axis=0)) # [ 45 120 231 384]
print(np.prod(arr1, axis=1)) # [ 24 1680 11880]

# cumsum()累和----------------------
print(np.cumsum(arr1))
# [ 1 3 6 10 15 21 28 36 45 55 66 78]
print(np.cumsum(arr1, axis=0))
# [[ 1 2 3 4] [ 6 8 10 12] [15 18 21 24]]
print(np.cumsum(arr1, axis=1))
# [[ 1 3 6 10] [ 5 11 18 26] [ 9 19 30 42]]

# cumprod()累积----------------------
print(np.cumprod(arr1))
# [1 2 6 24 120 720 5040 40320 362880 3628800 39916800 479001600]
print(np.cumprod(arr1, axis=0))
# [[ 1 2 3 4] [ 5 12 21 32] [ 45 120 231 384]]
print(np.cumprod(arr1, axis=1))
# [[1 2 6 24] [5 30 210 1680] [9 90 990 11880]]

# min()最小数----------------------
print(np.min(arr1)) # 1
print(np.min(arr1, axis=0)) # [1 2 3 4]
print(np.min(arr1, axis=1)) # [1 5 9]

# max()最大数----------------------
print(np.max(arr1)) # 12
print(np.max(arr1, axis=0)) # [ 9 10 11 12]
print(np.max(arr1, axis=1)) # [ 4 8 12]

# percentile()百分位数----------------------
print(np.percentile(arr1, 50)) # 6.5
print(np.percentile(arr1, [25, 50, 75])) # [3.75 6.5 9.25]
print(np.percentile(arr1, 50, axis=0)) # [5. 6. 7. 8.]
print(np.percentile(arr1, [25, 50, 75], axis=0))
# [[ 3. 4. 5. 6.] [ 5. 6. 7. 8.] [ 7. 8. 9. 10.]]
print(np.percentile(arr1, 50, axis=1)) # [ 2.5 6.5 10.5]
print(np.percentile(arr1, [25, 50, 75], axis=1))
# [[ 1.75 5.75 9.75] [ 2.5 6.5 10.5 ] [ 3.25 7.25 11.25]]

# quantile()一分位数----------------------
print(np.quantile(arr1, 0.5)) # 6.5
print(np.quantile(arr1, [0.25, 0.5, 0.75])) # [3.75 6.5 9.25]
print(np.quantile(arr1, 0.5, axis=0)) # [5. 6. 7. 8.]
print(np.quantile(arr1, [0.25, 0.5, 0.75], axis=0))
# [[ 3. 4. 5. 6.] [ 5. 6. 7. 8.] [ 7. 8. 9. 10.]]
print(np.quantile(arr1, 0.5, axis=1)) # [ 2.5 6.5 10.5]
print(np.quantile(arr1, [0.25, 0.5, 0.75], axis=1))
# [[ 1.75 5.75 9.75] [ 2.5 6.5 10.5 ] [ 3.25 7.25 11.25]]

# median()中位数----------------------
print(np.median(arr1)) # 6.5
print(np.median(arr1, axis=0)) # [5. 6. 7. 8.]
print(np.median(arr1, axis=1)) # [ 2.5 6.5 10.5]

# average()加权平均数----------------------
# 每个元素与对应的权数相乘之和 除以 总权数之和
# 权数可以是任何数,不受负数影响
arr2 = np.arange(10, 20)
weight = np.arange(1, 11)
print(weight) # [ 1 2 3 4 5 6 7 8 9 10]
print(np.average(arr2, weights=weight)) # 16.0
#
arr3 = np.arange(10, 20).reshape(2, 5)
weight2 = np.arange(1, 11).reshape(2, 5)
print(weight2) # [[ 1 2 3 4 5] [ 6 7 8 9 10]]
print(np.average(arr3, weights=weight2, axis=0))
# [14.28571429 14.88888889 15.63636364 16.46153846 17.33333333]
#
arr3 = np.arange(10, 20).reshape(2, 5)
weight2 = np.arange(1, 11).reshape(2, 5)
print(weight2) # [[ 1 2 3 4 5] [ 6 7 8 9 10]]
print(np.average(arr3, weights=weight2, axis=1)) # [12.66666667 17.25]

# mean()平均数----------------------
print(np.mean(arr1)) # 6.5
print(np.mean(arr1, axis=0)) # [5. 6. 7. 8.]
print(np.mean(arr1, axis=1)) # [ 2.5 6.5 10.5]

# std()标准差----------------------
# 每个元素与平均数之差的平方之和再开根
print(np.std(arr1)) # 3.452052529534663
print(np.std(arr1, axis=0)) # [3.26598632 3.26598632 3.26598632 3.26598632]
print(np.std(arr1, axis=1)) # [1.11803399 1.11803399 1.11803399]

# var()方差----------------------
# 每个元素与平均数之差的平方之和
print(np.var(arr1)) # 11.916666666666666
print(np.var(arr1, axis=0)) # [10.66666667 10.66666667 10.66666667 10.66666667]
print(np.var(arr1, axis=1)) # [1.25 1.25 1.25]

维度转换

np.newaxis

np.expand_dim()

np.concatenate()

np.vstack()

np.row_stack()

np.hstack()

np.column_stack()

.flatten()

np.transpose()

np.split()

np.array_split()

np.vsplit()

np.hsplit()

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 升维
arr2 = np.arange(10)
print(arr2)
# np.newaxis
print(arr2[np.newaxis, :]) # 1行10列
print(arr2[:, np.newaxis]) # 10行1列
# np.expand_dim()
print(np.expand_dims(arr2, axis=0)) # 1行10列
print(np.expand_dims(arr2, axis=1)) # 10行1列
# np.reshape()
print(arr2.reshape(1, 10)) # 1行10列
print(arr2.reshape(10, 1)) # 10行1列
print(arr2.reshape(1, -1)) # 1行?列
print(arr2.reshape(-1, 1)) # ?行1列
# --------------------------------------
# 降维
arr1 = np.arange(6).reshape(2, 3)
arr2 = np.arange(8).reshape(4, 2)
arr3 = np.random.randint(10, 21, (4, 3))
print(arr1)
# [[0 1 2]
# [3 4 5]]
print(arr2)
# [[0 1]
# [2 3]
# [4 5]
# [6 7]]
print(arr3)
# [[15 12 11]
# [14 20 13]
# [12 16 14]
# [18 13 18]]
print('--------------------------')
# np.concatenate() # 指定轴合并
print(np.concatenate([arr1, arr3]))
print(np.concatenate([arr1, arr3, arr1]))
# [[ 0 1 2]
# [ 3 4 5]
# [15 12 11]
# [14 20 13]
# [12 16 14]
# [18 13 18]]
# np.vstack() # 垂直行合并
print(np.vstack([arr1, arr3]))
# np.row_stack() # 垂直行合并
print(np.row_stack([arr1, arr3]))
# [[ 0 1 2]
# [ 3 4 5]
# [15 12 11]
# [14 20 13]
# [12 16 14]
# [18 13 18]]
# np.hstack() # 水平列合并
print(np.hstack([arr2, arr3]))
# np.column_stack() # 水平列合并
print(np.column_stack([arr2, arr3]))
# [[ 0 1 15 12 11]
# [ 2 3 14 20 13]
# [ 4 5 12 16 14]
# [ 6 7 18 13 18]]
# np.flatten() # 至一维
print(arr1.flatten())
# np.transpose()/.T # 行列倒转
print(np.transpose(arr2))
print(arr2.T)
# np.split() # 分割
print(np.split(arr2, 2, axis=0))
# np.array_split() # 不等分割
print(np.array_split(arr2, 2, axis=0))
# np.vsplit() # 横向分割
print(np.vsplit(arr2, 2)
# np.hsplit() # 横向分割
print(np.hsplit(arr2, 2)