02.Numpy

Posted kingboy100

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02.Numpy相关的知识,希望对你有一定的参考价值。

01.array

  1 # -*- coding: utf-8 -*-
  2 """
  3 Numpy ??? ?? 
  4  - ????(??, ??) ??? ???? ?? ?? 
  5  - list ??? : ??? ??, ???? ??, ?? ??
  6  - Series ??? 
  7    -> ??/?? ?? 
  8    -> ?? ??, ?? ?? 
  9    -> indexing/slicing ?? 
 10  - n?? ?? ?? ?? ?? 
 11    1. random ?? 
 12    2. array ?? 
 13    3. sampling ?? 
 14    4. arange ?? 
 15 """
 16 
 17 import numpy as np
 18 
 19 # numpy 1?? ???? 
 20 lst = [1,2,3]
 21 arr = np.array(lst)
 22 print(lst) # [1, 2, 3]
 23 #print(lst**2) # error
 24 print(arr) # [1 2 3]
 25 print(arr**2) # [1 4 9]
 26 print(type(arr)) # <class ‘numpy.ndarray‘>
 27 
 28 lst2 = [1, "two", False]
 29 print(lst2) # [1, ‘two‘, False]
 30 
 31 arr2 = np.array(lst2)
 32 print(arr2) # [‘1‘ ‘two‘ ‘False‘]
 33 print(arr2.shape) # (3,)
 34 
 35 
 36 # 1. random ?? : ?? ?? 
 37 #help(np.random.randn)
 38 
 39 data = np.random.randn(3, 4) # 3?4?-12?? ?? 
 40 print(data)
 41 ‘‘‘
 42 [[ 0.21625386 -1.11271239  1.26352269 -0.29090546]
 43  [ 1.19998039 -0.93916248 -0.96475192 -0.71720834]
 44  [-0.35985917  0.46820202  0.20267762  0.56218989]]
 45 ‘‘‘
 46 
 47 for row in data :
 48     print(? ?? :, row.mean())
 49     print(? ?? :, row.sum())
 50     
 51 
 52 # 1) ??/?? ??
 53 print(??=, data.sum())
 54 print(??=, data.mean())
 55 print(??=,data.var())
 56 print(????=,data.std())
 57 
 58 # 2) ???? 
 59 print(data + data) # 2?
 60 print(data - data) # 0
 61 
 62 # 3) indexing
 63 print(data[2,2]) # 3?3?
 64 print(data[:,2]) # 3? ?? 
 65     
 66 
 67 # 2. array ?? : ??? ?? 
 68 
 69 # 1) ?? list - 1?? 
 70 lst1 = [3, 5.6, 4, 7, 8]
 71 arr1 = np.array(lst1)
 72 print(arr1)
 73 
 74 # ??/???? 
 75 print(??=, arr1.var())
 76 print(????=, arr1.std())
 77 ‘‘‘
 78 ??= 3.4016000000000006
 79 ????= 1.8443427013437608
 80 
 81 ??? ??, ???? 
 82 ?? = sum((x-avg)**2) / n
 83 ?? ?? 
 84 ?? = sum((x-avg)**2) / n-1
 85 ‘‘‘
 86 avg = arr1.mean()
 87 diff = arr1 - avg # braodcast 
 88 var_result = sum(diff**2) / len(arr1)
 89 print(??=, var_result)
 90 # ??= 3.4016000000000006
 91 std_result = np.sqrt(var_result)
 92 print(????=, std_result)
 93 ‘‘‘
 94 ??= 3.4016000000000006
 95 ????= 1.8443427013437608
 96 ‘‘‘
 97 
 98 # 2) ?? list ->  2?? 
 99 lst2 = [[1,2,3,4,5], [6,7,8,9,10]]
100 print(lst2)
101 arr2  = np.array(lst2)
102 print(arr2)
103 print(arr2.shape) # (2, 5)
104 print(np.shape(arr2)) # (2, 5)
105 
106 # ppt. 21
107 print(arr2[1,:]) # 2? ?? - [ 6  7  8  9 10]
108 print(arr2[:,2]) # 3? ?? - [3 8] 
109 print(arr2[1,2]) # 2?3? - 8
110 print(arr2[:,1:3])
111 ‘‘‘
112 [[2 3]
113  [7 8]]
114 ‘‘‘
115 
116 # broadcast ??(????)
117 # - ?? ??? ? ???? ??? 
118 
119 # 1) scala(0) vs vector(1)
120 print(arr1) # [3.  5.6 4.  7.  8. ]
121 print(arr1 * 0.5) # [1.5 2.8 2.  3.5 4. ]
122 
123 # 2) scala(0) vs matrix(2)
124 print(arr2 * 0.5)
125 
126 # 3) vector(1) vs matrix(2)
127 print(arr1 + arr2)
128 ‘‘‘
129 [3.  5.6 4.  7.  8. ]
130    +
131 [[ 1  2  3  4  5]
132  [ 6  7  8  9 10]]
133    =
134 [[ 4.   7.6  7.  11.  13. ]
135  [ 9.  12.6 12.  16.  18. ]]
136 ‘‘‘
137 
138 
139 # 3. sampling ??
140 
141 # 1) choice?? : ??? ? ?? ?? 
142 num = list(range(11)) # 0~10
143 print(num)
144 n = len(num)
145 
146 #np.random.choice : ???.??.??() 
147 idx = np.random.choice(n, 5, replace=False)
148 # replace=False : ??? 
149 print(idx) # [4 3 1 5 0]
150 
151 
152 import pandas as pd 
153 
154 score = pd.read_csv("../data/score_iq.csv")
155 print(score.info())
156 ‘‘‘
157 RangeIndex: 150 entries, 0 to 149
158 Data columns (total 6 columns):
159 ‘‘‘
160 
161 # train : 70%, test : 30%
162 n=len(score)
163 
164 idx = np.random.choice(n, int(n*0.7), replace=False)
165 
166 # pandas -> numpy
167 np_score = np.array(score)
168 print(np_score.shape) # (150, 6)
169 
170 train_set = np_score[idx, :]
171 print(train_set.shape) # (105, 6)
172 
173 # test set : list+for
174 # [???-3  for-1  if-2]
175 test_idx = [i  for i in range(150) if i not in idx]
176 print(test_idx)
177 print(len(test_idx)) # 45
178 
179 test_set = np_score[test_idx, :]
180 print(test_set.shape) # (45, 6)
181 
182 # 2) shuffle ?? 
183 #help(np.random.shuffle) # x : array or list
184 
185 print(np_score[:10,:])
186 
187 np.random.shuffle(np_score) # array
188 print(np_score[:10,:])
189 
190 
191 # 4. arange ?? : range(n) : 0~n-1 ?? 
192 zerr = np.zeros((3, 5))
193 print(zerr)
194 ‘‘‘
195 [[0. 0. 0. 0. 0.]
196  [0. 0. 0. 0. 0.]
197  [0. 0. 0. 0. 0.]]
198 ‘‘‘
199 
200 cnt = 0
201 for i in np.arange(3) : # 0~2
202     for j in np.arange(5) : # 0~4
203         cnt += 1 # ??? 
204         zerr[i,j] = cnt
205 
206 print(zerr)
207 ‘‘‘
208 [[ 1.  2.  3.  4.  5.]
209  [ 6.  7.  8.  9. 10.]
210  [11. 12. 13. 14. 15.]]
211 ‘‘‘
212 cnt = 0
213 for i in range(3) : # 0~2
214     for j in range(5) : # 0~4
215         cnt += 1 # ??? 
216         zerr[i,j] = cnt
217 
218 print(zerr)
219 ‘‘‘
220 [[ 1.  2.  3.  4.  5.]
221  [ 6.  7.  8.  9. 10.]
222  [11. 12. 13. 14. 15.]]
223 ‘‘‘

 

 

 

02.indexing

 1 # -*- coding: utf-8 -*-
 2 """
 3 numpy indexing
 4   - 2,3?? indexing
 5   - boolean indexing
 6 """
 7 
 8 import numpy as np
 9 
10 # 1. indexing
11 ‘‘‘
12 1?? : obj[index]
13 2?? : obj[row, col] - row default
14 3?? : obj[side, row, col] - side default
15 ‘‘‘
16 
17 # 2?? indexing
18 arr2d = np.array([[1,2,3], [4,5,6], [7,8,9]])
19 print(arr2d.shape) # (3, 3)
20 
21 print(arr2d[1]) # 2? ?? - [4 5 6]
22 print(arr2d[:,1]) # 2? ?? - [2 5 8]
23 print(arr2d[1,2]) # 2?3? - 6
24 
25 # 3?? indexing
26 arr3d = np.array([[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]]])
27 print(arr3d)
28 print(arr3d.shape) # (2, 2, 3)
29 
30 print(arr3d[1]) # 2? ?? 
31 print(arr3d[1, 0]) # 2? 1? ?? 
32 print(arr3d[1, 0, 2]) # 2? 1? 3? 
33 ‘‘‘
34 [[ 7  8  9]
35  [10 11 12]]
36 [7 8 9]
37 9
38 ‘‘‘
39 
40 print(arr3d[1, :, :2])
41 ‘‘‘
42 [[ 7  8]
43  [10 11]]
44 ‘‘‘
45 
46 # 4. boolean indexing
47 data = np.random.randn(3, 4) # 12? 
48 print(data)
49 
50 # ???? ?? 
51 result = data[data >= 0.7]
52 print(result)
53 
54 # 0.3 ~ 0.7
55 #result = data[data >= 0.3 and data <= 0.7]
56 
57 result2 = data[np.logical_and(data >= 0.3, data <= 0.7)]
58 print(result2) 
59 ‘‘‘
60 [1.06451721 0.9287353 ]
61 [0.6895027]
62 ‘‘‘

 

 

 

03.reshape

 1 # -*- coding: utf-8 -*-
 2 """
 3  reshape : ?? ?? 
 4   - 1?? ?? -> 2?? ?? 
 5   - 2?? ?? -> ?? ?? ?? 
 6  T : ????(?? ?? ??)
 7  swapaxis : ? ?? 
 8  transpose : ? ?? ??? ??? ?? ?? 
 9 """
10 
11 import numpy as np
12 
13 # 1. reshape
14 lst = range(1,13) # 1~12
15 
16 # 1?? -> 2??
17 arr2d = np.array(lst).reshape(3,4) # 1?? -> 2?? 
18 print(arr2d)
19 print(arr2d.shape) # (3, 4)
20 
21 # 2?? ?? ?? 
22 arr2d = np.array(arr2d).reshape(2,6) # ?? : ? ?? 
23 print(arr2d.shape) # (2, 6)
24 
25 # 2?? -> 3?? 
26 arr3d = np.array(arr2d).reshape(1,4,3)
27 print(arr3d)
28 print(arr3d.shape) # (1, 4, 3)
29 ‘‘‘
30 [[[ 1  2  3]
31   [ 4  5  6]
32   [ 7  8  9]
33   [10 11 12]]]
34 ‘‘‘
35 
36 # 2. ????(?<->?)
37 print(arr2d.T)
38 print(arr2d.T.shape) # (6, 2)
39 
40 
41 # 3. swapaxes
42 # axis = 0(?), axis=1(?)
43 print(arr2d.swapaxes(0,1)) # (6, 2)
44 
45 
46 # 4. transpose
47 ‘‘‘
48 1?? : ?? ?? 
49 2?? : ?<-? ?? = ???? 
50 3?? : ? ??? ??? ?? ??(o) 
51 ‘‘‘
52 
53 arr3d = np.arange(1,25).reshape(4, 2, 3) # 1~24
54 print(arr3d)
55 print(arr3d.shape) # (4, 2, 3) - (?,?,?)
56 
57 
58 # 3?? : (0,1,2) -> (2,1,0) : ?? 
59 arr3d_def = arr3d.transpose() # default : (2,1,0)
60 print(arr3d_def)
61 print(arr3d_def.shape) # (3, 2, 4)
62 
63 # (0,1,2) -> (2,0,1)
64 arr3d_user = arr3d.transpose(2,0,1)
65 print(arr3d_user)
66 print(arr3d_user.shape) # (3, 4, 2)

 

 

 

04.axis_dot

 1 # -*- coding: utf-8 -*-
 2 """
 3  1. axis : ??, ?? 
 4  2. np.dot() : ??? - tf.matmul()
 5  3. ANN?? ??? 
 6     - ???(H) = ??(X) * ???(W) + ??(B)
 7  4. ???? ?? ??? 
 8     - ???(Y) = ??(X) * ???(a) + ??(b)
 9 """
10 
11 import numpy as np
12 
13 # 1. axis : ppt. 56
14 # ?? : ??? ??(? ??)
15 # ?? : ??? ??(? ??)
16 
17 arr = np.arange(1,21).reshape(5,4)
18 print(arr)
19 
20 print(?? ??=,arr.sum())
21 print(? ?? ??=,arr.sum(axis=0)) # ? ?? 
22 print(? ?? ??=,arr.sum(axis=1)) # ? ?? 
23 ‘‘‘
24 ?? ??= 210
25 ? ?? ??= [45 50 55 60]
26 ? ?? ??= [10 26 42 58 74]
27 ‘‘‘
28 
29 # 2. np.dot(a, b) # a,b : ?? 
30 a = np.array([[1,1], [0,1]]) 
31 print(a.shape) # (2, 2)
32 b = np.array([[2,3], [1,5]])
33 print(b.shape) # (2, 2)
34 ‘‘‘
35 ??? ?? 
36 1. a,b ?? ?? 
37 2. a(?) == b(?) : ? ?? 
38 ‘‘‘
39 
40 c = np.dot(a, b)
41 print(c)
42 print(c.shape)
43 ‘‘‘
44 a(r,c) * b(r,c) = c(a(r), b(c))
45 [[3 8]
46  [1 5]]
47 (2, 2)
48 ‘‘‘
49 
50 print(np.ndim(a), np.ndim(b), np.ndim(c))
51 # 2 2 2
52 
53 
54 # 1) 1? ??? : x(1,2) * w(2,2) = h(1,2) 
55 x = np.array([[0.1, 0.2]])
56 w = np.array([[1,2], [2,3]])
57 print(x.shape) # (1, 2)
58 print(w.shape) # (2, 2)
59 
60 h = np.dot(x, w)
61 print(h=, h) # h= [[0.5 0.8]]
62 print(h.shape) # (1, 2)
63 
64 # 2) 2? ??? : x(2,2) * w(2,3) = h(2,3)
65 x = np.array([[0.1, 0.2], [0.3, 0.4]]) # (2,2)
66 w = np.array([[1,2,3],[2,3,4]]) # (2,3)
67 
68 h = np.dot(x, w)
69 print(h=)
70 print(h)
71 print(h.shape) # (2, 3)

 

 

 

05.dot_example

 1 # -*- coding: utf-8 -*-
 2 """
 3 ANN Model example
 4 """
 5 
 6 import numpy as np
 7 
 8 # 1. ANN model
 9 ‘‘‘
10 input x : image(28x28)
11 hidden node : 32?
12 weight : 28x32
13 ‘‘‘
14 
15 # x data ?? 
16 print(>>> x image data <<<)
17 x_img = np.random.randint(0,2, 784) # image vector(0 or 1)
18 x_img2d = x_img.reshape(28, 28) # matrix
19 print(x_img2d)
20 print(x_img2d.shape) # (28, 28)
21 
22 # weight data ?? 
23 print(>>> weight data <<<)
24 weight = np.random.randn(28, 32) 
25 print(weight)
26 print(weight.shape) # (28, 32)
27 
28 # hidden node ?? 
29 print(>>> hidden node <<<)
30 # (28, 28) * (28, 32) = (28, 32)
31 hidden = np.dot(x_img2d, weight)
32 print(hidden)
33 print(hidden.shape)

 

以上是关于02.Numpy的主要内容,如果未能解决你的问题,请参考以下文章

[学习笔记] [数据分析] 02NumPy入门与应用

python第三方库02:NumPy基础及取值操作

02 numpy 索引与切片

python数据分析-02numpy库

numpy库使用总结

量化投资_NumPy_01