一、数组对象的属性
数组的大小(元素个数)
array.size
数组的维度
array.ndim
数组元素的数据类型
array.dtype
数组的形状
array.shape
数组中每个元素占用的内存空间
array.itemsize
数组所有元素占用的内存空间(字节)
array.nbytes
数组转置
array.T
数组的形状
array.shape
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
二、数组的索引运算
array15 = np.random.randint(0, 100, 10)
array15
array([51, 52, 46, 4, 58, 55, 40, 98, 75, 3])
array15[0] = 100
array15
array([100, 52, 46, 4, 58, 55, 40, 98, 75, 3])
array15[-1] = 200
array15
array([100, 52, 46, 4, 58, 55, 40, 98, 75, 200])
array15[[1, 2, 5, 1, 1, -1]]
array([ 52, 46, 55, 52, 52, 200])
array16 = np.arange(1, 10).reshape((3, 3))
array16
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
array16[1]
array([4, 5, 6])
array16[1][1]
5
二维数组的索引运算
array16[1, 1]
5
array16[-1, 1]
8
array16[[0, 1, 1, 2], [0, 1, 2, 0]]
array([1, 5, 6, 7])
array16[[0, 1, 1, 2]]
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]])
array15[[True, False, False, True, False, True, True, False, False, False]]
array([100, 4, 55, 40])
array15 > 50
array([ True, True, False, False, True, True, False, True, True,
True])
array15[array15 > 50]
array([100, 52, 58, 55, 98, 75, 200])
array15 % 2 != 0
array([False, False, False, False, False, True, False, False, True,
False])
array15[array15 % 2 != 0]
array([55, 75])
(array15 > 50) & (array15 % 2 != 0)
array([False, False, False, False, False, True, False, False, True,
False])
array15[(array15 > 50) & (array15 % 2 != 0)]
array([55, 75])
array15[(array15 > 50) | (array15 % 2 != 0)]
array([100, 52, 58, 55, 98, 75, 200])
array15[(array15 > 50) & ~(array15 % 2 != 0)]
array([100, 52, 58, 98, 200])
array15[(array15 > 50) & (array15 % 2 == 0)]
array([100, 52, 58, 98, 200])
array16[array16 > 5]
array([6, 7, 8, 9])
array15[1:5]
array([52, 46, 4, 58])
array15[1:5:2]
array([52, 4])
array15[-3:-8:-1]
array([98, 40, 55, 58, 4])
array16
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
array16[:2, 1:]
array([[2, 3],
[5, 6]])
array16[2]
array([7, 8, 9])
array16[2, :]
array([7, 8, 9])
array16[2:, :]
array([[7, 8, 9]])
array16[::2, ::2]
array([[1, 3],
[7, 9]])
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
三、数组对象的方法
源数据
array17 = np.random.randint(200, 500, 12)
1
array([351, 293, 272, 284, 243, 339, 461, 322, 214, 429, 339, 439])
array17.sum()
3986
np.sum(array17)
3986
array17.mean()
332.1666666666667
np.mean(array17)
332.1666666666667
np.median(array17)
330.5
np.quantile(array17, 0.5)
330.5
array17.max()
461
np.amax(array17)
461
array17.min()
214
np.amin(array17)
214
array17.ptp()
247
np.ptp(array17)
247
q1, q3 = np.quantile(array17, [0.25, 0.75])
iqr = q3 - q1
iqr
q1, q3 = np.quantile(array17, [0.25, 0.75])
iqr = q3 - q1
iqr
89.5
array17.var()
5610.63888888889
np.var(array17)
5610.63888888889
array17.std()
74.90419807253055
np.std(array17)
74.90419807253055
array17.cumsum()
array([ 351, 644, 916, 1200, 1443, 1782, 2243, 2565, 2779, 3208, 3547,
3986])
array19 = np.array([1, 0, 2, 3, 0, 0, 4, 5])
array19
array([1, 0, 2, 3, 0, 0, 4, 5])
array19[array19.nonzero()]
array([1, 2, 3, 4, 5])
array19[array19 != 0]
array([1, 2, 3, 4, 5])
array17.sort()
array17
array([214, 243, 272, 284, 293, 322, 339, 339, 351, 429, 439, 461])
array16.T
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
array16.swapaxes(0, 1)
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
array16.transpose()
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
array17.tolist()
[214, 243, 272, 284, 293, 322, 339, 339, 351, 429, 439, 461]
array16.tolist()
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
array20 = np.random.randint(0, 100, (12, 5))
array20
array([[12, 11, 78, 15, 61],
[40, 3, 78, 69, 54],
[51, 38, 13, 42, 46],
[89, 60, 46, 42, 56],
[48, 30, 75, 11, 87],
[57, 28, 11, 39, 81],
[99, 98, 29, 5, 50],
[97, 68, 6, 9, 59],
[83, 86, 44, 93, 89],
[50, 19, 25, 24, 94],
[20, 74, 9, 32, 28],
[99, 35, 84, 56, 4]])
array20.reshape(4, 15)
array([[12, 11, 78, 15, 61, 40, 3, 78, 69, 54, 51, 38, 13, 42, 46],
[89, 60, 46, 42, 56, 48, 30, 75, 11, 87, 57, 28, 11, 39, 81],
[99, 98, 29, 5, 50, 97, 68, 6, 9, 59, 83, 86, 44, 93, 89],
[50, 19, 25, 24, 94, 20, 74, 9, 32, 28, 99, 35, 84, 56, 4]])
array20.reshape(2, 3, 10)
array([[[12, 11, 78, 15, 61, 40, 3, 78, 69, 54],
[51, 38, 13, 42, 46, 89, 60, 46, 42, 56],
[48, 30, 75, 11, 87, 57, 28, 11, 39, 81]],
[[99, 98, 29, 5, 50, 97, 68, 6, 9, 59],
[83, 86, 44, 93, 89, 50, 19, 25, 24, 94],
[20, 74, 9, 32, 28, 99, 35, 84, 56, 4]]])
np.resize(array20, (3, 3, 3))
array([[[12, 11, 78],
[15, 61, 40],
[ 3, 78, 69]],
[[54, 51, 38],
[13, 42, 46],
[89, 60, 46]],
[[42, 56, 48],
[30, 75, 11],
[87, 57, 28]]])
np.resize(array20, (20, 5))
array([[12, 11, 78, 15, 61],
[40, 3, 78, 69, 54],
[51, 38, 13, 42, 46],
[89, 60, 46, 42, 56],
[48, 30, 75, 11, 87],
[57, 28, 11, 39, 81],
[99, 98, 29, 5, 50],
[97, 68, 6, 9, 59],
[83, 86, 44, 93, 89],
[50, 19, 25, 24, 94],
[20, 74, 9, 32, 28],
[99, 35, 84, 56, 4],
[12, 11, 78, 15, 61],
[40, 3, 78, 69, 54],
[51, 38, 13, 42, 46],
[89, 60, 46, 42, 56],
[48, 30, 75, 11, 87],
[57, 28, 11, 39, 81],
[99, 98, 29, 5, 50],
[97, 68, 6, 9, 59]])
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
四、数组和标量的运算
array1 = np.array([1, 2, 3, 4, 5])
array1
array([1, 2, 3, 4, 5])
array1 + 10
array([11, 12, 13, 14, 15])
array1 % 2
array([1, 0, 1, 0, 1], dtype=int32)
array1 ** 2
array([ 1, 4, 9, 16, 25])
array2 = np.arange(1, 10).reshape(3, 3)
array2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
array2 * 10
array([[10, 20, 30],
[40, 50, 60],
[70, 80, 90]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
五、数组和数组的运算
array1 = np.array([1, 2, 3, 4, 5])
array1
array([1, 2, 3, 4, 5])
array3 = np.array([10, 10, 10, 0.1, 0.01])
array3
array([10. , 10. , 10. , 0.1 , 0.01])
array1 / array3
array([1.e-01, 2.e-01, 3.e-01, 4.e+01, 5.e+02])
array4 = np.array([[10, 10, 10], [20, 20, 20], [30, 40, 50]])
array4
array([[10, 10, 10],
[20, 20, 20],
[30, 40, 50]])
array2 * array4
array([[ 10, 20, 30],
[ 80, 100, 120],
[210, 320, 450]])
array5 = np.arange(10, 70, 10).reshape(2, 3)
array5
array([[10, 20, 30],
[40, 50, 60]])
array5.shape
(2, 3)
array2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
array6 = np.array([[10, 20, 30]])
array6
array([[10, 20, 30]])
array2.shape
(3, 3)
array6.shape
(1, 3)
array2 + array6
array([[11, 22, 33],
[14, 25, 36],
[17, 28, 39]])
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
广播机制
两个形状(shape属性)不一致的数组能否运算,取决于是否可以通过广播机制让它们的形状一致。
广播机制的适用条件(二选一):
两个数组后缘维度(shape属性从后往前看)相同。
两个数组后缘维度不相同,但是不同的部分其中一方是1。
1
2
3
4
5
6
array6
array([[10, 20, 30]])
array7 = np.array([10, 20, 30]).reshape(3, 1)
array7
array([[10],
[20],
[30]])
array6 + array7
array([[20, 30, 40],
[30, 40, 50],
[40, 50, 60]])
1
2
3
4
5
6
7
8
9
10
11
12
13
六、NumPy中操作数组的函数
array8 = np.array([1, np.nan, 3, np.inf, 5])
array8
array([ 1., nan, 3., inf, 5.])
np.isnan(array8)
array([False, True, False, False, False])
np.any(np.isnan(array8))
True
np.isinf(array8)
array([False, False, False, True, False])
np.any(np.isinf(array8))
True
array8[~np.isinf(array8) & ~np.isnan(array8)]
array([1., 3., 5.])
array9 = np.array([[1, 0, 2], [-1, 3, 1]])
array9
array([[ 1, 0, 2],
[-1, 3, 1]])
array10 = np.array([[3, 1], [2, 1], [1, 0]])
array10
array([[3, 1],
[2, 1],
[1, 0]])
array9 @ array10
array([[5, 1],
[4, 2]])
np.matmul(array9, array10)
array([[5, 1],
[4, 2]])
array11 = np.random.randint(1, 100, 10)
array11
array([75, 35, 21, 74, 19, 21, 56, 2, 56, 80])
np.sort(array11)
array([ 2, 19, 21, 21, 35, 56, 56, 74, 75, 80])
array11
array([75, 35, 21, 74, 19, 21, 56, 2, 56, 80])
array11.sort()
array11
array([ 2, 19, 21, 21, 35, 56, 56, 74, 75, 80])
array12 = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
array13 = np.array([[4, 4, 4], [5, 5, 5], [6, 6, 6]])
array12
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
array13
array([[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
array14 = np.vstack((array12, array13))
array14
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
np.hstack((array12, array13))
array([[1, 1, 1, 4, 4, 4],
[2, 2, 2, 5, 5, 5],
[3, 3, 3, 6, 6, 6]])
np.stack((array12, array13), axis=0)
array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]])
np.stack((array12, array13), axis=1)
array([[[1, 1, 1],
[4, 4, 4]],
[[2, 2, 2],
[5, 5, 5]],
[[3, 3, 3],
[6, 6, 6]]])
np.split(array14, 3)
[array([[1, 1, 1],
[2, 2, 2]]),
array([[3, 3, 3],
[4, 4, 4]]),
array([[5, 5, 5],
[6, 6, 6]])]
np.append(array11, 100)
array([ 2, 19, 21, 21, 35, 56, 56, 74, 75, 80, 100])
np.insert(array11, 1, 100)
array([ 2, 100, 19, 21, 21, 35, 56, 56, 74, 75, 80])
np.repeat(array11, 2)
array([ 2, 2, 19, 19, 21, 21, 21, 21, 35, 35, 56, 56, 56, 56, 74, 74, 75,
75, 80, 80])
np.tile(array11, 2)
array([ 2, 19, 21, 21, 35, 56, 56, 74, 75, 80, 2, 19, 21, 21, 35, 56, 56,
74, 75, 80])
array11
array([ 2, 19, 21, 21, 35, 56, 56, 74, 75, 80])
np.extract(array11 % 2 != 0, array11)
array([19, 21, 21, 35, 75])
np.select([array11 < 30, array11 > 60], [array11 ** 2, array11 / 10])
array([ 4. , 361. , 441. , 441. , 0. , 0. , 0. , 7.4, 7.5,
8. ])
np.where(array11 > 50, array11 / 10, array11 ** 2)
array([ 4. , 361. , 441. , 441. , 1225. , 5.6, 5.6, 7.4,
7.5, 8. ])
np.put(array11, [0, 2, 3, 5], [100, 200])
array11
array([100, 19, 200, 100, 35, 200, 56, 74, 75, 80])
np.place(array11, array11 > 50, [1, 2, 3])
array11
array([ 1, 19, 2, 3, 35, 1, 2, 3, 1, 2])
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
七、向量
v1 = np.array([3, 4])
v2 = np.array([5, 1])
v1 + v2
array([8, 5])
v1 - v2
array([-2, 3])
2 * v1
array([6, 8])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
用向量判断最接近的数据
hao = np.array([80, 95, 98])
hao
array([80, 95, 98])
stu1 = np.array([100, 20, 50])
stu2 = np.array([90, 92, 95])
stu3 = np.array([80, 94, 97])
theta1 = np.dot(hao, stu1) / np.linalg.norm(hao) / np.linalg.norm(stu1)
np.rad2deg(np.arccos(theta1))
34.54767877562217
theta2 = np.dot(hao, stu2) / np.linalg.norm(hao) / np.linalg.norm(stu2)
np.rad2deg(np.arccos(theta2))
3.8613998977477624
theta3 = np.dot(hao, stu3) / np.linalg.norm(hao) / np.linalg.norm(stu3)
np.rad2deg(np.arccos(theta3))
0.26109321614097053
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
由上数据可以知道最后一组数据与源数组最接近
八、行列式运算
det1 = np.vstack((v1, v2))
det1
array([[3, 4],
[5, 1]])
np.linalg.det(det1)
-17.0
det2 = np.arange(1, 10).reshape(3, -1)
det2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.linalg.det(det2)
0.0
det2[-1, -1] = 8
det2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 8]])
np.around(np.linalg.det(det2))
3.0
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
九、矩阵运算
mat1 = np.array([1, 3, 5, 9, 4, 3, 6, 2]).reshape(4, 2)
mat1
array([[1, 3],
[5, 9],
[4, 3],
[6, 2]])
mat2 = np.array([10, 10, 20, 20, 30, 30]).reshape(2, 3)
mat2
array([[10, 10, 20],
[20, 30, 30]])
mat1 @ mat2
array([[ 70, 100, 110],
[230, 320, 370],
[100, 130, 170],
[100, 120, 180]])
np.matmul(mat1, mat2)
array([[ 70, 100, 110],
[230, 320, 370],
[100, 130, 170],
[100, 120, 180]])
mat1.T
array([[1, 5, 4, 6],
[3, 9, 3, 2]])
mat2.T
array([[10, 20],
[10, 30],
[20, 30]])
mat3 = np.arange(1, 10).reshape(3, 3)
mat3
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
mat3[-1, -1] = 8
mat3
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 8]])
mat4 = np.linalg.inv(mat3)
mat4
array([[-2.66666667, 2.66666667, -1. ],
[ 3.33333333, -4.33333333, 2. ],
[-1. , 2. , -1. ]])
np.around(mat3 @ mat4)
array([[ 1., 0., -0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
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
实例一:解方程组
线性方程组有唯一解的条件:系数矩阵(A)的秩等于增广矩阵([Ab])的秩,而且跟未知数的个数相同。
矩阵的秩(rank):线性无关的行的数量。
线性相关:如果一个向量能够通过另一个向量的线性变换得到,那么两个向量是线性相关的;否则就是线性无关的。
A = np.array([[1, 2, 1], [3, 7, 2], [2, 2, 1]])
b = np.array([8, 23, 9]).reshape(-1, 1)
np.linalg.matrix_rank(A)
3
Ab = np.hstack((A, b))
Ab
array([[ 1, 2, 1, 8],
[ 3, 7, 2, 23],
[ 2, 2, 1, 9]])
np.linalg.matrix_rank(Ab)
3
用函数解决并且得到答案
np.linalg.solve(A, b)
array([[1.],
[2.],
[3.]])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
也可以通过运算获得答案
np.linalg.inv(A) @ b
array([[1.],
[2.],
[3.]])
1
2
3
4
实例二:最小二乘法求斜率与截距
incomings = np.array([5550, 7500, 10500, 15000, 20000, 25000, 30000, 40000])
incomings
array([ 5550, 7500, 10500, 15000, 20000, 25000, 30000, 40000])
outcomings = np.array([800, 1800, 1250, 2000, 1800, 2100, 2500, 3500])
outcomings
array([ 800, 1800, 1250, 2000, 1800, 2100, 2500, 3500])
1
2
3
4
5
6
7
8
9
10
11
计算开始
np.corrcoef(incomings, outcomings)
array([[1. , 0.91950911],
[0.91950911, 1. ]])
相关系数达到了0.9+,认为强相关
#=⋅+
#=⋅+1⋅
ca = incomings.reshape(-1, 1)
cb = np.ones(incomings.size).reshape(-1, 1)
param1 = np.hstack((ca, cb))
param2 = outcomings.reshape(-1, 1)
param2是y的值
r1, r2, *_ = np.linalg.lstsq(param1, param2, rcond=None)
a, b = r1.flatten()
r1
array([[6.22570756e-02],
[7.73803255e+02]])
r2
array([708347.96412991])
def predict(sal):
return round(a * sal + b, 1)
print(predict(3000))
print(predict(18000))
print(predict(28000))
print(predict(58000))
print(predict(120000))
960.6
1894.4
2517.0
4384.7
8244.7
np.polyfit(incomings, outcomings, 1)
array([6.22570756e-02, 7.73803255e+02])