Repository navigation
Expand file tree
/
Copy pathNumpy.py
More file actions
386 lines (367 loc) · 14.5 KB
/
Copy pathNumpy.py
File metadata and controls
386 lines (367 loc) · 14.5 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#TỔNG HỢP FULL KIẾN THỨC CẦN THIẾT VỀ NUMPY
""" import numpy as np
array = np.array([1,2,3,4])
array = array / 2
print(array)
print(type(array)) """
#Bài 1: thư viên numpy
""" import numpy as np
array = np.array([1,2,3,4,5,6,7,8,9,10])
array = array * 10
print(array)
print(type(array)) """
#Bài 2: DIMENSIONS
'''import numpy as np
array = np.array(["A", "B", "C"])
print(array.ndim)'''
#hoặc
'''import numpy as np
array = np.array([["D", "E", "F"]])
print(array.ndim)'''
#hoặc
'''import numpy as np
array = np.array([[["F", "G", "H"]]])
print(array.ndim)'''
#hoặc
'''import numpy as np
array = np.array([[[["I", "J", "K"]]]])
print(array.ndim)'''
#Tạo thành matrix
'''import numpy as np
array = np.array([['A', 'B', 'C'],
['D', 'E', 'F'],
['G', 'H', 'I']])
print(array.ndim)'''
#Cách khác:
'''import numpy as np
array = np.array([[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'J']],
[['K', 'L', 'M'], ['N', 'O', 'P'], ['Q', 'R', 'S']],
[['T', 'U', 'V'], ['W', 'S', 'L'], ['Y', 'Z', '']]])
print(array.ndim)'''
# Multidimensional indexing is faster than chain indexing
'''import numpy as np
array = np.array([[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'J']],
[['K', 'L', 'M'], ['N', 'O', 'P'], ['Q', 'R', 'S']],
[['T', 'U', 'V'], ['W', 'S', 'L'], ['Y', 'Z', '']]])
print(array[0][0][0])''' #hoặc có thể viết print(array[0, 0, 0]) #giải thích: số thứ nhất là cột, số thứ 2 là hàng, số thứ 3 là khối từ trái sang phải
'''import numpy as np
array = np.array([[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'J']],
[['K', 'L', 'M'], ['N', 'O', 'P'], ['Q', 'R', 'S']],
[['T', 'U', 'V'], ['W', 'S', 'L'], ['Y', 'Z', '']]])
print(array[2][0][0])''' #hoặc có thể viết print(array[2, 0, 0]) #giải thích: số thứ nhất là cột, số thứ 2 là hàng, số thứ 3 là khối từ trái sang phải
#Luyện tập bất kỳ(ví dụ tạo tên là DO NGUYEN TIN TIN)
'''import numpy as np
array = np.array([[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'J']],
[['K', 'L', 'M'], ['N', 'O', 'P'], ['Q', 'R', 'S']],
[['T', 'U', 'V'], ['W', 'S', 'L'], ['Y', 'Z', 'I']]])
words = array[0,1,0] + array[1,1,1] + array[1,1,0] + array[0,2,0] + array[2,0,1] + array[2,2,0] + array[0,1,1] + array[1,1,0] + array[2,0,0] + array[2,2,2] + array[1,1,0] + array[2,0,0] + array[2,2,2] + array[1,1,0]
result = " ".join(words)
print(result)'''
# SLICING USING NUMPY
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[0])'''
#Làm tiếp tục(chỉ có start)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[-1])''' #Có thể dùng negative indexing bắt đầu từ -1
#Làm tiếp dạng khác(có start và enđ)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[0:4])''' #Giải thích: số đầu tiên là start(bắt đầu từ số 0), số thứ 2 là end(đếm từ trên xuống bắt đầu số 1)
#Làm tiếp tục:
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[1:4])''' #Giải thích: số đầu tiên là start(đếm từ trên xuống bắt đầu từ số 0), số thứ 2 là end(đếm từ trên xuống bắt đầu số 1)
#Làm tiếp tục:(có start:)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[1:])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[0:4:2])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[0:4:3])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[::2])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[::-1])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:,0])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:,1])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:,-1])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:,-2])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:,1:4])''' #số đầu tiên là start(đếm từ trên xuống bắt đầu từ số 0), số thứ 2 là end(đếm từ trên xuống bắt đầu số 1)
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:, ::2])''' #Giải thích: số 2 là step
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:, 1::2])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:, ::-1])
#Làm tiếp tục(có start:end:step)
import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[:, ::-2])'''
#Làm tiếp tục(có start:end:step)
'''import numpy as np
array = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
#array[start:end:step]
print(array[0:2, 0:2])''' #Giải thích: Số đầu tiên(0) đếm bắt đầu từ 0, số thứ hai(2) đếm bắt đầu từ 1 ; trước dấu , là start ; sau dấu , là end'
#SCALAR ARITHMETIC IN NUMPY
#Scalar is a linear algebra term
'''import numpy as np
array =np.array([1,2,3])
print(array + 1)
print(array -2)
print(array * 4)
print(array / 3)
print(array ** 6)'''
#VECTORIZED MATH FUNCTIONS IN NUMPY
'''array = np.array([1,2,3])
print(np.sqrt(array))'''
#Làm tròn số truyền thống(>= 5) dùng round
'''array = np.array([1.0002, 2.96, 123.3121, 432.442])
print(np.round(array))'''
#Làm tròn số xuống dùng floor
'''array = np.array([1.0002, 2.96, 123.3121, 432.442])
print(np.floor(array))'''
# Làm tròn lên dùng ceil
'''array = np.array([1.0002, 2.96, 123.3121, 432.442])
print(np.ceil(array))'''
#Làm tròn số pi(~3,14159)
'''import numpy as np
print(np.pi)'''
# Kết hợp pi với array và phép tính
'''import numpy as np
radius = np.array([1,2,3])
print(np.pi * radius ** 2)'''
# Elemet-wise arithmetic
'''import numpy as np
array1 = np.array([1,2,3])
array2 = np.array([4,5,6])
print(array1 + array2) #Có thể dùng các phép toán khác như - * / ** //
print(array1 - array2)
print(array1 * array2)
print(array1 / array2)
print(array1 ** array2)'''
# Comparison operators
'''import numpy as np
scores = np.array([91, 55, 100, 73, 82, 64])
print(scores == 100)''' #Có thể thêm các điều kiện khác như > < >= <= !=
#Làm kiểu khác
'''import numpy as np
scores = np.array([91, 55, 100, 73, 82, 59, 89])
scores[scores < 60] = 0
print(scores)'''
# Broadcasting allows Numpy to perform operations on arrays
# with different shapes by virtually expanding dimensions
# so they match the larger array's shape
# The dimensions have the same size
# OR One of the dimensions has a size of 1
# shape dùng để tìm ra bao nhiêu hàng bao nhiêu cột
'''import numpy as np
array1 = np.array([[1,2,3,4]])
array2 = np.array([[1], [2], [3], [4]])
print(array1.shape) #Mảng array1 có 1 hàng và 4 cột
print(array2.shape) #Mảng array2 có 4 hàng và 1 cột'''
#Có thể thêm số vào array1 tùy thích
'''import numpy as np
array1 = np.array([[1,2,3,4],
[5,6,7,8]])
array2 = np.array([[1], [2], [3], [4]])
print(array1.shape) #Mảng array1 có 2 hàng và 4 cột
print(array2.shape) #Mảng array2 có 4 hàng 1 cột'''
#Có thể thêm số vào array2 tùy thích
'''import numpy as np
array1 = np.array([[1,2,3,4]])
array2 = np.array([[1], [2], [3], [4],
[5], [6], [7], [8]])
print(array1.shape) #Mảng array1 có 1 hàng và 4 cột
print(array2.shape) #Mảng array2 có 8 hàng và 1 cột'''
#Luyện tập 1 bài nữa về array1 và array2
'''import numpy as np
array1 = np.array([[1,2,3,4,5,6,7,8,9,10]])
array2 = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
print(array1.shape) #Mảng array1 có 1 hàng và 10 cột
print(array2.shape) #Mảng array2 có 10 hàng và 1 cột'''
#Chúng ta có thể dùng + - * / ** // % sqrt abs absolute fabs giữa array1 và array2
'''import numpy as np
array1 = np.array([[1,2,3,4,5,6,7,8,9,10]])
array2 = np.array([[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]])
print(array1 * array2) #có thể print(np.sqrt(array1)), print(np.abs(array2)), ... '''
# Aggregate functions = summarize data and typically return a single value
'''import numpy as np
array = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
#print(np.sum(array)) hoặc #print(np.sum(array, axis = con số)) #axis chỉ có 2 con số là 0 và 1 #nếu axis = 0 thì thực hiện phép toán theo chiều dọc, nếu axis = 1 thì thực hiện phép toán theo chiều ngang.
#print(np.mean(array))
#print(np.std(array))
#print(np.var(array))
#print(np.max(array))
#print(np.min(array))
#print(np.argmax(array))
#print(np.argmin(array)) '''
# Filtering = Refers to the process of selecting elements from an array that match a given condition
'''import numpy as np
ages = np.array([[11,18,19,22,23,26,30,32,54,43,46],
[2,17,16,13,23,65,76,85,97,45,41]])
teenagers = ages[ages < 18]
print(teenagers)
#Có thể thêm điều kiện khác để lọc ra
adults = ages[ages >= 18]
print(adults)
#Thêm nhiều điều kiện cùng lúc
peoples = [(ages >= 18) & (ages <= 65)] #ngoài kí hiệu & có thể dùng and hoặc |
print(peoples)
#Làm thêm cho senior
seniors = ages[ages >= 65]
print(seniors)
#Làm thêm ví dụ evens
evens = ages[ages % 2 == 0]
print(evens)
#Làm thêm ví dụ odds
odds = ages[ages % 2 != 0]
print(odds)
#Có thể dùng từ khóa where để lọc bằng cách đặt điều kiện trong Python
#Ví dụ cho adults
adults = np.where(ages >= 18, ages, 0) #Giải thích: nếu điều kiện đúng=> ages được giữ nguyên, nếu điều kiện sai=>giá trị được thay thế bằng 0.
print(adults)'''
#Creating Random Numbers in NUMPY
'''import numpy as np
rng = np.random.default_rng()
print(rng.integers(1, 7)) # hoặc print(rng.integers(low=1, high=101, size = số)) #dùng size = con số bất kì, nghĩa là nó sẽ in ra con số hoặc những số ngẫu nhiên tùy vào size mình chọn.
# Nếu size = (số x, số y) #Giải thích: x là số hàng, y là số cột '''
# Có thể thêm seed vào random numbers
'''import numpy as np
rng = np.random.default_rng(seed = 1) #Cú pháp là seed = value (giúp tái tạo kết quả ngẫu nhiên trong các thí nghiệm hoặc mô phỏng)
print(rng.integers(1, 7)) '''
#Từ khóa uniform trong NUMPY
'''import numpy as np
print(np.random.uniform()) #Giải thích: từ khóa uniform nếu không gán giá trị thì nó sẽ mặc định lấy số ngẫu nhiên trong phạm vi từ 0-->1((bao gồm số thập phân))
#Nếu gán giá trị thì nó sẽ tuân theo giá trình mình gán '''
#Luyện tập
'''import numpy as np
np.random.seed(seed=1)
print(np.random.uniform(low=-1, high=1, size=(5, 8))) '''
#Kết hợp array với random numbers + từ khóa shuffle(xáo trộn) các con số ngẫu nhiên trong NUMPY
'''import numpy as np
rng = np.random.default_rng()
array = np.array([1,2,3,4,5])
rng.shuffle(array) #Giải thích: shuffle dùng để xóa trộn số một cách ngẫu nhiên.
print(array) '''
#Luyện tập xáo trộn giá trị ngẫu nhiên dạng văn bản
'''import numpy as np
rng = np.random.default_rng()
family = np.array(["Tin Tin", "Tin Tín", "Tin Tưởng", "Ba Cường", "Má Phương"])
rng.shuffle(family) #hoặc có thể ghi family = rng.shuffle(family) ==> print(family)
print(family) '''
#Dùng từ khóa choice để lấy ngẫu nhiên ra 1 item
import numpy as np
rng = np.random.default_rng()
family = np.array(["Tin Tin", "Tin Tín", "Tin Tưởng", "Ba Cường", "Má Phương"])
family = rng.choice(family) #hoặc có thể ghi family = rng.choice(family) ==> print(family) #Có thể thêm size vào ví dụ: family = rng.choice(family, size(3,5))
print(family)
#BÀI TẬP: Thay vì dùng văn bản random chán có thể dùng hình dán(emoji) để gán vào cho nó thú vị hơn.
#__________________________________________________Hết____________________________________________________________