├── .gitignore
├── README.MD
├── algorithms
├── divide-and-conquer
│ └── Untitled.ipynb
├── graph
│ └── backtracking
│ │ ├── backtracking.MD
│ │ └── main.cpp
└── imgs
│ ├── 75435905_1500043306811247_1920143822067597312_n.png
│ ├── Knights-Tour-Animation.gif
│ ├── godemo.png
│ ├── result.png
│ └── visual.png
├── articles
├── GradientPaperSpace.MD
└── YOLO.ipynb
├── computer-vision
└── README.MD
├── data-processing
├── ImageAugmentation.ipynb
└── hanoi.jpg
├── deployment
├── .DS_Store
├── distributed-tensorflow
│ ├── .DS_Store
│ ├── PretrainedModelDeployment.ipynb
│ ├── README.md
│ ├── TensorFlowServing.ipynb
│ └── images
│ │ ├── .DS_Store
│ │ ├── Train-versus-Inference.png
│ │ └── serving_architecture.png
└── tensorflow-browser
│ └── README.md
├── essential-installs
├── vn-mac-python-and-libraries-installations.MD
└── vn-windows-python-and-libraries-installations.MD
├── gan
├── README.MD
└── cyclegan
│ ├── CycleGAN.ipynb
│ └── images
│ ├── Generator.jpg
│ ├── Resnet.jpg
│ └── styletransfer.jpeg
├── images
├── .DS_Store
├── Logistic .jpg
├── Recursion Algorithm_2.png
├── Recursion_Algorithm.png
├── class.jpg
├── gde.png
├── logis.gif
├── logmu.png
├── pp1.png
├── pp10.png
├── pp2.png
├── pp3.png
├── pp4.png
├── pp5.png
├── pp6.png
├── pp7.png
├── pp8.png
├── pp9.png
├── softmax.png
├── transfer.png
└── youtube.png
├── math
├── README.md
└── a193121287b64721de28fcbfaec9f5919a367dd2.png
├── models
├── cnn
│ └── README.md
├── linear-regression
│ ├── GrabProblem
│ │ ├── Grab-Solution.ipynb
│ │ └── GrabProblem.ipynb
│ └── README.MD
├── logistic-regression
│ ├── README.md
│ ├── logistic-regression.ipynb
│ └── marks.txt
├── nn
│ └── README.md
├── random-forests
│ └── README.md
└── rnn
│ └── README.md
├── numpy
├── README.md
├── array.ipynb
├── broadcasting.ipynb
├── indexing&slicing.ipynb
├── iterating.ipynb
├── linear-algebra
│ ├── addition.ipynb
│ ├── norms.ipynb
│ └── products.ipynb
└── manipulation.ipynb
├── python-tutorials
├── BinarySearch.ipynb
├── BinarySearchTree.ipynb
├── DFS
│ └── DFS - Pratice.ipynb
├── DPExamples.ipynb
├── JupyterNotebookAwesome.ipynb
├── README.md
├── Recursion.ipynb
├── Trie.ipynb
├── bellman-ford.ipynb
├── binary-tree.ipynb
├── binaryTree.ipynb
├── built-in-function.ipynb
├── dictionary.ipynb
├── dijkstra.ipynb
├── function.ipynb
├── graph.ipynb
├── heap.ipynb
├── images
│ ├── 14.png
│ ├── 14_base.png
│ ├── 15.png
│ ├── 16.png
│ ├── BST
│ │ ├── 1.png
│ │ ├── 2.0.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ ├── 7.png
│ │ └── 8.png
│ ├── DP
│ │ └── fm.png
│ ├── Heap
│ │ ├── 1.png
│ │ ├── 10.png
│ │ ├── 11.png
│ │ ├── 12.png
│ │ ├── 13.png
│ │ ├── 14.png
│ │ ├── 15.png
│ │ ├── 16.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ ├── 7.png
│ │ ├── 8.png
│ │ └── 9.png
│ ├── Linkedlist
│ │ ├── 3.png
│ │ └── 5.png
│ ├── Stack
│ │ └── 1.png
│ ├── binarySearch
│ │ ├── 1.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── 5.png
│ │ └── 6.png
│ ├── dfs
│ │ └── island.png
│ └── dijkstra
│ │ ├── 1.png
│ │ ├── 11.png
│ │ ├── 12.png
│ │ ├── 13.png
│ │ ├── 2.png
│ │ ├── 3.png
│ │ ├── 4.png
│ │ ├── 5.png
│ │ ├── 6.png
│ │ ├── 7.png
│ │ ├── 8.png
│ │ └── 9.png
├── integer-float-boolean.ipynb
├── linked-list.ipynb
├── list.ipynb
├── loop.ipynb
├── queue.ipynb
├── replace.ipynb
├── set.ipynb
├── sort.ipynb
├── stack.ipynb
├── string.ipynb
└── tuple.ipynb
├── tensorflow
├── README.MD
├── constant_addition.ipynb
├── hidden_layer.ipynb
├── matrix_summation.ipynb
└── variable_addition.ipynb
├── tf2.0
├── 1.SequentialModel.ipynb
├── 1.inference.jpg
├── 2.SubclassingModel.ipynb
├── 5. Transfer Learning VGG.ipynb
├── README.MD
├── TFv2 API.ipynb
├── data-processing-keep-dataframe.ipynb
├── data-processing.ipynb
├── data
│ └── Customer_Churn_Modelling.csv
├── img
│ ├── 0.png
│ ├── arc.jpg
│ ├── arc.svg
│ ├── cifa.png
│ ├── onehot.jpeg
│ └── training.png
├── saved_model.h5
├── setup-and-first-code.ipynb
├── tf 2.0 inference.jpg
├── tf-gan.ipynb
└── traffic-signs-classification
│ ├── NgocNetForSignsClassification.ipynb
│ ├── README.MD
│ ├── img
│ ├── arch.png
│ └── data.png
│ └── ngocnet.h5
└── visualize
├── multi-dimension
└── 3D-point.ipynb
└── pandas
├── DataFrameCreateRead.ipynb
├── README.MD
├── Series.ipynb
├── dataFrameUD.ipynb
└── images
├── 1.PNG
├── 10.png
├── 11.png
├── 2-1.png
├── 2.png
├── 3.png
├── 4.png
├── 5.png
├── 6.png
├── 7.png
├── 7.pnh.png
├── 8.png
└── demo.png
/.gitignore:
--------------------------------------------------------------------------------
1 | .ipynb_checkpoints
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | ## Lộ trình học Machine Learning, Deep Learning cho người mới bắt đầu
2 |
3 | Tôi đã từng học Machine Learning trong vòng 2 tháng và tôi tin bạn cũng có thể làm được.
4 |
5 | Lộ trình sẽ giúp bạn nắm chắc công nghệ này từ cơ bản đến nâng cao, xây dựng Machine Learning model từ python thuần cho đến các thư viện cao cấp như TensorFlow hay Keras. Đi sâu phân tích bản chất vấn đề là giá trị cốt lõi của khóa học này.
6 |
7 | **P/S:** Hãy để lại **1 star** để team có động lực xuất bản các phần tiếp theo và cũng đừng quên chia sẻ tới bạn bè của bạn.
8 |
9 |
10 | Tôi là một trong các lecturer của lớp VietAI Hanoi khoá 3 và khoá 4. Hiện tại tôi đã vào Sài Gòn nên không tham gia tiếp để giảng dạy nhưng vẫn đóng vai trò Advisor cho lớp khoá 5.
11 |
12 |
13 |
14 |
15 |
16 | Ngày 10/12/2019, sau 2 vòng phỏng vấn, Google chính thức công nhận tôi là Google Developer Expert in Machine Learning đầu tiên tại Việt Nam.
17 |
18 |
19 |
20 | Tìm tôi [ở đây](https://developers.google.com/community/experts/directory).
21 |
22 | Video giới thiệu về Machine Learning:
23 |
24 |
27 |
28 | Giới thiệu về thành tựu và mục tiêu của VietAI [tại đây](https://docs.google.com/presentation/d/1A_oDWZyC6NhYPeHNrWJbESxSPUT7f0Gg-PLfXDtVKus/edit?usp=sharing).
29 |
30 |
31 | **Xem tôi trên [Youtube](https://www.youtube.com/channel/UCrZIaMmLGsq_J-4Z1Qww0cw)**
32 |
33 |
34 |
35 |
36 | ### Mục lục
37 | ----
38 | - 1. Kiến thức toán học cần thiết (Hoàn tất)
39 | - 2. Kỹ năng lập trình Python (Hoàn tất)
40 | - 3. Thư viện Numpy và TensorFlow (Hoàn tất)
41 | - 4. Hiển thị dữ liệu và các công cụ (Đang tiến hành)
42 | - [Dữ liệu bảng](./visualize/pandas)
43 | - 5. Bài toán hồi quy (Regression) (Hoàn tất)
44 | - 6. Bài toán phân loại (Classification) (Hoàn tất)
45 | - 7. Triển khai (Deploy) Machine Learning Model trên Production (Hoàn tất)
46 | - 8. Machine Learning trên trình duyệt và TensorFlowJS (Hoàn tất)
47 | - 9. Cập nhật mới nhất
48 | - [TensorFlow 2.0](https://github.com/bangoc123/learn-machine-learning-in-two-months/tree/master/tf2.0)
49 | - [Trải nghiệm PaperSpace Gradient Community](https://github.com/bangoc123/learn-machine-learning-in-two-months/tree/master/articles/GradientPaperSpace.MD)
50 | - 10. Ôn luyện thuật toán mỗi ngày (Đang tiến hành)
51 | - [Backtracking Algorithm](./algorithms/graph/backtracking/backtracking.MD)
52 | - 11. Một số ứng dụng triển khai thực tế (Đang tiến hành)
53 | - [Ứng dụng Transfer Learning](https://protonx.app/transfer-learning)
54 |
55 |
--------------------------------------------------------------------------------
/algorithms/divide-and-conquer/Untitled.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "nums = [-2,1,-3,4,-1,2,1,-5,4]"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 4,
15 | "metadata": {},
16 | "outputs": [
17 | {
18 | "data": {
19 | "text/plain": [
20 | "1"
21 | ]
22 | },
23 | "execution_count": 4,
24 | "metadata": {},
25 | "output_type": "execute_result"
26 | }
27 | ],
28 | "source": [
29 | "sum(nums)"
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": null,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": []
38 | }
39 | ],
40 | "metadata": {
41 | "kernelspec": {
42 | "display_name": "Python 3",
43 | "language": "python",
44 | "name": "python3"
45 | },
46 | "language_info": {
47 | "codemirror_mode": {
48 | "name": "ipython",
49 | "version": 3
50 | },
51 | "file_extension": ".py",
52 | "mimetype": "text/x-python",
53 | "name": "python",
54 | "nbconvert_exporter": "python",
55 | "pygments_lexer": "ipython3",
56 | "version": "3.7.4"
57 | }
58 | },
59 | "nbformat": 4,
60 | "nbformat_minor": 2
61 | }
62 |
--------------------------------------------------------------------------------
/algorithms/graph/backtracking/backtracking.MD:
--------------------------------------------------------------------------------
1 | ### Thuật toán Quay lui (Backtracking) với bài toán con mã đi hết bản đồ.
2 |
3 |
4 | 
5 |
6 | Bài toán này đặt ra một yêu cầu khá thú vị. Tìm đường cho con Mã có thể đặt chân đến tất cả các điểm trên bàn cờ với mỗi vị trí con Mã chỉ được đi đến duy nhất **một lần**.
7 |
8 | Với bài toán này chúng ta có thể áp dụng thuật toán Backtracking. Ý tưởng khá đơn giản, khi bạn đứng trước 2 con đường, bạn sẽ chọn 1 đường để đi, nếu đường đó là đường cụt, bạn **quay lại** vị trí ban đầu và đi đường thứ 2.
9 |
10 | Hình này có thể miêu tả rõ ý tưởng của thuật toán này:
11 |
12 |
13 |
14 | Thuật toán sẽ thử hết những khả năng có thể xảy ra cho đến khi mà nó đạt được mục đích của mình.
15 |
16 | Quá trình này sẽ diễn ra liên tục cho đến khi tất cả các điểm trong bàn cờ được con Mã đi đến.
17 |
18 | Với ý tưởng như vậy chúng ta sẽ giải quyết vấn đề này như sau:
19 | - Sử dụng thuật toán DFS (Depth First Search) để đi đến tất cả các vị trí có thể đi trên bàn cờ.
20 | - Nếu con Mã đi vào một vị trí mà nó không thể đi tiếp (tức là những điểm nó có thể đi từ điểm hiện tại đã được đặt chân đến rồi) thì quay lại vị trí trước đó và lựa chọn con đường khác.
21 | - Việc lặp lại này sẽ diễn ra liên tục cho đến khi tất cả các điểm trên bàn cờ được đi tới.
22 |
23 | Ví dụ với bàn cờ (8x8):
24 |
25 |
26 |
27 |
28 |
29 | Đây là thứ tự các bước đi của con Mã, nó sẽ nhảy từ 1 -> 2 -> 3 đến khi nào hết tất cả các vị trí trên bàn cờ.
30 |
31 | Tất nhiên sẽ có những kích cỡ bàn cờ mà cho dù con Mã có đi đường nào cũng không thể thoả mãn được điều kiện của chúng ta.
32 |
33 | Nào chúng ta bắt đầu khai triển thuật toán:
34 |
35 | Để cho đơn giản chúng ta có thể sử dụng ví dụ với bàn cờ **5 x 5**.
36 |
37 | 1. **Ta sẽ khởi tạo một ma trận 2 chiều như sau:**
38 |
39 | | 1 | -1 | -1 | -1 | -1 |
40 | |---|---|---|---|---|
41 | | -1 | -1 | -1 | -1 | -1 |
42 | | -1 | -1 | -1 | -1 | -1 |
43 | | -1 | -1 | -1 | -1 | -1 |
44 | | -1 | -1 | -1 | -1 | -1 |
45 |
46 |
47 | Điểm khởi đầu của con Mã chính là **0x0** tức là dòng 1 cột 1 trong ma trận tượng trưng cho bàn cờ.
48 |
49 |
50 | ```
51 | N = 5
52 | for (int i = 0; i < N; i++) {
53 | for (int j = 0; j < N; j++) {
54 | matrix[i][j] = -1;
55 | }
56 | }
57 |
58 | matrix[0][0] = 1;
59 |
60 | ```
61 |
62 | 2. **Thiết kế đường đi cho con Mã:**
63 |
64 | Nếu bạn biết chơi cờ vua thì hoàn toàn biết rằng con Mã có thể đi chéo 2 nước. Vì thế trong hàm đường đi ta sẽ thiểt kế như sau:
65 |
66 | Giả sử x là vị trí theo cột của con Mã, y là vị trí theo dòng của con Mã. thì 1 trong 2 vị trí này sẽ thay phiên nhau thay đổi 1 và 2 (Bao gồm cả cộng và trừ).
67 |
68 |
69 |
70 |
71 |
72 | Ví dụ trong trường hợp này nếu vị trí của dấu **X** là (x,y) thì vị trí của con Mã sẽ là (x+2, y-1).
73 |
74 | Dựa vào tính chất này ta có thể liêt kê nhiều nhất 8 điểm mà một con Mã có thể đi được đến từ một điểm trong bàn cờ.
75 |
76 | ```
77 | int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
78 | int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
79 |
80 | ```
81 |
82 | Ta lưu những vị trí x và y khả dĩ tương ứng vào 2 mảng, Với 1 vị trí tương ứng trong 2 mảng thì đó chính là một khả năng thay đổi vị trí của con Mã.
83 |
84 | Nếu ta thực hiện vòng lặp đồng thời qua 2 mảng này thì con Mã sẽ đi được đến 8 điểm ta vừa nhắc tới.
85 |
86 | ```
87 | for (int i=0; i < 8; i++) {
88 | int nextX = x + xMove[i];
89 | int nextY = y + yMove[i];
90 | }
91 |
92 | ```
93 |
94 | Và để đảm bảo điểm tiếp đến là thuộc bàn cờ ta cần kiểm tra rằng nó nằm trong bàn cờ với hàm sau:
95 |
96 | ```
97 | bool isSafe(int x, int y) {
98 | return x >= 0 && y >= 0 && x <= N - 1 && y <= N-1;
99 | }
100 | ```
101 |
102 | Nếu mà điểm tới mà không thuộc bàn cờ thì con Mã sẽ không đi tới đó.
103 |
104 | 3. **Tạo hàm đường đi**
105 |
106 | Chúng ta sử dụng thuật toán DFS để tiến hành đi đường cho con Mã. Chú ý hàm DFS sẽ luôn có một mảng để đánh dấu những điểm đi qua để tránh trường hợp con Mã đi lại những điểm đã đi qua.
107 |
108 | Trong bài này ta sử dụng chính giá trị của từng ô để đánh dấu là đã đi qua điểm đó:
109 |
110 | ```
111 | if (isSafe(nextX, nextY)) {
112 | if (matrix[nextX][nextY] == -1) {
113 | matrix[nextX][nextY] = cnt + 1;
114 | if (dfs(nextX, nextY, cnt+1) == 1) {
115 | return 1;
116 | } else {
117 | matrix[nextX][nextY] = -1; // Solution 1.
118 | }
119 | }
120 |
121 | }
122 |
123 | ```
124 |
125 | Giải thích một chút:
126 |
127 | ```
128 | if (isSafe(nextX, nextY)) {
129 | if (matrix[nextX][nextY] == -1) {
130 | matrix[nextX][nextY] = cnt + 1;
131 | ...
132 | }
133 | }
134 |
135 | ```
136 |
137 | Hàm này kiểm tra điểm đến tiếp theo sẽ nằm trong bản đồ và chưa từng được bước tới. Những điểm chưa từng được đặt chân tới sẽ được đánh dấu -1 như ban đầu chúng ta đã biết.
138 |
139 | Và ngay sau đó nếu điểm đó thoả mãn cả 2 địa điểm trên thì ta đánh dấu cho nó bằng chính số bước cần đi để tới được đó. Phần này bạn sẽ hiểu rõ hơn nếu đọc kỹ biến cnt từ đầu.
140 |
141 | Tiếp theo để đơn giản hoá việc lập trình ta sử dụng đệ quy để đi đến các điểm tiếp theo. Và **ma thuật chính xuất hiện từ đây**. Vì hàm đệ quy có tính chất **quay ngược lại** (backtracking), nên nếu trong trường hợp con Mã đi vào đường cụt, dựa vào việc hàm đệ quy trả về 0, ta đánh dấu điểm đi qua từ vị trí trước là **chưa đi qua**.
142 |
143 | ```
144 | if (dfs(nextX, nextY, cnt+1) == 1) {
145 | return 1;
146 | } else {
147 | matrix[nextX][nextY] = -1; // Solution 1.
148 | }
149 | ```
150 |
151 | Tại sao phải làm vậy? Nếu mà ta đi vào đường cụt nhưng ta vẫn đánh dấu ta đã đi qua thì nếu ta lại vị trí trước để đi đường tiếp theo có thể ta đã bỏ qua một số điểm mà chúng có thể là điểm nằm trên đường đi cần tìm.
152 |
153 | Nếu có thời gian tôi sẽ tạo ảnh động để cho các bạn hiểu rõ hơn nhưng tôi nghĩ đến đây bạn đã có thể tượng tượng phần nào.
154 |
155 | Vậy khi nào hàm trả về 1? Tức là tìm ra con đường? Trường hợp này sẽ chính là khi mà số bước từ điểm ban đầu đến vị trí hiện tại chính bằng kích thước của bàn cờ. Ta có thể kết luận con Mã đa đi qua tất cả các điểm trên bàn cờ.
156 |
157 | Đây là toàn bộ hàm tìm đường của chúng ta:
158 |
159 | ```
160 | int dfs(int x, int y, int cnt) {
161 |
162 |
163 | int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
164 | int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
165 |
166 | for (int i=0; i < 8; i++) {
167 | int nextX = x + xMove[i];
168 | int nextY = y + yMove[i];
169 |
170 | if (cnt == N*N) {
171 | return 1;
172 | }
173 |
174 | if (isSafe(nextX, nextY)) {
175 | if (matrix[nextX][nextY] == -1) {
176 | matrix[nextX][nextY] = cnt + 1;
177 | if (dfs(nextX, nextY, cnt+1) == 1) {
178 | return 1;
179 | } else {
180 | matrix[nextX][nextY] = -1; // Solution 1.
181 | }
182 | }
183 |
184 | }
185 |
186 | }
187 |
188 | return 0;
189 | }
190 |
191 | ```
192 |
193 | Chúng ta có thể đặt việc backtracking ở trước chỗ **return 0**.
194 |
195 | Và đây là kết quả của chúng ta sau khi chạy thuật toán này:
196 |
197 |
198 |
199 | Toàn bộ phần trình bày bên trên là một cách giải bài toán này dựa vào thuật toán **Backtracking**. Tôi sẽ áp dụng thuật toán này để giải nhiều bài hơn nữa.
200 |
201 | [Source code đầy đủ ở đây.](./main.cpp)
202 |
203 |
204 |
205 |
206 |
207 |
208 |
209 |
210 |
211 |
212 |
213 |
214 |
215 |
216 |
217 |
218 |
219 |
220 |
221 |
--------------------------------------------------------------------------------
/algorithms/graph/backtracking/main.cpp:
--------------------------------------------------------------------------------
1 | //
2 | // main.cpp
3 | // Knight’s tour
4 | //
5 | // Created by Ngoc Nguyen on 11/5/19.
6 | // Copyright © 2019 Ngoc Nguyen. All rights reserved.
7 | //
8 |
9 | // https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
10 |
11 | #include
12 | #define N 5
13 | using namespace std;
14 |
15 | int matrix[N][N];
16 |
17 | bool isSafe(int x, int y) {
18 | return x >= 0 && y >= 0 && x <= N - 1 && y <= N-1;
19 | }
20 |
21 | int dfs(int x, int y, int cnt) {
22 |
23 |
24 | int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
25 | int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
26 |
27 | for (int i=0; i < 8; i++) {
28 | int nextX = x + xMove[i];
29 | int nextY = y + yMove[i];
30 |
31 | if (cnt == N*N) {
32 | return 1;
33 | }
34 |
35 | if (isSafe(nextX, nextY)) {
36 | if (matrix[nextX][nextY] == -1) {
37 | matrix[nextX][nextY] = cnt + 1;
38 | if (dfs(nextX, nextY, cnt+1) == 1) {
39 | return 1;
40 | } else {
41 | matrix[nextX][nextY] = -1; // Solution 1.
42 | }
43 | }
44 |
45 | }
46 |
47 | }
48 | // matrix[x][y] = -1; // Solution 2.
49 | return 0;
50 | }
51 |
52 | int main(int argc, const char * argv[]) {
53 | // insert code here...
54 | std::cout << "The Knight’s tour problem\n";
55 |
56 | for (int i = 0; i < N; i++) {
57 | for (int j = 0; j < N; j++) {
58 | matrix[i][j] = -1;
59 | }
60 | }
61 | matrix[0][0] = 1;
62 | dfs(0, 0, 1);
63 |
64 |
65 | for (int i = 0; i < N; i++) {
66 | for (int j = 0; j < N; j++) {
67 | cout << matrix[i][j] << "-";
68 | }
69 |
70 | cout << "\n" << endl;
71 | }
72 |
73 | return 0;
74 | }
75 |
--------------------------------------------------------------------------------
/algorithms/imgs/75435905_1500043306811247_1920143822067597312_n.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/algorithms/imgs/75435905_1500043306811247_1920143822067597312_n.png
--------------------------------------------------------------------------------
/algorithms/imgs/Knights-Tour-Animation.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/algorithms/imgs/Knights-Tour-Animation.gif
--------------------------------------------------------------------------------
/algorithms/imgs/godemo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/algorithms/imgs/godemo.png
--------------------------------------------------------------------------------
/algorithms/imgs/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/algorithms/imgs/result.png
--------------------------------------------------------------------------------
/algorithms/imgs/visual.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/algorithms/imgs/visual.png
--------------------------------------------------------------------------------
/articles/GradientPaperSpace.MD:
--------------------------------------------------------------------------------
1 | ## Một số trải nghiệm lần đầu khi sử dụng PaperSpace Gradient
2 |
3 |
4 | Chính xác là ngày 11/10/2019, PaperSpace có công bố chương trình [Gradient Community Notebooks](https://blog.paperspace.com/paperspace-launches-gradient-community-notebooks/).
5 |
6 | 3 loại GPU được free đó là:
7 |
8 | - Free-CPU — C3 CPU instance
9 | - Free-GPU+ — NVIDIA M4000 GPU
10 | - Free-P5000 — NVIDIA P5000 GPU
11 |
12 |
13 | Nào chúng ta cùng sử gói free P5000, rõ ràng P5000 hiệu năng tốt hơn M4000 nên chúng ta sẽ chọn GPU này để test trước.
14 |
15 | Link so sánh [ở đây](https://gpu.userbenchmark.com/Compare/Nvidia-Quadro-P5000-vs-Nvidia-Quadro-M4000/m197331vsm37687).
16 |
17 |
18 | #### Bước 1: Tạo tài khoản [PaperSpace](https://www.paperspace.com/)
19 |
20 | Bước này đơn giản rồi nên tôi không đề cập nhiều, chú ý bạn cần thẻ visa/master để đăng ký sử dụng tài nguyên. Việc này chỉ để xác nhận tài khoản của bạn, việc sử dụng free GPU không tốn bất cứ một chi phí nào. Nhưng hay đảm bảo tắt hết mọi thứ trước khi logout để tránh trường hợp xấu nha bạn. :D
21 |
22 | Tôi đã tạo tài khoản PaperSpace rất lâu rồi và hi vọng một ngày nào đó free GPU và ngày đó đã đến. :D
23 |
24 |
25 | #### Bước 2: Truy cập vào [PaperSpace Gradient](https://www.paperspace.com/console/gradient)
26 |
27 |
28 |
29 | Chọn Run một Jupyter Notebook
30 |
31 | #### Bước 3: Chọn một Framework để train model
32 |
33 |
34 |
35 | #### Bước 4: Chọn free GPU mà bạn muốn.
36 |
37 |
38 |
39 | Một điều đáng tiếc đó là chúng ta chỉ có thể chạy một Notebook tại một thời điểm duy nhất và 6 tiếng sau notebook sẽ tự động tắt.
40 |
41 | Điểm này thì Colab của Google vượt trội hơn hẳn khi bạn có thể bật bao nhiêu Notebook tuỳ thích tại cùng một thời điểm. Nhưng mà chú ý rằng khi mà chúng ta chạy 2 Notebook trên Colab, bản chất Colab chạy cả 2 notebooks trên cùng một container được nối với gmail của bạn. Lợi thế đây chỉ là tiện ích sử dụng mà không phải là lợi thế về tài nguyên.
42 |
43 |
44 |
45 |
46 | Notebook sẽ bị tắt sau 6 tiếng, điểm trừ khá lớn đây khi Colab là 12 tiếng. :(
47 |
48 | #### Bước 5: Mở Notebook
49 |
50 |
51 |
52 | Một điểm trừ tiếp theo là việc load notebook khá lâu, lúc đầu tôi tưởng hệ thống bị đứng nhưng thực chất hệ thống đang load, hi vọng tình trạng này sẽ được cải thiện trong tương lai.
53 |
54 |
55 |
56 | Một chút về giao diện: Thank Bar bên trên khá lớn khiến không gian để code bị giảm đi.
57 |
58 |
59 | #### Bước 6: Kiểm tra GPU
60 |
61 | ```
62 | from tensorflow.python.client import device_lib
63 |
64 | device_lib.list_local_devices()
65 | ```
66 |
67 |
68 |
69 | Yeah số hiệu P5000 đã hiện ra.
70 |
71 | Giao diện thao tác:
72 |
73 |
74 |
75 |
76 | Ở đây chúng ta có thể thấy sự học hỏi khá nhiều từ Colab trong tính năng quản lý thư mục và files. Tôi còn nhớ những thời điểm đầu sử dụng Colab, system này còn chưa có taskbar quản lý file, phải rất vất vả mới kết nối được Google Drive với container của Colab. :(
77 |
78 | Đây là [bài viết](http://bit.ly/2PgQwl5) rất lâu rồi mà tôi đã từng viết về setting Colab từ những ngày đầu tiên.
79 |
80 | Tôi có test tốc độ upload file của hệ thống này nhưng cũng khá chậm không có gì quá đặc sắc. Vì thế lựa chọn tốt vẫn là kéo dataset từ Internet về, với 300GB SSD cũng không phải con số quá lớn với Deep Learning.
81 |
82 |
83 | #### Bước 7: Tiến hành Training
84 |
85 | Điểm cộng lớn nhất ở đây là tốc độ training khá ấn tượng
86 |
87 |
88 |
89 | Chỉ cần 20.82s để có thể train xong 60000 ảnh của bộ dataset MNIST trong 5 epochs. Tôi có cảm giác nhanh hơn K80 ở Colab một chút, yếu tố này có thể phụ thuộc vào lượng người dùng hiện có của 2 bên nên để đưa ra kết luận cuối cùng thì cần một thời gian dài để sử dụng.
90 |
91 | Code training nằm [ở đây](https://www.tensorflow.org/tutorials/quickstart/beginner).
92 |
93 | Đây là những cảm nhận đầu tiên của tôi khi sử dụng nền tảng này. Rõ ràng việc thiếu tài nguyên là rào cản lớn cho những người yêu thích học và thực hành AI. Cho nên việc các công ty lớn công bố những chương trình hỗ trợ Community là hoàn toàn giá trị và ý nghĩa cho sự phát triển chung của ngày công nghệ tương lai này.
94 |
95 | Việc còn nhiều thiếu sót là không tránh khỏi nên hi vọng chúng ta có thể ủng hộ và góp ý cho sản phẩm này hoàn thiện hơn.
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
--------------------------------------------------------------------------------
/articles/YOLO.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 3,
6 | "metadata": {},
7 | "outputs": [
8 | {
9 | "ename": "ModuleNotFoundError",
10 | "evalue": "No module named 'utils'",
11 | "output_type": "error",
12 | "traceback": [
13 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
14 | "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
15 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# from object_detection.utils import ops as utils_ops\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mlabel_map_util\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mvisualization_utils\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mvis_util\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
16 | "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'utils'"
17 | ]
18 | }
19 | ],
20 | "source": [
21 | "import numpy as np\n",
22 | "import os\n",
23 | "import six.moves.urllib as urllib\n",
24 | "import sys\n",
25 | "import tarfile\n",
26 | "import tensorflow as tf\n",
27 | "import zipfile\n",
28 | " \n",
29 | "from collections import defaultdict\n",
30 | "from io import StringIO\n",
31 | "from matplotlib import pyplot as plt\n",
32 | "from PIL import Image\n",
33 | " \n",
34 | "sys.path.append(\"..\")\n",
35 | "# from object_detection.utils import ops as utils_ops\n",
36 | " \n",
37 | "from utils import label_map_util\n",
38 | " \n",
39 | "from utils import visualization_utils as vis_util"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": null,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": [
48 | "3\n"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": null,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": []
57 | }
58 | ],
59 | "metadata": {
60 | "kernelspec": {
61 | "display_name": "Python 3",
62 | "language": "python",
63 | "name": "python3"
64 | },
65 | "language_info": {
66 | "codemirror_mode": {
67 | "name": "ipython",
68 | "version": 3
69 | },
70 | "file_extension": ".py",
71 | "mimetype": "text/x-python",
72 | "name": "python",
73 | "nbconvert_exporter": "python",
74 | "pygments_lexer": "ipython3",
75 | "version": "3.7.4"
76 | }
77 | },
78 | "nbformat": 4,
79 | "nbformat_minor": 2
80 | }
81 |
--------------------------------------------------------------------------------
/computer-vision/README.MD:
--------------------------------------------------------------------------------
1 | ## Thị giác Máy tính (Computer Vision - CV)
2 |
3 | ### 1. Thị giác máy tính là gì?
4 |
5 |
6 |
7 | Ảnh từ [CS231](http://vision.stanford.edu/teaching/cs131_fall1819/index.html)
8 |
9 | Mục tiêu của ngành nghiên cứu này là viết ra những chương trình có thể hiểu được với hình ảnh.
10 |
11 | Khởi đầu của môn học này là một project hè ở MIT.
12 |
13 | 
14 |
15 | Vậy những loại data nào có thể trích xuất từ một hình ảnh:
16 |
17 | - Ma trận 3D
18 | - Ngữ nghĩa của một hình ảnh
19 |
20 | Ví dụ về Image Captioning
21 |
22 | 
23 |
24 | ### 2. Pixel và bộ lọc
25 |
26 | #### 2.1. Bức ảnh giống như một hàm
27 |
28 | Một bức ảnh bao gồm rất nhiều pixel. Một pixel có thể thể hiện cường độ của 3 màu: đỏ, xanh lục và xanh dương (R - Red, G - Green, B - Blue).
29 |
30 | Một bức ảnh là một hàm
chạy từ
cho đến
31 |
32 | -
trả về cường độ tại vị trí
33 |
34 | - Được giới hạn trong một hình chữ nhật hữu hạn các cạnh a, b, c, d. Mỗi một pixel trong miền giới hạn đó có giá trị dao động từ 0 đến 255.
35 |
36 |
37 |
38 | #### 2.1. Biểu diễn ảnh bằng Ma trận
39 |
40 |
41 |
42 |
43 |
44 | Hình ảnh từ [Lecture 4 - CS131 - Stanford](http://vision.stanford.edu/teaching/cs131_fall1617/lectures/lecture4_pixels%20and%20filters_cs131_2016.pdf)
45 |
46 | Các bạn có thể thấy mỗi pixel có thể là một hàm độc lập, và tập hợp các hàm độc lập này tạo nên một bức ảnh.
47 |
48 |
--------------------------------------------------------------------------------
/data-processing/hanoi.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/data-processing/hanoi.jpg
--------------------------------------------------------------------------------
/deployment/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/deployment/.DS_Store
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/deployment/distributed-tensorflow/.DS_Store
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/README.md:
--------------------------------------------------------------------------------
1 | ### 11. Triển khai (Deploy) Machine Learning Model trên Production
2 | ----
3 | **a. TensorFlow Serving**
4 | - [Giới thiệu TensorFlow Serving](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/deployment/distributed-tensorflow/TensorFlowServing.ipynb)
5 | - Sự khác nhau giữa Training và Inference
6 | - TensorFlow Serving Architect
7 | - [Triển khai Một Model train sẵn với TensorFlow Serving](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/deployment/distributed-tensorflow/PretrainedModelDeployment.ipynb)
8 | - [Trích xuất Graph từ Model được train và Deploy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/deployment/distributed-tensorflow/GraphDeployment.ipynb)
9 |
10 |
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/TensorFlowServing.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "#### 1. Sự khác nhau giữa Training và Inference\n",
8 | "\n"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "
"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "Ảnh từ [Gartner Blog Network](https://blogs.gartner.com/paul-debeasi/2019/02/14/training-versus-inference/)"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "- **Quá trình training:** Dựa trên tập data được chuẩn bị sẵn trước, train và test một hoặc nhiều models thực hiện các mục đích khác nhau, ví dụ như một model dùng để phân biệt ảnh chó và ảnh mèo như hình bên trên."
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "- **Quá trình inference:** Khi model đã được train xong, ta cần thiết kế flow để có thể sử dụng cho một hoặc nhiều data mới. Flow này sẽ bao gồm phần nhận input là một dữ liệu mới và trả về ouput là kết quả dự đoán của model."
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "Nếu bạn là một lập trình viên AI thì mục tiêu không chỉ là train được một model mà còn phải deploy được quá trình inference này dưới nhiều dạng khác nhau và điển hình nhất [Restful API](https://searchmicroservices.techtarget.com/definition/RESTful-API)."
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "Thật may mắn là Google đã cho ra mắt Tensorflow Serving và họ cũng dùng nó cho Production của họ, vì thế bạn hoàn toàn có thể tin tưởng rằng cấu trúc này có thể phục vụ được hàng tỉ người dùng. :3"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "#### 2. Kiến trúc TensorFlow Serving"
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "Chú ý rằng TensorFlow Serving là thư viện tách biệt hoàn toàn với Tensorflow nhé."
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "
"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "Ảnh từ [Tensorflow Website](https://www.tensorflow.org/tfx/serving/architecture)"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "**4 thành phần chính:**\n",
86 | "\n",
87 | "Phần này chủ yếu dành để tham khảo vì hầu như quy trình này được TensorFlow Serving thực hiện tự động\n",
88 | "\n",
89 | "- **Servables:** Abstract Class dùng để quản lý các models của bạn, bạn có thể quản lý nhiều models hoặc một model với nhiều phiên bản khác nhau.\n",
90 | "- **Source:** Nơi lưu trữ files/data\n",
91 | "- **Manager:** \n",
92 | " + quản lý full lifecycle của một Servables (có loading, serving và unloading)\n",
93 | " + lắng nghe Source và kiểm tra tất cả các phiên bản\n",
94 | "- **Loader:** \n",
95 | " + Cho phép sử dụng một số hạ tầng thông thường, định nghĩa API quy chuẩn\n",
96 | " \n",
97 | "**2 bước chính:**\n",
98 | "- Sources tạo ra Loader cho một phiên bản Servable\n",
99 | "- Loader gửi phiên bản này cho Manager, phần phụ trách load và phục vụ người dùng"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": null,
105 | "metadata": {},
106 | "outputs": [],
107 | "source": []
108 | }
109 | ],
110 | "metadata": {
111 | "kernelspec": {
112 | "display_name": "Python 3",
113 | "language": "python",
114 | "name": "python3"
115 | },
116 | "language_info": {
117 | "codemirror_mode": {
118 | "name": "ipython",
119 | "version": 3
120 | },
121 | "file_extension": ".py",
122 | "mimetype": "text/x-python",
123 | "name": "python",
124 | "nbconvert_exporter": "python",
125 | "pygments_lexer": "ipython3",
126 | "version": "3.7.4"
127 | }
128 | },
129 | "nbformat": 4,
130 | "nbformat_minor": 2
131 | }
132 |
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/images/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/deployment/distributed-tensorflow/images/.DS_Store
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/images/Train-versus-Inference.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/deployment/distributed-tensorflow/images/Train-versus-Inference.png
--------------------------------------------------------------------------------
/deployment/distributed-tensorflow/images/serving_architecture.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/deployment/distributed-tensorflow/images/serving_architecture.png
--------------------------------------------------------------------------------
/deployment/tensorflow-browser/README.md:
--------------------------------------------------------------------------------
1 | ### 11. Machine Learning trên trình duyệt và TensorFlowJS
2 | ----
3 |
4 | Phần này được mô phỏng chủ yếu trên bài nói của tôi tại Google I/O Extended Hanoi 2018. Slide và code của bài nói này nằm [tại đây](https://github.com/bangoc123/tensorflow-js).
5 |
6 | - **a. Lịch sử TensorFlowJS**
7 | - **b. Các API chính của TensorFlowJS**
8 | - **c. Xây dựng mô hình Hồi quy tuyến tính**
9 | - **d. Xây dựng mạng Convolutional Neural Network(CNN)**
10 | - **e. MobileNet**
11 | - **f. Transfer Learning**
--------------------------------------------------------------------------------
/gan/README.MD:
--------------------------------------------------------------------------------
1 | ### 11. Mô hình sinh, GAN và CycleGAN
2 | ----
3 |
4 | **a. Mô hình sinh**
5 | - Giới thiệu về mô hình sinh
6 | - Mộ số ứng dụng
7 |
8 | **b. GAN**
9 | - Giới thiệu GAN
10 | - Train model GAN
11 |
12 | **c. CycleGAN**
13 |
14 | - Giới thiệu CycleGAN
15 | - [Train Model CycleGAN](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/gan/cyclegan/CycleGAN.ipynb)
16 |
--------------------------------------------------------------------------------
/gan/cyclegan/images/Generator.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/gan/cyclegan/images/Generator.jpg
--------------------------------------------------------------------------------
/gan/cyclegan/images/Resnet.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/gan/cyclegan/images/Resnet.jpg
--------------------------------------------------------------------------------
/gan/cyclegan/images/styletransfer.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/gan/cyclegan/images/styletransfer.jpeg
--------------------------------------------------------------------------------
/images/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/.DS_Store
--------------------------------------------------------------------------------
/images/Logistic .jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/Logistic .jpg
--------------------------------------------------------------------------------
/images/Recursion Algorithm_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/Recursion Algorithm_2.png
--------------------------------------------------------------------------------
/images/Recursion_Algorithm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/Recursion_Algorithm.png
--------------------------------------------------------------------------------
/images/class.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/class.jpg
--------------------------------------------------------------------------------
/images/gde.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/gde.png
--------------------------------------------------------------------------------
/images/logis.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/logis.gif
--------------------------------------------------------------------------------
/images/logmu.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/logmu.png
--------------------------------------------------------------------------------
/images/pp1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp1.png
--------------------------------------------------------------------------------
/images/pp10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp10.png
--------------------------------------------------------------------------------
/images/pp2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp2.png
--------------------------------------------------------------------------------
/images/pp3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp3.png
--------------------------------------------------------------------------------
/images/pp4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp4.png
--------------------------------------------------------------------------------
/images/pp5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp5.png
--------------------------------------------------------------------------------
/images/pp6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp6.png
--------------------------------------------------------------------------------
/images/pp7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp7.png
--------------------------------------------------------------------------------
/images/pp8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp8.png
--------------------------------------------------------------------------------
/images/pp9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/pp9.png
--------------------------------------------------------------------------------
/images/softmax.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/softmax.png
--------------------------------------------------------------------------------
/images/transfer.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/transfer.png
--------------------------------------------------------------------------------
/images/youtube.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/images/youtube.png
--------------------------------------------------------------------------------
/math/README.md:
--------------------------------------------------------------------------------
1 | ### 1. Kiến thức toán học cần thiết
2 | ----
3 | - **a. Đại số tuyến tính**
4 |
5 | Nếu các bạn có nhiều thời gian và sự kiên trì thì có thể học toàn bộ khóa này [MIT 18.06 Linear Algebra, Spring 2005
6 | ](https://www.youtube.com/watch?v=ZK3O402wf1c&list=PLE7DDD91010BC51F8). Nhưng đối với bản thân tôi có những phần trong khóa này đến bây giờ tôi vẫn chưa dùng đến. Vì thế nếu **bạn không có nhiều thời gian, muốn tăng nhanh tốc độ** thì có thể học theo từng phần tôi nhấn mạnh ở phía dưới đây.
7 | - **Scalar/Vector**
8 | - Giới thiệu vector/scalar và các thành phần của chúng: [Vector basics - Khan Academy](https://www.khanacademy.org/math/precalculus/vectors-precalc#vector-basic)
9 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/array.ipynb)
10 | - **Ma trận (Matrix)**
11 | - Giới thiệu về ma trận: [Introduction to matrices - Khan Academy](https://www.khanacademy.org/math/precalculus/precalc-matrices#intro-to-matrices)
12 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/array.ipynb)
13 | - **Chuyển vị ma trận**
14 | - Cách chuyển vị ma trận và những vấn đề liên quan: [Transpose of a matrix - Khan Academy](https://www.khanacademy.org/math/linear-algebra/matrix-transformations#matrix-transpose)
15 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/manipulation.ipynb)
16 | - **Norm Vector**
17 | - Norm L1/L2: [Vector Norms](https://youtu.be/5fN2J8wYnfw)
18 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/norms.ipynb)
19 | - **Tensor**
20 | - Giới thiệu về Tensor: [Tensors for Beginners 0: Tensor Definition](https://youtu.be/TvxmkZmBa-k)
21 | - **Các phép toán với ma trận**
22 | - Phép cộng ma trận
23 | - Phương pháp cộng/trừ ma trận: [Matrix addition and subtraction | Matrices | Precalculus | Khan Academy
24 | ](https://youtu.be/WR9qCSXJlyY)
25 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/addition.ipynb)
26 | - Phép nhân ma trận
27 | - Các phương pháp nhân ma trận: [Lec 3 | MIT 18.06 Linear Algebra, Spring 2005](https://youtu.be/FX4C-JpTFgY)
28 | - [Thực hành với Numpy](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/products.ipynb)
29 | - Tích Hadamard/Element-Wise
30 | - Phương pháp tính tích Element-Wise: [Element-Wise Multiplication and Division of Matrices
31 | ](https://youtu.be/2GPZlRVhQWY)
32 | - **Ma trận đơn vị**
33 | - Miêu tả ma trận đơn vị: [Identity matrix | Matrices | Precalculus | Khan Academy
34 | ](https://youtu.be/3cnIa0fYJkY)
35 | - **Ma trận nghịch đảo**
36 | - Phương pháp tính ma trận nghịch đảo: [Lec 3 | MIT 18.06 Linear Algebra, Spring 2005](https://youtu.be/FX4C-JpTFgY?t=21m14s)
37 | - **b. Đạo hàm**
38 |
39 | - Đây là series kinh điển để nhắc lại kiến thức đạo hàm của bạn. [Essence of calculus - 3Blue1Brown](https://www.youtube.com/watch?v=WUvTyaaNkzM&list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr)
40 |
41 | - Anh Tiệp có một bài rất đầy đủ về đạo hàm ở đây: [Machine Learning cơ bản - Phần Toán](https://machinelearningcoban.com/math/). Hãy thực hành tính toán phần 3.5, tôi đảm bảo bạn sẽ nắm chắc được đạo hàm trên vector và ma trận.
42 | - Ngoài phần cơ bản quan trọng thì việc nắm thuần thục **Chain Rule** và **Production Rule** là rất quan trọng đặc biệt là dành cho thuật toán **Backpropagation** trong **Deep Learing**. Bạn hãy xem kỹ video này: [Visualizing the chain rule and product rule | Essence of calculus, chapter 4
43 | ](https://youtu.be/YG15m2VwSjA?list=PLZHQObOWTQDMsr9K-rj53DwVRMYO3t5Yr)
44 |
45 | - **c. Lý thuyết xác suất**
46 | - **Các khái niệm cơ bản**
47 | - Những khái niệm cơ bản: [Basic theoretical probability](https://www.khanacademy.org/math/statistics-probability/probability-library#basic-theoretical-probability)
48 | - Xác suất sử dụng không gian mẫu: [Probability using sample spaces](https://www.khanacademy.org/math/statistics-probability/probability-library#probability-sample-spaces)
49 | - Tiên đề xác suất: [Axioms of Probability](https://youtu.be/xuv6BCR-iNc)
50 | - Các loại xác suất
51 | - Xác suất có điều kiện:
52 | - Giới thiệu: [Dependent probability introduction | Probability and Statistics | Khan Academy
53 | ](https://youtu.be/VjLEoo3hIoM)
54 | - Các ví dụ liên quan: [Dependent probability example | Probability and Statistics | Khan Academy
55 | ](https://youtu.be/xPUm5SUVzTE)
56 | - Công thức Bayes: [CRITICAL THINKING - Fundamentals: Bayes' Theorem](https://youtu.be/OqmJhPQYRc8)
57 | - Xác suất độc lập: [Compound probability of independent events | Probability and Statistics | Khan Academy
58 | ](https://youtu.be/xSc4oLA9e8o)
59 | - Biến ngẫu nhiên và phân phối xác suất: [Biến ngẫu nhiên và phân phối xác suất](https://dominhhai.github.io/vi/2017/10/prob-rand-var/#2-1-h%C3%A0m-kh%E1%BB%91i-x%C3%A1c-su%E1%BA%A5t-c%E1%BB%A7a-bi%E1%BA%BFn-r%E1%BB%9Di-r%E1%BA%A1c)
--------------------------------------------------------------------------------
/math/a193121287b64721de28fcbfaec9f5919a367dd2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/math/a193121287b64721de28fcbfaec9f5919a367dd2.png
--------------------------------------------------------------------------------
/models/logistic-regression/marks.txt:
--------------------------------------------------------------------------------
1 | 34.62365962451697,78.0246928153624,0
2 | 30.28671076822607,43.89499752400101,0
3 | 35.84740876993872,72.90219802708364,0
4 | 60.18259938620976,86.30855209546826,1
5 | 79.0327360507101,75.3443764369103,1
6 | 45.08327747668339,56.3163717815305,0
7 | 61.10666453684766,96.51142588489624,1
8 | 75.02474556738889,46.55401354116538,1
9 | 76.09878670226257,87.42056971926803,1
10 | 84.43281996120035,43.53339331072109,1
11 | 95.86155507093572,38.22527805795094,0
12 | 75.01365838958247,30.60326323428011,0
13 | 82.30705337399482,76.48196330235604,1
14 | 69.36458875970939,97.71869196188608,1
15 | 39.53833914367223,76.03681085115882,0
16 | 53.9710521485623,89.20735013750205,1
17 | 69.07014406283025,52.74046973016765,1
18 | 67.94685547711617,46.67857410673128,0
19 | 70.66150955499435,92.92713789364831,1
20 | 76.97878372747498,47.57596364975532,1
21 | 67.37202754570876,42.83843832029179,0
22 | 89.67677575072079,65.79936592745237,1
23 | 50.534788289883,48.85581152764205,0
24 | 34.21206097786789,44.20952859866288,0
25 | 77.9240914545704,68.9723599933059,1
26 | 62.27101367004632,69.95445795447587,1
27 | 80.1901807509566,44.82162893218353,1
28 | 93.114388797442,38.80067033713209,0
29 | 61.83020602312595,50.25610789244621,0
30 | 38.78580379679423,64.99568095539578,0
31 | 61.379289447425,72.80788731317097,1
32 | 85.40451939411645,57.05198397627122,1
33 | 52.10797973193984,63.12762376881715,0
34 | 52.04540476831827,69.43286012045222,1
35 | 40.23689373545111,71.16774802184875,0
36 | 54.63510555424817,52.21388588061123,0
37 | 33.91550010906887,98.86943574220611,0
38 | 64.17698887494485,80.90806058670817,1
39 | 74.78925295941542,41.57341522824434,0
40 | 34.1836400264419,75.2377203360134,0
41 | 83.90239366249155,56.30804621605327,1
42 | 51.54772026906181,46.85629026349976,0
43 | 94.44336776917852,65.56892160559052,1
44 | 82.36875375713919,40.61825515970618,0
45 | 51.04775177128865,45.82270145776001,0
46 | 62.22267576120188,52.06099194836679,0
47 | 77.19303492601364,70.45820000180959,1
48 | 97.77159928000232,86.7278223300282,1
49 | 62.07306379667647,96.76882412413983,1
50 | 91.56497449807442,88.69629254546599,1
51 | 79.94481794066932,74.16311935043758,1
52 | 99.2725269292572,60.99903099844988,1
53 | 90.54671411399852,43.39060180650027,1
54 | 34.52451385320009,60.39634245837173,0
55 | 50.2864961189907,49.80453881323059,0
56 | 49.58667721632031,59.80895099453265,0
57 | 97.64563396007767,68.86157272420604,1
58 | 32.57720016809309,95.59854761387875,0
59 | 74.24869136721598,69.82457122657193,1
60 | 71.79646205863379,78.45356224515052,1
61 | 75.3956114656803,85.75993667331619,1
62 | 35.28611281526193,47.02051394723416,0
63 | 56.25381749711624,39.26147251058019,0
64 | 30.05882244669796,49.59297386723685,0
65 | 44.66826172480893,66.45008614558913,0
66 | 66.56089447242954,41.09209807936973,0
67 | 40.45755098375164,97.53518548909936,1
68 | 49.07256321908844,51.88321182073966,0
69 | 80.27957401466998,92.11606081344084,1
70 | 66.74671856944039,60.99139402740988,1
71 | 32.72283304060323,43.30717306430063,0
72 | 64.0393204150601,78.03168802018232,1
73 | 72.34649422579923,96.22759296761404,1
74 | 60.45788573918959,73.09499809758037,1
75 | 58.84095621726802,75.85844831279042,1
76 | 99.82785779692128,72.36925193383885,1
77 | 47.26426910848174,88.47586499559782,1
78 | 50.45815980285988,75.80985952982456,1
79 | 60.45555629271532,42.50840943572217,0
80 | 82.22666157785568,42.71987853716458,0
81 | 88.9138964166533,69.80378889835472,1
82 | 94.83450672430196,45.69430680250754,1
83 | 67.31925746917527,66.58935317747915,1
84 | 57.23870631569862,59.51428198012956,1
85 | 80.36675600171273,90.96014789746954,1
86 | 68.46852178591112,85.59430710452014,1
87 | 42.0754545384731,78.84478600148043,0
88 | 75.47770200533905,90.42453899753964,1
89 | 78.63542434898018,96.64742716885644,1
90 | 52.34800398794107,60.76950525602592,0
91 | 94.09433112516793,77.15910509073893,1
92 | 90.44855097096364,87.50879176484702,1
93 | 55.48216114069585,35.57070347228866,0
94 | 74.49269241843041,84.84513684930135,1
95 | 89.84580670720979,45.35828361091658,1
96 | 83.48916274498238,48.38028579728175,1
97 | 42.2617008099817,87.10385094025457,1
98 | 99.31500880510394,68.77540947206617,1
99 | 55.34001756003703,64.9319380069486,1
100 | 74.77589300092767,89.52981289513276,1
101 |
--------------------------------------------------------------------------------
/models/nn/README.md:
--------------------------------------------------------------------------------
1 | ### 8. Xây dựng mạng Neural Network
2 | ----
3 | - **a. Neural Network**
4 |
5 | - Định nghĩa
6 | - Mô phỏng thực tế
7 |
8 | - **b. Hàm kích hoạt phi tuyến**
9 |
10 | - Sigmoid
11 | - Tanh
12 | - ReLU
13 |
14 |
15 | - **c. Thuật toán lan truyền ngược (backpropagation)**
16 |
17 | - Đạo hàm riêng (Partial Derivative)
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 | - Ôn lại đạo hàm hàm hợp (Chain Rule)
44 | - Công thức
45 | - Mô phỏng thực tế
46 |
47 | - **d. Thuật toán tối ưu Loss Function (Optimization Algorithms)**
48 | - Thuật toán Gradient Descent
49 |
50 | - **e. Một số vấn đề khi huấn luyện Neural Network**
51 |
--------------------------------------------------------------------------------
/models/random-forests/README.md:
--------------------------------------------------------------------------------
1 | ### 7. Xây dựng mô hình Decision Trees và Random Forests
2 | ----
3 |
4 | - **a. Mô hình Cây quyết định (Decision Trees)**
5 |
6 | - Định nghĩa
7 | - Mô phỏng thực tế
8 |
9 | - **b. Các thuật toán**
10 |
11 | - ID3
12 | - C4.5
13 | - C5.0
14 | - CART
15 |
16 | - **c. Triển khai thuật toán CART**
17 |
18 | - Câu hỏi (Question)
19 |
20 | - **d. Bài toán phân loại ô tô**
21 |
22 | - Vấn đề
23 | - Giải quyết bằng mô hình Cây quyết định
24 |
25 | - **e. Mô hình Random Forests**
26 |
27 | - Định nghĩa
--------------------------------------------------------------------------------
/models/rnn/README.md:
--------------------------------------------------------------------------------
1 | ### 10. Xây dựng mạng Recurrent Neural Network (RNN)
2 | ----
3 |
4 | **a. Language Models**
5 |
6 | **b. Recurrent Neural Network (RNN)**
7 |
8 | - Giới thiệu RNN
9 | - Cấu trúc mạng RNN
10 | - Gate Recurrent Unit (GRU)
11 | - Long Short-Term Memory (LSTM)
12 |
13 | **b. Vấn đề thường gặp và cách giải quyết**
14 | - Vanishing
15 | - Expoding Gradient Descent
--------------------------------------------------------------------------------
/numpy/README.md:
--------------------------------------------------------------------------------
1 | ### 3. Thư viện Numpy và TensorFlow
2 | ----
3 | - **a. Numpy**
4 |
5 | - Giới thiệu về NumPy
6 |
7 | - Trang chủ của [NumPy](http://www.numpy.org/)
8 | - NumPy là viết tắt của **"Numerical Python"**, là một thư viện chuyên để xử lý, tính toán vector, ma trận.
9 | - NumPy thường được sử dụng cùng [SciPy](https://www.scipy.org/) và [Matplotlib](https://matplotlib.org/) để thay thế cho [MatLab](https://www.mathworks.com/products/matlab.html) vô cùng đắt đỏ.
10 | - Được viết bằng Python và C nên tốc độ thực thi tốt.
11 | - Các APIs
12 | - [Mảng NumPy - NumPy Array](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/array.ipynb)
13 | - [Truy cập thông qua chỉ mục và cắt Array - Array Indexing và Array Slicing](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/indexing%26slicing.ipynb)
14 | - [Phép lan truyền - Broadcasting](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/broadcasting.ipynb)
15 | - [Phép lặp - Iterating](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/iterating.ipynb)
16 | - [Các thao tác trên Array - Array Manipulation](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/manipulation.ipynb)
17 | - [Đại số tuyến tính - Linear Algebra](https://github.com/bangoc123/learn-machine-learning-in-two-months/tree/master/numpy/linear-algebra)
18 | - [Phép nhân vô hướng](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/products.ipynb)
19 | - [Norm ma trận](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/norms.ipynb)
20 | - [Phép cộng ma trận](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/numpy/linear-algebra/addition.ipynb)
21 |
22 | - **b. TensorFlow**
23 |
24 | Toàn bộ kiến thức cần biết về TensorFlow nằm [tại đây](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/tensorflow/README.MD)
--------------------------------------------------------------------------------
/numpy/iterating.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Phép lặp (Iterating)\n",
8 | "----\n",
9 | "\n",
10 | "Xem thêm tại [Iterating](https://docs.scipy.org/doc/numpy/reference/generated/numpy.nditer.html)"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {},
16 | "source": [
17 | "### I. Lặp qua các phần tử"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "##### 1. Mảng một chiều"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 25,
30 | "metadata": {},
31 | "outputs": [],
32 | "source": [
33 | "import numpy as np"
34 | ]
35 | },
36 | {
37 | "cell_type": "code",
38 | "execution_count": 26,
39 | "metadata": {},
40 | "outputs": [],
41 | "source": [
42 | "numbers_1D = np.arange(10)"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 27,
48 | "metadata": {},
49 | "outputs": [
50 | {
51 | "data": {
52 | "text/plain": [
53 | "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
54 | ]
55 | },
56 | "execution_count": 27,
57 | "metadata": {},
58 | "output_type": "execute_result"
59 | }
60 | ],
61 | "source": [
62 | "numbers_1D"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": 28,
68 | "metadata": {},
69 | "outputs": [
70 | {
71 | "name": "stdout",
72 | "output_type": "stream",
73 | "text": [
74 | "0\n",
75 | "1\n",
76 | "2\n",
77 | "3\n",
78 | "4\n",
79 | "5\n",
80 | "6\n",
81 | "7\n",
82 | "8\n",
83 | "9\n"
84 | ]
85 | }
86 | ],
87 | "source": [
88 | "for item in np.nditer(numbers_1D):\n",
89 | " print item"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "##### 2. Mảng hai chiều"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 7,
102 | "metadata": {},
103 | "outputs": [],
104 | "source": [
105 | "numbers_2D = np.random.random((3, 2))"
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "execution_count": 8,
111 | "metadata": {},
112 | "outputs": [
113 | {
114 | "data": {
115 | "text/plain": [
116 | "array([[0.50255155, 0.14477689],\n",
117 | " [0.25697343, 0.26250991],\n",
118 | " [0.14294119, 0.86949863]])"
119 | ]
120 | },
121 | "execution_count": 8,
122 | "metadata": {},
123 | "output_type": "execute_result"
124 | }
125 | ],
126 | "source": [
127 | "numbers_2D"
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "execution_count": 9,
133 | "metadata": {},
134 | "outputs": [
135 | {
136 | "name": "stdout",
137 | "output_type": "stream",
138 | "text": [
139 | "0.5025515528886214\n",
140 | "0.14477689330268562\n",
141 | "0.25697343316107546\n",
142 | "0.26250990807465013\n",
143 | "0.14294119185276755\n",
144 | "0.8694986286997518\n"
145 | ]
146 | }
147 | ],
148 | "source": [
149 | "for item in np.nditer(numbers_2D):\n",
150 | " print item"
151 | ]
152 | },
153 | {
154 | "cell_type": "markdown",
155 | "metadata": {},
156 | "source": [
157 | "##### 3. Mảng ba chiều"
158 | ]
159 | },
160 | {
161 | "cell_type": "code",
162 | "execution_count": 29,
163 | "metadata": {},
164 | "outputs": [],
165 | "source": [
166 | "numbers_3D = np.random.random((2, 3, 2))"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 30,
172 | "metadata": {},
173 | "outputs": [
174 | {
175 | "data": {
176 | "text/plain": [
177 | "array([[[0.60994814, 0.02299673],\n",
178 | " [0.56888235, 0.17304354],\n",
179 | " [0.18849752, 0.6025226 ]],\n",
180 | "\n",
181 | " [[0.64390825, 0.50995602],\n",
182 | " [0.98812906, 0.5546137 ],\n",
183 | " [0.28536714, 0.01933138]]])"
184 | ]
185 | },
186 | "execution_count": 30,
187 | "metadata": {},
188 | "output_type": "execute_result"
189 | }
190 | ],
191 | "source": [
192 | "numbers_3D"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": 31,
198 | "metadata": {},
199 | "outputs": [
200 | {
201 | "name": "stdout",
202 | "output_type": "stream",
203 | "text": [
204 | "0.6099481441938507\n",
205 | "0.022996726612215745\n",
206 | "0.5688823546426007\n",
207 | "0.17304354432564684\n",
208 | "0.18849752347038662\n",
209 | "0.6025226030053162\n",
210 | "0.6439082520107483\n",
211 | "0.50995602226168\n",
212 | "0.9881290561467033\n",
213 | "0.5546137031592939\n",
214 | "0.28536714183460965\n",
215 | "0.01933137707226673\n"
216 | ]
217 | }
218 | ],
219 | "source": [
220 | "for item in np.nditer(numbers_3D):\n",
221 | " print item"
222 | ]
223 | },
224 | {
225 | "cell_type": "markdown",
226 | "metadata": {},
227 | "source": [
228 | "##### 4. Thay đổi giá trị trong quá trình lặp"
229 | ]
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": 33,
234 | "metadata": {},
235 | "outputs": [],
236 | "source": [
237 | "numbers = np.arange(7)"
238 | ]
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 34,
243 | "metadata": {},
244 | "outputs": [
245 | {
246 | "data": {
247 | "text/plain": [
248 | "array([0, 1, 2, 3, 4, 5, 6])"
249 | ]
250 | },
251 | "execution_count": 34,
252 | "metadata": {},
253 | "output_type": "execute_result"
254 | }
255 | ],
256 | "source": [
257 | "numbers"
258 | ]
259 | },
260 | {
261 | "cell_type": "code",
262 | "execution_count": 35,
263 | "metadata": {},
264 | "outputs": [],
265 | "source": [
266 | "# Bình phương mỗi phần tử\n",
267 | "# Chú ý: Thêm cờ op_flags = ['readwrite'] để bật chế độ có thể thay đổi các phần tử trong mảng\n",
268 | "for item in np.nditer(numbers, op_flags = ['readwrite']):\n",
269 | " item[...] = item ** 2"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 36,
275 | "metadata": {},
276 | "outputs": [
277 | {
278 | "data": {
279 | "text/plain": [
280 | "array([ 0, 1, 4, 9, 16, 25, 36])"
281 | ]
282 | },
283 | "execution_count": 36,
284 | "metadata": {},
285 | "output_type": "execute_result"
286 | }
287 | ],
288 | "source": [
289 | "numbers"
290 | ]
291 | },
292 | {
293 | "cell_type": "markdown",
294 | "metadata": {},
295 | "source": [
296 | "### II. Lặp qua dòng"
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "execution_count": 40,
302 | "metadata": {},
303 | "outputs": [],
304 | "source": [
305 | "float_2D = np.random.random((2, 2))"
306 | ]
307 | },
308 | {
309 | "cell_type": "code",
310 | "execution_count": 41,
311 | "metadata": {},
312 | "outputs": [
313 | {
314 | "data": {
315 | "text/plain": [
316 | "array([[0.93482515, 0.51820666],\n",
317 | " [0.19629551, 0.01829495]])"
318 | ]
319 | },
320 | "execution_count": 41,
321 | "metadata": {},
322 | "output_type": "execute_result"
323 | }
324 | ],
325 | "source": [
326 | "float_2D"
327 | ]
328 | },
329 | {
330 | "cell_type": "code",
331 | "execution_count": 53,
332 | "metadata": {},
333 | "outputs": [
334 | {
335 | "name": "stdout",
336 | "output_type": "stream",
337 | "text": [
338 | "[0.93482515 0.19629551] [0.51820666 0.01829495]\n"
339 | ]
340 | }
341 | ],
342 | "source": [
343 | "for x in np.nditer(float_2D, flags = ['external_loop'], order = 'F'):\n",
344 | " print x,"
345 | ]
346 | },
347 | {
348 | "cell_type": "code",
349 | "execution_count": 54,
350 | "metadata": {},
351 | "outputs": [
352 | {
353 | "name": "stdout",
354 | "output_type": "stream",
355 | "text": [
356 | "[0.93482515 0.51820666 0.19629551 0.01829495]\n"
357 | ]
358 | }
359 | ],
360 | "source": [
361 | "for x in np.nditer(float_2D, flags = ['external_loop'], order = 'C'):\n",
362 | " print x,"
363 | ]
364 | }
365 | ],
366 | "metadata": {
367 | "kernelspec": {
368 | "display_name": "Python 2",
369 | "language": "python",
370 | "name": "python2"
371 | },
372 | "language_info": {
373 | "codemirror_mode": {
374 | "name": "ipython",
375 | "version": 2
376 | },
377 | "file_extension": ".py",
378 | "mimetype": "text/x-python",
379 | "name": "python",
380 | "nbconvert_exporter": "python",
381 | "pygments_lexer": "ipython2",
382 | "version": "2.7.14"
383 | }
384 | },
385 | "nbformat": 4,
386 | "nbformat_minor": 2
387 | }
388 |
--------------------------------------------------------------------------------
/numpy/linear-algebra/addition.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Phép cộng ma trận và vector"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "### I. Cộng ma trận"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "##### 1. Công thức"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | ""
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "Ảnh từ [Matrix_addition](https://en.wikipedia.org/wiki/Matrix_addition)"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "##### 2. Mô phỏng"
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | "##### a. Phép cộng"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | ""
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | "##### b. Phép trừ"
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | ""
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | "##### 3. Thực hành với Numpy"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 5,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "import numpy as np"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 6,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "A = np.array([\n",
96 | " [1, 3],\n",
97 | " [1, 0],\n",
98 | " [1, 2]\n",
99 | "])"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 7,
105 | "metadata": {},
106 | "outputs": [],
107 | "source": [
108 | "B = np.array([\n",
109 | " [0, 0],\n",
110 | " [7, 5],\n",
111 | " [2, 1]\n",
112 | "])"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 11,
118 | "metadata": {},
119 | "outputs": [
120 | {
121 | "data": {
122 | "text/plain": [
123 | "array([[1, 3],\n",
124 | " [8, 5],\n",
125 | " [3, 3]])"
126 | ]
127 | },
128 | "execution_count": 11,
129 | "metadata": {},
130 | "output_type": "execute_result"
131 | }
132 | ],
133 | "source": [
134 | "np.add(A, B)"
135 | ]
136 | },
137 | {
138 | "cell_type": "markdown",
139 | "metadata": {},
140 | "source": [
141 | "Tương đương với"
142 | ]
143 | },
144 | {
145 | "cell_type": "code",
146 | "execution_count": 8,
147 | "metadata": {},
148 | "outputs": [
149 | {
150 | "data": {
151 | "text/plain": [
152 | "array([[1, 3],\n",
153 | " [8, 5],\n",
154 | " [3, 3]])"
155 | ]
156 | },
157 | "execution_count": 8,
158 | "metadata": {},
159 | "output_type": "execute_result"
160 | }
161 | ],
162 | "source": [
163 | "A + B"
164 | ]
165 | },
166 | {
167 | "cell_type": "code",
168 | "execution_count": null,
169 | "metadata": {},
170 | "outputs": [],
171 | "source": []
172 | }
173 | ],
174 | "metadata": {
175 | "kernelspec": {
176 | "display_name": "Python 2",
177 | "language": "python",
178 | "name": "python2"
179 | },
180 | "language_info": {
181 | "codemirror_mode": {
182 | "name": "ipython",
183 | "version": 2
184 | },
185 | "file_extension": ".py",
186 | "mimetype": "text/x-python",
187 | "name": "python",
188 | "nbconvert_exporter": "python",
189 | "pygments_lexer": "ipython2",
190 | "version": "2.7.14"
191 | }
192 | },
193 | "nbformat": 4,
194 | "nbformat_minor": 2
195 | }
196 |
--------------------------------------------------------------------------------
/numpy/linear-algebra/norms.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Norm\n",
8 | "----"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | "### I. Lý thuyết"
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "Xem kỹ thêm tại [Norm (mathematics) - Wiki](https://en.wikipedia.org/wiki/Norm_(mathematics))"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "Norm của một vector là chiều dài của vector đó"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "### II. L2 Norm"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {},
42 | "source": [
43 | "L2 Norm là căn tổng bình phương các giá trị của vector"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "##### 1. Công thức"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | ""
58 | ]
59 | },
60 | {
61 | "cell_type": "markdown",
62 | "metadata": {},
63 | "source": [
64 | "##### 2. Thực hành với Numpy"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "Xem kỹ thêm tại [numpy.linalg.norm](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.norm.html#r46)"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": 10,
77 | "metadata": {},
78 | "outputs": [],
79 | "source": [
80 | "from numpy import linalg"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 16,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "A = np.array([1, 2, 3, 2, 3, 4])"
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "metadata": {},
95 | "source": [
96 | "Norm L2 of A = $\\sqrt{1^2 + 2^2 + 3^2 + 2^2 + 3^2 + 4^2}$\n"
97 | ]
98 | },
99 | {
100 | "cell_type": "code",
101 | "execution_count": 17,
102 | "metadata": {},
103 | "outputs": [
104 | {
105 | "data": {
106 | "text/plain": [
107 | "6.557438524302"
108 | ]
109 | },
110 | "execution_count": 17,
111 | "metadata": {},
112 | "output_type": "execute_result"
113 | }
114 | ],
115 | "source": [
116 | "linalg.norm(A)"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "metadata": {},
122 | "source": [
123 | "### II. L1 Norm"
124 | ]
125 | },
126 | {
127 | "cell_type": "markdown",
128 | "metadata": {},
129 | "source": [
130 | "L1 Norm là tổng trị tuyệt đối các giá trị của vector"
131 | ]
132 | },
133 | {
134 | "cell_type": "markdown",
135 | "metadata": {},
136 | "source": [
137 | "##### 1. Công thức"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | ""
145 | ]
146 | },
147 | {
148 | "cell_type": "markdown",
149 | "metadata": {},
150 | "source": [
151 | "##### 2. Thực hành với Numpy"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": 20,
157 | "metadata": {},
158 | "outputs": [],
159 | "source": [
160 | "B = np.array([6, 20, 10, -4])"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "Norm L1 of B = |6| + |20| + |10| + |-4|"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 21,
173 | "metadata": {},
174 | "outputs": [
175 | {
176 | "data": {
177 | "text/plain": [
178 | "40.0"
179 | ]
180 | },
181 | "execution_count": 21,
182 | "metadata": {},
183 | "output_type": "execute_result"
184 | }
185 | ],
186 | "source": [
187 | "linalg.norm(B, ord=1)"
188 | ]
189 | }
190 | ],
191 | "metadata": {
192 | "kernelspec": {
193 | "display_name": "Python 2",
194 | "language": "python",
195 | "name": "python2"
196 | },
197 | "language_info": {
198 | "codemirror_mode": {
199 | "name": "ipython",
200 | "version": 2
201 | },
202 | "file_extension": ".py",
203 | "mimetype": "text/x-python",
204 | "name": "python",
205 | "nbconvert_exporter": "python",
206 | "pygments_lexer": "ipython2",
207 | "version": "2.7.14"
208 | }
209 | },
210 | "nbformat": 4,
211 | "nbformat_minor": 2
212 | }
213 |
--------------------------------------------------------------------------------
/python-tutorials/BinarySearch.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Linear Search"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Độ phức tạp: O(N)"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | ""
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 3,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "nums = [1, 9, 10, 20, 30, -9, 8, -2, 6]"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 4,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "def linearSearch(arr, target):\n",
40 | " for idx in range(len(arr)):\n",
41 | " if arr[idx] == target:\n",
42 | " return idx\n",
43 | " \n",
44 | " return -1"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": 5,
50 | "metadata": {},
51 | "outputs": [
52 | {
53 | "data": {
54 | "text/plain": [
55 | "-1"
56 | ]
57 | },
58 | "execution_count": 5,
59 | "metadata": {},
60 | "output_type": "execute_result"
61 | }
62 | ],
63 | "source": [
64 | "linearSearch(nums, 2)"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "execution_count": 6,
70 | "metadata": {},
71 | "outputs": [
72 | {
73 | "data": {
74 | "text/plain": [
75 | "3"
76 | ]
77 | },
78 | "execution_count": 6,
79 | "metadata": {},
80 | "output_type": "execute_result"
81 | }
82 | ],
83 | "source": [
84 | "linearSearch(nums, 20)"
85 | ]
86 | },
87 | {
88 | "cell_type": "markdown",
89 | "metadata": {},
90 | "source": [
91 | "### Binary Search"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {},
97 | "source": [
98 | "Độ phức tạp: O(log(N))"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "metadata": {},
104 | "source": [
105 | ""
106 | ]
107 | },
108 | {
109 | "cell_type": "markdown",
110 | "metadata": {},
111 | "source": [
112 | ""
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "metadata": {},
118 | "source": [
119 | ""
120 | ]
121 | },
122 | {
123 | "cell_type": "markdown",
124 | "metadata": {},
125 | "source": [
126 | ""
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | ""
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 20,
139 | "metadata": {},
140 | "outputs": [],
141 | "source": [
142 | "nums = [1, 9, 10, 20, 30, -9, 8, -2, 6, 5, 13, 4]"
143 | ]
144 | },
145 | {
146 | "cell_type": "code",
147 | "execution_count": 21,
148 | "metadata": {},
149 | "outputs": [],
150 | "source": [
151 | "nums.sort()"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": 22,
157 | "metadata": {},
158 | "outputs": [
159 | {
160 | "data": {
161 | "text/plain": [
162 | "[-9, -2, 1, 4, 5, 6, 8, 9, 10, 13, 20, 30]"
163 | ]
164 | },
165 | "execution_count": 22,
166 | "metadata": {},
167 | "output_type": "execute_result"
168 | }
169 | ],
170 | "source": [
171 | "\n",
172 | "nums"
173 | ]
174 | },
175 | {
176 | "cell_type": "code",
177 | "execution_count": 23,
178 | "metadata": {},
179 | "outputs": [],
180 | "source": [
181 | "i = 0"
182 | ]
183 | },
184 | {
185 | "cell_type": "code",
186 | "execution_count": 24,
187 | "metadata": {},
188 | "outputs": [],
189 | "source": [
190 | "j = len(nums) - 1"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": 25,
196 | "metadata": {
197 | "scrolled": true
198 | },
199 | "outputs": [
200 | {
201 | "data": {
202 | "text/plain": [
203 | "11"
204 | ]
205 | },
206 | "execution_count": 25,
207 | "metadata": {},
208 | "output_type": "execute_result"
209 | }
210 | ],
211 | "source": [
212 | "j"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {},
218 | "source": [
219 | "#### Sử dụng vòng lặp"
220 | ]
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": 38,
225 | "metadata": {},
226 | "outputs": [],
227 | "source": [
228 | "def binarySearch(nums, target):\n",
229 | " left = 0\n",
230 | " right = len(nums) - 1\n",
231 | " while left <= right:\n",
232 | " mid = left + (right - left) // 2\n",
233 | " if nums[mid] < target:\n",
234 | " left = mid + 1\n",
235 | " elif nums[mid] > target:\n",
236 | " right = mid - 1\n",
237 | " else:\n",
238 | " return mid\n",
239 | " \n",
240 | " return -1"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": 39,
246 | "metadata": {},
247 | "outputs": [
248 | {
249 | "data": {
250 | "text/plain": [
251 | "7"
252 | ]
253 | },
254 | "execution_count": 39,
255 | "metadata": {},
256 | "output_type": "execute_result"
257 | }
258 | ],
259 | "source": [
260 | "binarySearch(nums, 9)"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "#### Sử dụng đệ quy"
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": 49,
273 | "metadata": {},
274 | "outputs": [],
275 | "source": [
276 | "def binarySearchWithRecursion(nums, left, right, target):\n",
277 | " if left > right:\n",
278 | " return -1\n",
279 | " mid = left + (right - left) // 2\n",
280 | " if nums[mid] == target: \n",
281 | " return mid\n",
282 | " elif nums[mid] < target:\n",
283 | " left = mid + 1\n",
284 | " else:\n",
285 | " right = mid - 1\n",
286 | " \n",
287 | " return binarySearchWithRecursion(nums, left, right, target)"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": 58,
293 | "metadata": {},
294 | "outputs": [
295 | {
296 | "data": {
297 | "text/plain": [
298 | "7"
299 | ]
300 | },
301 | "execution_count": 58,
302 | "metadata": {},
303 | "output_type": "execute_result"
304 | }
305 | ],
306 | "source": [
307 | "binarySearchWithRecursion(nums, 0, len(nums) - 1, 9)"
308 | ]
309 | },
310 | {
311 | "cell_type": "markdown",
312 | "metadata": {},
313 | "source": [
314 | "#### Tìm phần tử xuất hiện đầu tiên"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": 6,
320 | "metadata": {},
321 | "outputs": [],
322 | "source": [
323 | "def binarySearchFirstElement(nums, target):\n",
324 | " left = 0\n",
325 | " right = len(nums) - 1\n",
326 | " while left <= right:\n",
327 | " mid = left + (right - left) // 2\n",
328 | " if nums[mid] == target and (mid == left or nums[mid-1] < target):\n",
329 | " return mid\n",
330 | " elif nums[mid] < target:\n",
331 | " left = mid + 1\n",
332 | " else:\n",
333 | " right = mid - 1\n",
334 | " return -1"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 7,
340 | "metadata": {},
341 | "outputs": [
342 | {
343 | "data": {
344 | "text/plain": [
345 | "3"
346 | ]
347 | },
348 | "execution_count": 7,
349 | "metadata": {},
350 | "output_type": "execute_result"
351 | }
352 | ],
353 | "source": [
354 | "binarySearchFirstElement([1, 1, 1, 2, 2 , 2], 2)"
355 | ]
356 | },
357 | {
358 | "cell_type": "code",
359 | "execution_count": 8,
360 | "metadata": {},
361 | "outputs": [],
362 | "source": [
363 | "def binarySearchLastElement(nums, target):\n",
364 | " left = 0\n",
365 | " right = len(nums) - 1\n",
366 | " while left <= right:\n",
367 | " mid = left + (right - left) // 2\n",
368 | " if nums[mid] == target and (mid == right or target < nums[mid + 1]):\n",
369 | " return mid\n",
370 | " elif nums[mid] > target:\n",
371 | " right = mid - 1\n",
372 | " else:\n",
373 | " left = mid + 1\n",
374 | " return -1"
375 | ]
376 | },
377 | {
378 | "cell_type": "code",
379 | "execution_count": 10,
380 | "metadata": {},
381 | "outputs": [
382 | {
383 | "data": {
384 | "text/plain": [
385 | "5"
386 | ]
387 | },
388 | "execution_count": 10,
389 | "metadata": {},
390 | "output_type": "execute_result"
391 | }
392 | ],
393 | "source": [
394 | "binarySearchLastElement([1, 1, 1, 2, 2 , 2], 2)"
395 | ]
396 | },
397 | {
398 | "cell_type": "code",
399 | "execution_count": null,
400 | "metadata": {},
401 | "outputs": [],
402 | "source": []
403 | }
404 | ],
405 | "metadata": {
406 | "kernelspec": {
407 | "display_name": "Python 3",
408 | "language": "python",
409 | "name": "python3"
410 | },
411 | "language_info": {
412 | "codemirror_mode": {
413 | "name": "ipython",
414 | "version": 3
415 | },
416 | "file_extension": ".py",
417 | "mimetype": "text/x-python",
418 | "name": "python",
419 | "nbconvert_exporter": "python",
420 | "pygments_lexer": "ipython3",
421 | "version": "3.8.2"
422 | }
423 | },
424 | "nbformat": 4,
425 | "nbformat_minor": 4
426 | }
427 |
--------------------------------------------------------------------------------
/python-tutorials/BinarySearchTree.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### 1. Tạo 1 cây"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | ""
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | ""
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | ""
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | ""
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | ""
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | ""
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | ""
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | ""
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 135,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "class Node:\n",
80 | " def __init__(self, val):\n",
81 | " self.val = val\n",
82 | " self.left = None\n",
83 | " self.right = None"
84 | ]
85 | },
86 | {
87 | "cell_type": "code",
88 | "execution_count": 86,
89 | "metadata": {},
90 | "outputs": [],
91 | "source": [
92 | "aList = [10, 20, -10, 40, 15, 8, 6, -15]"
93 | ]
94 | },
95 | {
96 | "cell_type": "code",
97 | "execution_count": 87,
98 | "metadata": {},
99 | "outputs": [],
100 | "source": [
101 | "def addANode(root, val):\n",
102 | " if root is None:\n",
103 | " return Node(val)\n",
104 | " \n",
105 | " if val < root.val:\n",
106 | " root.left = addANode(root.left, val)\n",
107 | " else:\n",
108 | " root.right = addANode(root.right, val)\n",
109 | " \n",
110 | " return root"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 88,
116 | "metadata": {},
117 | "outputs": [],
118 | "source": [
119 | "root = Node(aList[0])\n",
120 | "for idx in range(1, len(aList)):\n",
121 | " root = addANode(root, aList[idx])"
122 | ]
123 | },
124 | {
125 | "cell_type": "markdown",
126 | "metadata": {},
127 | "source": [
128 | "### 2. In các phần từ trong một cây"
129 | ]
130 | },
131 | {
132 | "cell_type": "markdown",
133 | "metadata": {},
134 | "source": [
135 | "In-Order"
136 | ]
137 | },
138 | {
139 | "cell_type": "code",
140 | "execution_count": 89,
141 | "metadata": {},
142 | "outputs": [],
143 | "source": [
144 | "def inOrder(root):\n",
145 | " if root is None:\n",
146 | " return\n",
147 | " inOrder(root.left)\n",
148 | " print(root.val)\n",
149 | " inOrder(root.right)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Post-Order"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 90,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "def postOrder(root):\n",
166 | " if root is None:\n",
167 | " return\n",
168 | " postOrder(root.left)\n",
169 | " postOrder(root.right)\n",
170 | " print(root.val)"
171 | ]
172 | },
173 | {
174 | "cell_type": "markdown",
175 | "metadata": {},
176 | "source": [
177 | "Pre-Order"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": 91,
183 | "metadata": {},
184 | "outputs": [],
185 | "source": [
186 | "def preOrder(root):\n",
187 | " if root is None:\n",
188 | " return\n",
189 | " print(root.val)\n",
190 | " preOrder(root.left)\n",
191 | " preOrder(root.right)\n",
192 | " "
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "### 3. Tìm phần tử trong một cây"
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": 92,
205 | "metadata": {},
206 | "outputs": [],
207 | "source": [
208 | "def findANode(root, val):\n",
209 | " if root is None:\n",
210 | " return None\n",
211 | " \n",
212 | " if val == root.val:\n",
213 | " return root\n",
214 | " elif val > root.val:\n",
215 | " return findANode(root.right, val)\n",
216 | " else:\n",
217 | " return findANode(root.left, val)\n",
218 | " "
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": 93,
224 | "metadata": {},
225 | "outputs": [],
226 | "source": [
227 | "findANode(root, -1)"
228 | ]
229 | },
230 | {
231 | "cell_type": "markdown",
232 | "metadata": {},
233 | "source": [
234 | "### 4. Xóa phần tử trong một cây"
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": 101,
240 | "metadata": {},
241 | "outputs": [],
242 | "source": [
243 | "def minValueNode(node): \n",
244 | " current = node \n",
245 | " \n",
246 | " # Tìm con trái nhỏ nhất \n",
247 | " while(current.left is not None): \n",
248 | " current = current.left \n",
249 | " \n",
250 | " return current "
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 128,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": [
259 | "def removeANode(root, val):\n",
260 | " if root is None:\n",
261 | " return root\n",
262 | " \n",
263 | " if val > root.val:\n",
264 | " root.right = removeANode(root.right, val)\n",
265 | " elif val < root.val:\n",
266 | " root.left = removeANode(root.left, val)\n",
267 | " else:\n",
268 | " # Một node có một con hoặc không con nào\n",
269 | " if root.left is None:\n",
270 | " temp = root.right\n",
271 | " root = None\n",
272 | " return temp\n",
273 | " elif root.right is None:\n",
274 | " temp = root.left\n",
275 | " root = None\n",
276 | " return temp\n",
277 | " \n",
278 | " # Một node có 2 con\n",
279 | " # Tìm node bên trái nhỏ nhất bên cây con phải \n",
280 | " temp = minValueNode(root.right)\n",
281 | " \n",
282 | " print(temp.val)\n",
283 | " \n",
284 | " # Gắn node hiện tại với giá trị nhỏ nhất bên cây con phải\n",
285 | " root.val = temp.val\n",
286 | " \n",
287 | " # Xóa node vừa tìm được\n",
288 | " root.right = removeANode(root.right, temp.val)\n",
289 | " \n",
290 | " return root "
291 | ]
292 | },
293 | {
294 | "cell_type": "code",
295 | "execution_count": 129,
296 | "metadata": {},
297 | "outputs": [
298 | {
299 | "name": "stdout",
300 | "output_type": "stream",
301 | "text": [
302 | "-15\n",
303 | "-10\n",
304 | "6\n",
305 | "8\n",
306 | "10\n",
307 | "15\n",
308 | "20\n",
309 | "40\n"
310 | ]
311 | }
312 | ],
313 | "source": [
314 | "inOrder(root)"
315 | ]
316 | },
317 | {
318 | "cell_type": "code",
319 | "execution_count": 130,
320 | "metadata": {},
321 | "outputs": [
322 | {
323 | "data": {
324 | "text/plain": [
325 | "<__main__.Node at 0x41701c0>"
326 | ]
327 | },
328 | "execution_count": 130,
329 | "metadata": {},
330 | "output_type": "execute_result"
331 | }
332 | ],
333 | "source": [
334 | "root"
335 | ]
336 | },
337 | {
338 | "cell_type": "code",
339 | "execution_count": 133,
340 | "metadata": {},
341 | "outputs": [
342 | {
343 | "name": "stdout",
344 | "output_type": "stream",
345 | "text": [
346 | "-15\n",
347 | "-10\n",
348 | "8\n",
349 | "10\n",
350 | "15\n",
351 | "20\n",
352 | "40\n"
353 | ]
354 | }
355 | ],
356 | "source": [
357 | "x = inOrder(root)"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "execution_count": null,
363 | "metadata": {},
364 | "outputs": [],
365 | "source": []
366 | }
367 | ],
368 | "metadata": {
369 | "kernelspec": {
370 | "display_name": "Python 3",
371 | "language": "python",
372 | "name": "python3"
373 | },
374 | "language_info": {
375 | "codemirror_mode": {
376 | "name": "ipython",
377 | "version": 3
378 | },
379 | "file_extension": ".py",
380 | "mimetype": "text/x-python",
381 | "name": "python",
382 | "nbconvert_exporter": "python",
383 | "pygments_lexer": "ipython3",
384 | "version": "3.8.2"
385 | }
386 | },
387 | "nbformat": 4,
388 | "nbformat_minor": 4
389 | }
390 |
--------------------------------------------------------------------------------
/python-tutorials/DFS/DFS - Pratice.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## 1. Bài toán liên quan tới đảo"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "### 1.1 Đếm số lượng đảo"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "[Leetcode 200](https://leetcode.com/problems/number-of-islands/)"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "Đếm số lượng đảo trong một ma trận"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "Bài giải"
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 4,
48 | "metadata": {},
49 | "outputs": [],
50 | "source": [
51 | "def isOk(x, y, m, n):\n",
52 | " return x >= 0 and x < m and y >= 0 and y < n \n",
53 | "\n",
54 | "def dfs(start, grid, visited, m, n):\n",
55 | " x, y = start\n",
56 | " xRows = [-1, 1, 0, 0]\n",
57 | " xCols = [0, 0, -1, 1]\n",
58 | " cnt = 0\n",
59 | " for i in range(4):\n",
60 | " new_x = x + xRows[i]\n",
61 | " new_y = y + xCols[i]\n",
62 | " if isOk(new_x, new_y, m, n) and not visited[new_x][new_y] and grid[new_x][new_y] == '1':\n",
63 | " visited[new_x][new_y] = True\n",
64 | " dfs((new_x, new_y), grid, visited, m, n)\n",
65 | " \n",
66 | "class Solution:\n",
67 | " def numIslands(self, grid) -> int:\n",
68 | " m = len(grid)\n",
69 | " if m == 0:\n",
70 | " return 0\n",
71 | " \n",
72 | " n = len(grid[0])\n",
73 | " \n",
74 | " visited = [[False for _ in range(n)] for _ in range(m)]\n",
75 | " cnt = 0\n",
76 | " for i in range(m):\n",
77 | " for j in range(n):\n",
78 | " if not visited[i][j] and grid[i][j] == '1':\n",
79 | " dfs((i, j), grid, visited, m, n)\n",
80 | " cnt += 1\n",
81 | " \n",
82 | " return cnt"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 5,
88 | "metadata": {},
89 | "outputs": [],
90 | "source": [
91 | "grid = [[\"1\",\"1\",\"1\",\"1\",\"0\"],[\"1\",\"1\",\"0\",\"1\",\"0\"],[\"1\",\"1\",\"0\",\"0\",\"0\"],[\"0\",\"0\",\"0\",\"0\",\"0\"]]"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": 6,
97 | "metadata": {},
98 | "outputs": [
99 | {
100 | "data": {
101 | "text/plain": [
102 | "[['1', '1', '1', '1', '0'],\n",
103 | " ['1', '1', '0', '1', '0'],\n",
104 | " ['1', '1', '0', '0', '0'],\n",
105 | " ['0', '0', '0', '0', '0']]"
106 | ]
107 | },
108 | "execution_count": 6,
109 | "metadata": {},
110 | "output_type": "execute_result"
111 | }
112 | ],
113 | "source": [
114 | "grid"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": 8,
120 | "metadata": {},
121 | "outputs": [
122 | {
123 | "data": {
124 | "text/plain": [
125 | "1"
126 | ]
127 | },
128 | "execution_count": 8,
129 | "metadata": {},
130 | "output_type": "execute_result"
131 | }
132 | ],
133 | "source": [
134 | "sol = Solution() \n",
135 | "sol.numIslands(grid)"
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "### 2.1 Tìm đảo rộng nhất"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "[Leetcode 695](https://leetcode.com/problems/max-area-of-island/)"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Tìm diện tích lớn nhất của một đảo trong ma trận"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 11,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "def isOk(x, y, m, n):\n",
166 | " return x >= 0 and x < m and y >= 0 and y < n \n",
167 | "\n",
168 | "def dfs(start, grid, visited, total, m, n):\n",
169 | " x, y = start\n",
170 | " xRows = [-1, 1, 0, 0]\n",
171 | " xCols = [0, 0, -1, 1]\n",
172 | " cnt = 0\n",
173 | " for i in range(4):\n",
174 | " new_x = x + xRows[i]\n",
175 | " new_y = y + xCols[i]\n",
176 | " if isOk(new_x, new_y, m, n) and not visited[new_x][new_y] and grid[new_x][new_y] == 1:\n",
177 | " visited[new_x][new_y] = True\n",
178 | " total[\"value\"] += 1\n",
179 | " dfs((new_x, new_y), grid, visited, total, m, n)\n",
180 | " \n",
181 | "\n",
182 | "\n",
183 | "class Solution2:\n",
184 | " def maxAreaOfIsland(self, grid) -> int:\n",
185 | " self.max = 0\n",
186 | " m = len(grid)\n",
187 | " n = len(grid[0])\n",
188 | " \n",
189 | " visited = [[False for _ in range(n)] for _ in range(m)]\n",
190 | " \n",
191 | " for i in range(m):\n",
192 | " for j in range(n):\n",
193 | " if not visited[i][j] and grid[i][j] == 1:\n",
194 | " aDict = {\"value\":1}\n",
195 | " visited[i][j] = True\n",
196 | " dfs((i, j), grid, visited, aDict, m, n)\n",
197 | " # print(aDict)\n",
198 | " if aDict[\"value\"] > self.max:\n",
199 | " self.max = aDict[\"value\"]\n",
200 | " \n",
201 | " return self.max"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 10,
207 | "metadata": {},
208 | "outputs": [],
209 | "source": [
210 | "grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],\n",
211 | " [0,0,0,0,0,0,0,1,1,1,0,0,0],\n",
212 | " [0,1,1,0,1,0,0,0,0,0,0,0,0],\n",
213 | " [0,1,0,0,1,1,0,0,1,0,1,0,0],\n",
214 | " [0,1,0,0,1,1,0,0,1,1,1,0,0],\n",
215 | " [0,0,0,0,0,0,0,0,0,0,1,0,0],\n",
216 | " [0,0,0,0,0,0,0,1,1,1,0,0,0],\n",
217 | " [0,0,0,0,0,0,0,1,1,0,0,0,0]]"
218 | ]
219 | },
220 | {
221 | "cell_type": "code",
222 | "execution_count": 13,
223 | "metadata": {},
224 | "outputs": [
225 | {
226 | "data": {
227 | "text/plain": [
228 | "6"
229 | ]
230 | },
231 | "execution_count": 13,
232 | "metadata": {},
233 | "output_type": "execute_result"
234 | }
235 | ],
236 | "source": [
237 | "sol2 = Solution2() \n",
238 | "sol2.maxAreaOfIsland(grid)"
239 | ]
240 | }
241 | ],
242 | "metadata": {
243 | "kernelspec": {
244 | "display_name": "Python 3",
245 | "language": "python",
246 | "name": "python3"
247 | },
248 | "language_info": {
249 | "codemirror_mode": {
250 | "name": "ipython",
251 | "version": 3
252 | },
253 | "file_extension": ".py",
254 | "mimetype": "text/x-python",
255 | "name": "python",
256 | "nbconvert_exporter": "python",
257 | "pygments_lexer": "ipython3",
258 | "version": "3.8.2"
259 | }
260 | },
261 | "nbformat": 4,
262 | "nbformat_minor": 4
263 | }
264 |
--------------------------------------------------------------------------------
/python-tutorials/DPExamples.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## 1. Floyd Warshall"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "Bài toán tìm độ dài ngắn nhất từ tất cả các đỉnh đến các đỉnh còn lại trong một đồ thị không có chu trình âm."
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 39,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "edges = [\n",
31 | " [0, 1, 3],\n",
32 | " [0, 3, 1],\n",
33 | " [1, 2, 6],\n",
34 | " [3, 2, 5],\n",
35 | " [3, 4, -2],\n",
36 | " [4, 5, 8],\n",
37 | " [5, 3, 7]\n",
38 | "]"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 40,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "V = 6"
48 | ]
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": 41,
53 | "metadata": {},
54 | "outputs": [],
55 | "source": [
56 | "inf = float('inf')"
57 | ]
58 | },
59 | {
60 | "cell_type": "code",
61 | "execution_count": 42,
62 | "metadata": {},
63 | "outputs": [],
64 | "source": [
65 | "dist = [[inf for _ in range(V)] for _ in range(V)]"
66 | ]
67 | },
68 | {
69 | "cell_type": "code",
70 | "execution_count": 43,
71 | "metadata": {},
72 | "outputs": [],
73 | "source": [
74 | "for i in range(V):\n",
75 | " for j in range(V):\n",
76 | " if i == j:\n",
77 | " dist[i][j] = 0"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 44,
83 | "metadata": {},
84 | "outputs": [
85 | {
86 | "data": {
87 | "text/plain": [
88 | "[[0, inf, inf, inf, inf, inf],\n",
89 | " [inf, 0, inf, inf, inf, inf],\n",
90 | " [inf, inf, 0, inf, inf, inf],\n",
91 | " [inf, inf, inf, 0, inf, inf],\n",
92 | " [inf, inf, inf, inf, 0, inf],\n",
93 | " [inf, inf, inf, inf, inf, 0]]"
94 | ]
95 | },
96 | "execution_count": 44,
97 | "metadata": {},
98 | "output_type": "execute_result"
99 | }
100 | ],
101 | "source": [
102 | "dist"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 45,
108 | "metadata": {},
109 | "outputs": [],
110 | "source": [
111 | "for edge in edges:\n",
112 | " u, v, w = edge\n",
113 | " dist[u][v] = w"
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "execution_count": 46,
119 | "metadata": {},
120 | "outputs": [
121 | {
122 | "data": {
123 | "text/plain": [
124 | "[[0, 3, inf, 1, inf, inf],\n",
125 | " [inf, 0, 6, inf, inf, inf],\n",
126 | " [inf, inf, 0, inf, inf, inf],\n",
127 | " [inf, inf, 5, 0, -2, inf],\n",
128 | " [inf, inf, inf, inf, 0, 8],\n",
129 | " [inf, inf, inf, 7, inf, 0]]"
130 | ]
131 | },
132 | "execution_count": 46,
133 | "metadata": {},
134 | "output_type": "execute_result"
135 | }
136 | ],
137 | "source": [
138 | "dist"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 47,
144 | "metadata": {},
145 | "outputs": [],
146 | "source": [
147 | "for i in range(V):\n",
148 | " for j in range(V):\n",
149 | " for k in range(V):\n",
150 | " if dist[i][k] + dist[k][j] < dist[i][j]:\n",
151 | " dist[i][j] = dist[i][k] + dist[k][j]"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": 38,
157 | "metadata": {},
158 | "outputs": [
159 | {
160 | "data": {
161 | "text/plain": [
162 | "[[0, 3, 6, 1, -1, 7],\n",
163 | " [inf, 0, 6, inf, inf, inf],\n",
164 | " [inf, inf, 0, inf, inf, inf],\n",
165 | " [inf, inf, 5, 0, -2, 6],\n",
166 | " [inf, inf, 20, 15, 0, 8],\n",
167 | " [inf, inf, 12, 7, 5, 0]]"
168 | ]
169 | },
170 | "execution_count": 38,
171 | "metadata": {},
172 | "output_type": "execute_result"
173 | }
174 | ],
175 | "source": [
176 | "dist"
177 | ]
178 | }
179 | ],
180 | "metadata": {
181 | "kernelspec": {
182 | "display_name": "Python 3",
183 | "language": "python",
184 | "name": "python3"
185 | },
186 | "language_info": {
187 | "codemirror_mode": {
188 | "name": "ipython",
189 | "version": 3
190 | },
191 | "file_extension": ".py",
192 | "mimetype": "text/x-python",
193 | "name": "python",
194 | "nbconvert_exporter": "python",
195 | "pygments_lexer": "ipython3",
196 | "version": "3.8.2"
197 | }
198 | },
199 | "nbformat": 4,
200 | "nbformat_minor": 4
201 | }
202 |
--------------------------------------------------------------------------------
/python-tutorials/README.md:
--------------------------------------------------------------------------------
1 | ### 2. Kỹ năng lập trình Python
2 | ----
3 | Python là ngôn ngữ được dùng nhiều nhất để làm Machine Learning vì tính đơn giản gọn nhẹ của nó. Nhưng để đưa vào Production thì tôi nghĩ Javascript cũng là một lựa chọn không tồi. Tôi sẽ chia sẻ về Machine Learning với Javascript trong các phần tiếp theo.
4 |
5 | - **a. Cài đặt Python và các thư viện cần thiết**:
6 | - [Windows](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/essential-installs/vn-windows-python-and-libraries-installations.MD)
7 | - Python
8 | - Pip
9 | - Jupyter Notebook
10 | - TensorFlow
11 | - Annaconda
12 | - [Mac](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/essential-installs/vn-mac-python-and-libraries-installations.MD)
13 | - Python
14 | - Pip
15 | - Jupyter Notebook
16 | - **b. Tính chất đặc điểm**
17 |
18 | Python là ngôn ngữ thông dịch có:
19 | - Điểm mạnh:
20 | - Dễ viết/ Dễ đọc
21 | - Quy trình phát triển phần mềm nhanh vì dòng lệnh được thông dịch thành mã máy và thực thi ngay lập tức
22 | - Có nhiều thư viện mạnh để tính toán cũng như làm Machine Learning như Numpy, Sympy, Scipy, Matplotlib, Pandas, TensorFlow, Keras, vv.
23 | - Điểm yếu:
24 | - Mang đầy đủ điểm yếu của các ngôn ngữ thông dịch như tốc độ chậm, tiềm tàng lỗi trong quá trình thông dịch, source code dễ dàng bị dịch ngược.
25 | - Ngôn ngữ có tính linh hoạt cao nên thiếu tính chặt chẽ.
26 | - **c. Các hàm dựng sẵn và kiểu dữ liệu trên Python**
27 | - Các hàm dựng sẵn trong Python
28 | - [Sort](./sort.ipynb)
29 | - Buble Sort
30 | - Insertion Sort
31 | - Merge Sort
32 | - [Replace](./replace.ipynb)
33 | - Các kiểu dữ liệu trong Python
34 |
35 | Phần này được thiết kế dựa trên khóa [Data Structures & Algorithms in Python - Udacity](https://classroom.udacity.com/courses/ud513) nhưng tôi sẽ đi sâu hơn vào các ví dụ thực tế hơn là chỉ dừng lại ở lý thuyết. Bên cạnh đó tôi sẽ giúp các bạn nắm rõ một số Data Structure quan trọng trong Machine Learning như **Graph** và **Tree**.
36 | - [Integer, Float và Boolean](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/integer-float-boolean.ipynb)
37 | - [String](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/string.ipynb)
38 | - List-Based Collection
39 | - [List](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/list.ipynb)
40 | - [Linked List](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/linked-list.ipynb)
41 | - [Stack](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/stack.ipynb)
42 | - [Queue](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/queue.ipynb)
43 | - [Tuple](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/tuple.ipynb)
44 | - [Dictionary](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/dictionary.ipynb)
45 | - [Set](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/set.ipynb)
46 | - [Graph](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/graph.ipynb)
47 | - [Binary Tree](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/binaryTree.ipynb)
48 |
49 | - Cấu trúc dữ liệu nâng cao
50 | - [Heap (Đống)](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/heap.ipynb)
51 | - **d. Vòng lặp**
52 | - [Hàm While và For In](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/loop.ipynb)
53 | - **e. Hàm**
54 | - [Hàm và các vấn đề liên quan](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/function.ipynb)
55 | - **f. Thuật toán**
56 | - [Dijkstra](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/dijkstra.ipynb): thuật toán tìm đường đi ngắn nhất từ một điểm bất kỳ đến các điểm còn lại
57 | - [Linear + Binary Search](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/python-tutorials/BinarySearch.ipynb)
58 | - [Backtracking](https://github.com/bangoc123/learn-machine-learning-in-two-months/blob/master/algorithms/graph/backtracking/backtracking.MD)
59 |
--------------------------------------------------------------------------------
/python-tutorials/Recursion.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Đệ Quy (Recursion)"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Đệ quy là một trong những thuật toán rất mạnh mẽ, đôi khi code rất ngắn nhưng có thể giải quyết những bài toán tưởng chừng như không thể nếu sử dụng các phương pháp khác."
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "#### 1. Cấu trúc đệ quy"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "Ý tưởng này được trích xuất từ cuốn: Programming abstractions in C++ - Eric Roberts
\n"
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | "
"
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | "\n",
43 | "#### 2. Ví dụ"
44 | ]
45 | },
46 | {
47 | "cell_type": "markdown",
48 | "metadata": {},
49 | "source": [
50 | "Hàm tính giai thừa **(factorial function)**. Viết hàm có thể tính được n!"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | "##### 2.1 Cách thông thường"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 11,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "n = 10\n",
67 | "def factorialFunc(n):\n",
68 | " final = 1\n",
69 | " for i in range(1, n+1):\n",
70 | " final = final * i\n",
71 | " return final\n"
72 | ]
73 | },
74 | {
75 | "cell_type": "code",
76 | "execution_count": 12,
77 | "metadata": {},
78 | "outputs": [
79 | {
80 | "data": {
81 | "text/plain": [
82 | "3628800"
83 | ]
84 | },
85 | "execution_count": 12,
86 | "metadata": {},
87 | "output_type": "execute_result"
88 | }
89 | ],
90 | "source": [
91 | "# Test\n",
92 | "factorialFunc(10)"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {},
98 | "source": [
99 | "##### 2.2 Sử dụng đệ quy"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 16,
105 | "metadata": {},
106 | "outputs": [],
107 | "source": [
108 | "def factorialFuncRec(n):\n",
109 | " if(n==1):\n",
110 | " return 1\n",
111 | " else:\n",
112 | " return n * factorialFuncRec(n-1)\n"
113 | ]
114 | },
115 | {
116 | "cell_type": "code",
117 | "execution_count": 14,
118 | "metadata": {},
119 | "outputs": [
120 | {
121 | "data": {
122 | "text/plain": [
123 | "3628800"
124 | ]
125 | },
126 | "execution_count": 14,
127 | "metadata": {},
128 | "output_type": "execute_result"
129 | }
130 | ],
131 | "source": [
132 | "# Test\n",
133 | "factorialFuncRec(10)"
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "metadata": {},
139 | "source": [
140 | "
"
141 | ]
142 | },
143 | {
144 | "cell_type": "code",
145 | "execution_count": null,
146 | "metadata": {},
147 | "outputs": [],
148 | "source": []
149 | }
150 | ],
151 | "metadata": {
152 | "kernelspec": {
153 | "display_name": "Python 2",
154 | "language": "python",
155 | "name": "python2"
156 | },
157 | "language_info": {
158 | "codemirror_mode": {
159 | "name": "ipython",
160 | "version": 2
161 | },
162 | "file_extension": ".py",
163 | "mimetype": "text/x-python",
164 | "name": "python",
165 | "nbconvert_exporter": "python",
166 | "pygments_lexer": "ipython2",
167 | "version": "2.7.16"
168 | }
169 | },
170 | "nbformat": 4,
171 | "nbformat_minor": 2
172 | }
173 |
--------------------------------------------------------------------------------
/python-tutorials/Trie.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Tạo cây và thêm từ"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "class Node:\n",
17 | " def __init__(self):\n",
18 | " self.next = {}\n",
19 | " self.cnt = 0"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": 2,
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "def addString(root, st):\n",
29 | " cur = root\n",
30 | " for letter in st:\n",
31 | " if letter not in cur.next:\n",
32 | " cur.next[letter] = Node()\n",
33 | " cur = cur.next[letter]\n",
34 | " cur.cnt += 1"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "execution_count": 3,
40 | "metadata": {},
41 | "outputs": [],
42 | "source": [
43 | "root = Node()"
44 | ]
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 4,
49 | "metadata": {},
50 | "outputs": [],
51 | "source": [
52 | "addString(root, 'ProtonX')"
53 | ]
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 5,
58 | "metadata": {},
59 | "outputs": [],
60 | "source": [
61 | "addString(root, 'Python2')"
62 | ]
63 | },
64 | {
65 | "cell_type": "code",
66 | "execution_count": 6,
67 | "metadata": {},
68 | "outputs": [],
69 | "source": [
70 | "addString(root, 'Python3')"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 7,
76 | "metadata": {},
77 | "outputs": [],
78 | "source": [
79 | "addString(root, 'ProtonX4')"
80 | ]
81 | },
82 | {
83 | "cell_type": "markdown",
84 | "metadata": {},
85 | "source": [
86 | "### In tất cả các chuỗi trong Trie"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 8,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "def printTrie(root, st):\n",
96 | " if root is None:\n",
97 | " return\n",
98 | " if root.cnt > 0:\n",
99 | " print(st)\n",
100 | " for letter in root.next:\n",
101 | " printTrie(root.next[letter], st + letter)"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 9,
107 | "metadata": {},
108 | "outputs": [
109 | {
110 | "name": "stdout",
111 | "output_type": "stream",
112 | "text": [
113 | "ProtonX\n",
114 | "ProtonX4\n",
115 | "Python2\n",
116 | "Python3\n"
117 | ]
118 | }
119 | ],
120 | "source": [
121 | "printTrie(root, \"\")"
122 | ]
123 | },
124 | {
125 | "cell_type": "markdown",
126 | "metadata": {},
127 | "source": [
128 | "### Tìm Chuỗi "
129 | ]
130 | },
131 | {
132 | "cell_type": "code",
133 | "execution_count": 10,
134 | "metadata": {},
135 | "outputs": [],
136 | "source": [
137 | "def search(root, st):\n",
138 | " cur = root\n",
139 | " for letter in st:\n",
140 | " if letter not in cur.next:\n",
141 | " return -1\n",
142 | " cur = cur.next[letter]\n",
143 | " if cur.cnt >= 1:\n",
144 | " return cur.cnt\n",
145 | " else:\n",
146 | " return -1"
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 11,
152 | "metadata": {},
153 | "outputs": [
154 | {
155 | "data": {
156 | "text/plain": [
157 | "-1"
158 | ]
159 | },
160 | "execution_count": 11,
161 | "metadata": {},
162 | "output_type": "execute_result"
163 | }
164 | ],
165 | "source": [
166 | "search(root, \"Python\")"
167 | ]
168 | },
169 | {
170 | "cell_type": "code",
171 | "execution_count": 12,
172 | "metadata": {},
173 | "outputs": [
174 | {
175 | "data": {
176 | "text/plain": [
177 | "1"
178 | ]
179 | },
180 | "execution_count": 12,
181 | "metadata": {},
182 | "output_type": "execute_result"
183 | }
184 | ],
185 | "source": [
186 | "search(root, \"Python2\")"
187 | ]
188 | },
189 | {
190 | "cell_type": "code",
191 | "execution_count": 13,
192 | "metadata": {},
193 | "outputs": [
194 | {
195 | "data": {
196 | "text/plain": [
197 | "1"
198 | ]
199 | },
200 | "execution_count": 13,
201 | "metadata": {},
202 | "output_type": "execute_result"
203 | }
204 | ],
205 | "source": [
206 | "search(root, \"Python3\")"
207 | ]
208 | },
209 | {
210 | "cell_type": "code",
211 | "execution_count": 14,
212 | "metadata": {},
213 | "outputs": [
214 | {
215 | "data": {
216 | "text/plain": [
217 | "-1"
218 | ]
219 | },
220 | "execution_count": 14,
221 | "metadata": {},
222 | "output_type": "execute_result"
223 | }
224 | ],
225 | "source": [
226 | "search(root, \"Python4\")"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": 15,
232 | "metadata": {},
233 | "outputs": [
234 | {
235 | "data": {
236 | "text/plain": [
237 | "-1"
238 | ]
239 | },
240 | "execution_count": 15,
241 | "metadata": {},
242 | "output_type": "execute_result"
243 | }
244 | ],
245 | "source": [
246 | "search(root, \"Py\")"
247 | ]
248 | },
249 | {
250 | "cell_type": "markdown",
251 | "metadata": {},
252 | "source": [
253 | "### Xóa chuỗi"
254 | ]
255 | },
256 | {
257 | "cell_type": "code",
258 | "execution_count": 16,
259 | "metadata": {},
260 | "outputs": [],
261 | "source": [
262 | "# Kiểm tra root có phải là lá hay không\n",
263 | "def isEmpty(root):\n",
264 | " return len(root.next) == 0"
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": 17,
270 | "metadata": {},
271 | "outputs": [],
272 | "source": [
273 | "def removeString(root, st, depth):\n",
274 | " if not root:\n",
275 | " return None\n",
276 | " \n",
277 | " # Vị trí đang xét là vị trí cuối cùng của chuối\n",
278 | " if depth == len(st):\n",
279 | " # Nếu là tiền tố hoặc có nhiều chuỗi thì chỉ trừ đi 1\n",
280 | " if root.cnt >= 1:\n",
281 | " root.cnt = 0\n",
282 | " \n",
283 | " # Không phải tiền tố của chuỗi khác thì xóa luôn\n",
284 | " if(isEmpty(root)):\n",
285 | " del root\n",
286 | " root = None\n",
287 | " \n",
288 | " return root\n",
289 | " \n",
290 | " \n",
291 | " letter = st[depth]\n",
292 | " \n",
293 | " root.next[letter] = removeString(root.next[letter], st, depth + 1)\n",
294 | " \n",
295 | " # Trong trường hợp chuỗi này chứa một chuỗi khác thì chỉ xóa đến đoạn mà chuỗi này\n",
296 | " # không bao gồm chuỗi tiền tố thôi\n",
297 | " if isEmpty(root) and root.cnt == 0:\n",
298 | " del root\n",
299 | " root = None\n",
300 | " return root"
301 | ]
302 | },
303 | {
304 | "cell_type": "code",
305 | "execution_count": 18,
306 | "metadata": {},
307 | "outputs": [],
308 | "source": [
309 | "root = Node()\n",
310 | "addString(root, 'Pro')\n",
311 | "addString(root, 'ProtonX')\n",
312 | "addString(root, 'ProtonX')\n",
313 | "addString(root, 'ProtonX')\n",
314 | "addString(root, 'ProtonX')\n",
315 | "addString(root, 'Python2')\n",
316 | "addString(root, 'Python3')\n",
317 | "addString(root, 'ProtonX4')"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": 19,
323 | "metadata": {},
324 | "outputs": [
325 | {
326 | "name": "stdout",
327 | "output_type": "stream",
328 | "text": [
329 | "Pro\n",
330 | "ProtonX\n",
331 | "ProtonX4\n",
332 | "Python2\n",
333 | "Python3\n"
334 | ]
335 | }
336 | ],
337 | "source": [
338 | "printTrie(root, \"\")"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 20,
344 | "metadata": {},
345 | "outputs": [
346 | {
347 | "data": {
348 | "text/plain": [
349 | "<__main__.Node at 0x5a9bc58>"
350 | ]
351 | },
352 | "execution_count": 20,
353 | "metadata": {},
354 | "output_type": "execute_result"
355 | }
356 | ],
357 | "source": [
358 | "removeString(root, \"ProtonX\", 0)"
359 | ]
360 | },
361 | {
362 | "cell_type": "code",
363 | "execution_count": 23,
364 | "metadata": {},
365 | "outputs": [
366 | {
367 | "name": "stdout",
368 | "output_type": "stream",
369 | "text": [
370 | "ProtonX4\n",
371 | "Python2\n",
372 | "Python3\n"
373 | ]
374 | }
375 | ],
376 | "source": [
377 | "printTrie(root, \"\")"
378 | ]
379 | },
380 | {
381 | "cell_type": "code",
382 | "execution_count": 24,
383 | "metadata": {},
384 | "outputs": [
385 | {
386 | "data": {
387 | "text/plain": [
388 | "<__main__.Node at 0x5a9bc58>"
389 | ]
390 | },
391 | "execution_count": 24,
392 | "metadata": {},
393 | "output_type": "execute_result"
394 | }
395 | ],
396 | "source": [
397 | "removeString(root, \"Pro\", 0)"
398 | ]
399 | },
400 | {
401 | "cell_type": "code",
402 | "execution_count": 25,
403 | "metadata": {},
404 | "outputs": [
405 | {
406 | "name": "stdout",
407 | "output_type": "stream",
408 | "text": [
409 | "ProtonX4\n",
410 | "Python2\n",
411 | "Python3\n"
412 | ]
413 | }
414 | ],
415 | "source": [
416 | "printTrie(root, \"\")"
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "execution_count": null,
422 | "metadata": {},
423 | "outputs": [],
424 | "source": []
425 | }
426 | ],
427 | "metadata": {
428 | "kernelspec": {
429 | "display_name": "Python 3",
430 | "language": "python",
431 | "name": "python3"
432 | },
433 | "language_info": {
434 | "codemirror_mode": {
435 | "name": "ipython",
436 | "version": 3
437 | },
438 | "file_extension": ".py",
439 | "mimetype": "text/x-python",
440 | "name": "python",
441 | "nbconvert_exporter": "python",
442 | "pygments_lexer": "ipython3",
443 | "version": "3.8.2"
444 | }
445 | },
446 | "nbformat": 4,
447 | "nbformat_minor": 4
448 | }
449 |
--------------------------------------------------------------------------------
/python-tutorials/bellman-ford.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 95,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "numberOfVertices = 7"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 96,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "edges = [\n",
19 | " [0, 4, 6],\n",
20 | " [0, 5, 3],\n",
21 | " [5, 4, -5],\n",
22 | " [5, 6, 9],\n",
23 | " [6, 5, 1],\n",
24 | " [4, 6, -10],\n",
25 | " [3, 4, 7],\n",
26 | " [3, 6, 6],\n",
27 | " [4, 1, 2],\n",
28 | " [1, 2, -1],\n",
29 | " [2, 3, -8]\n",
30 | "]"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "execution_count": 97,
36 | "metadata": {},
37 | "outputs": [],
38 | "source": [
39 | "INF = float('inf')"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 98,
45 | "metadata": {},
46 | "outputs": [],
47 | "source": [
48 | "dist = [INF for _ in range(numberOfVertices)]"
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 99,
54 | "metadata": {},
55 | "outputs": [
56 | {
57 | "data": {
58 | "text/plain": [
59 | "[inf, inf, inf, inf, inf, inf, inf]"
60 | ]
61 | },
62 | "execution_count": 99,
63 | "metadata": {},
64 | "output_type": "execute_result"
65 | }
66 | ],
67 | "source": [
68 | "dist"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 100,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "path = [-1 for _ in range(numberOfVertices)]"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 101,
83 | "metadata": {},
84 | "outputs": [],
85 | "source": [
86 | "def bellmanFord(src, edges, dist, path):\n",
87 | " dist[src] = 0\n",
88 | " for i in range(1, numberOfVertices):\n",
89 | " for edge in edges:\n",
90 | " s, e, w = edge\n",
91 | " if dist[s] != INF and dist[s] + w < dist[e]:\n",
92 | " dist[e] = dist[s] + w\n",
93 | " path[e] = s\n",
94 | " \n",
95 | " \n",
96 | " # Check has a cycle\n",
97 | " for edge in edges:\n",
98 | " s, e, w = edge\n",
99 | " if dist[s] != INF and dist[s] + w < dist[e]:\n",
100 | " print(\"Has a cycle here\")\n",
101 | " # Do sthing\n"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 102,
107 | "metadata": {},
108 | "outputs": [
109 | {
110 | "name": "stdout",
111 | "output_type": "stream",
112 | "text": [
113 | "Has a cycle here\n"
114 | ]
115 | }
116 | ],
117 | "source": [
118 | "bellmanFord(0, edges, dist, path)"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": 103,
124 | "metadata": {},
125 | "outputs": [
126 | {
127 | "data": {
128 | "text/plain": [
129 | "[0, -28, -29, -37, -30, -39, -40]"
130 | ]
131 | },
132 | "execution_count": 103,
133 | "metadata": {},
134 | "output_type": "execute_result"
135 | }
136 | ],
137 | "source": [
138 | "dist"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": 104,
144 | "metadata": {},
145 | "outputs": [
146 | {
147 | "data": {
148 | "text/plain": [
149 | "[-1, 4, 1, 2, 5, 6, 4]"
150 | ]
151 | },
152 | "execution_count": 104,
153 | "metadata": {},
154 | "output_type": "execute_result"
155 | }
156 | ],
157 | "source": [
158 | "path"
159 | ]
160 | }
161 | ],
162 | "metadata": {
163 | "kernelspec": {
164 | "display_name": "Python 3",
165 | "language": "python",
166 | "name": "python3"
167 | },
168 | "language_info": {
169 | "codemirror_mode": {
170 | "name": "ipython",
171 | "version": 3
172 | },
173 | "file_extension": ".py",
174 | "mimetype": "text/x-python",
175 | "name": "python",
176 | "nbconvert_exporter": "python",
177 | "pygments_lexer": "ipython3",
178 | "version": "3.8.2"
179 | }
180 | },
181 | "nbformat": 4,
182 | "nbformat_minor": 4
183 | }
184 |
--------------------------------------------------------------------------------
/python-tutorials/binary-tree.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [],
3 | "metadata": {},
4 | "nbformat": 4,
5 | "nbformat_minor": 2
6 | }
7 |
--------------------------------------------------------------------------------
/python-tutorials/binaryTree.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "Cây có rất nhiều ứng dụng\n",
8 | " - HTML để hiện thị trang web chúng ta đang dùng cũng là một cây được gọi là cây HTML\n",
9 | " - Quản lý vị trí trong công ty cũng là quản lý theo cây"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": null,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "Cây khác duy nhất đồ thị (Graph) đó là cây không có chu trình(Cir)"
19 | ]
20 | },
21 | {
22 | "cell_type": "markdown",
23 | "metadata": {},
24 | "source": [
25 | "### Cây nhị phân ( Binary Tree )"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {},
31 | "source": [
32 | "Cây nhị phân có nhiều nhất 2 nhánh con"
33 | ]
34 | },
35 | {
36 | "cell_type": "markdown",
37 | "metadata": {},
38 | "source": [
39 | "#### 1. Khởi tao cây từ một list"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | ""
47 | ]
48 | },
49 | {
50 | "cell_type": "code",
51 | "execution_count": 325,
52 | "metadata": {},
53 | "outputs": [],
54 | "source": [
55 | "data = [0, 1, 2, 3, 4, None, 5, None, None, 6, 7, None, None, 8, None]"
56 | ]
57 | },
58 | {
59 | "cell_type": "code",
60 | "execution_count": 326,
61 | "metadata": {},
62 | "outputs": [],
63 | "source": [
64 | "class Node:\n",
65 | " def __init__(self, val):\n",
66 | " self.left = None\n",
67 | " self.right = None\n",
68 | " self.val = val\n",
69 | "\n",
70 | " def __repr__(self):\n",
71 | " return str(self.val)\n",
72 | "\n",
73 | "def buildTree(data):\n",
74 | " if len(data) == 0:\n",
75 | " return Node()\n",
76 | " i = 0\n",
77 | " n = len(data)\n",
78 | " root = Node(data[0]) \n",
79 | " def insertNode(root, i, n):\n",
80 | " left_idx = 2 * i + 1\n",
81 | " right_idx = 2 * i + 2\n",
82 | "\n",
83 | " if left_idx < n:\n",
84 | " left_val = data[left_idx]\n",
85 | " left_node = Node(left_val)\n",
86 | " root.left = insertNode(left_node, left_idx, n)\n",
87 | "\n",
88 | "\n",
89 | " if right_idx < n:\n",
90 | " right_val = data[right_idx]\n",
91 | " right_node = Node(right_val)\n",
92 | " root.right = insertNode(right_node, right_idx, n)\n",
93 | "\n",
94 | " \n",
95 | " return root\n",
96 | " \n",
97 | " tree = insertNode(root, 0, n)\n",
98 | " return tree\n",
99 | " "
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 327,
105 | "metadata": {
106 | "scrolled": true
107 | },
108 | "outputs": [],
109 | "source": [
110 | "tree = buildTree(data)"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 328,
116 | "metadata": {
117 | "scrolled": true
118 | },
119 | "outputs": [
120 | {
121 | "data": {
122 | "text/plain": [
123 | "2"
124 | ]
125 | },
126 | "execution_count": 328,
127 | "metadata": {},
128 | "output_type": "execute_result"
129 | }
130 | ],
131 | "source": [
132 | "tree.right"
133 | ]
134 | },
135 | {
136 | "cell_type": "markdown",
137 | "metadata": {},
138 | "source": [
139 | "#### 2. Duyệt tiền thứ tự (Preorder Traversal)"
140 | ]
141 | },
142 | {
143 | "cell_type": "markdown",
144 | "metadata": {},
145 | "source": [
146 | ""
147 | ]
148 | },
149 | {
150 | "cell_type": "code",
151 | "execution_count": 329,
152 | "metadata": {},
153 | "outputs": [],
154 | "source": [
155 | "def preorderTraversal(tree):\n",
156 | " if tree == None:\n",
157 | " return\n",
158 | " if tree.val != None:\n",
159 | " print(tree.val)\n",
160 | " preorderTraversal(tree.left)\n",
161 | " preorderTraversal(tree.right) "
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": 330,
167 | "metadata": {},
168 | "outputs": [
169 | {
170 | "name": "stdout",
171 | "output_type": "stream",
172 | "text": [
173 | "0\n",
174 | "1\n",
175 | "3\n",
176 | "4\n",
177 | "6\n",
178 | "7\n",
179 | "2\n",
180 | "5\n",
181 | "8\n"
182 | ]
183 | }
184 | ],
185 | "source": [
186 | "preorderTraversal(tree)"
187 | ]
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {},
192 | "source": [
193 | "#### 3. Duyệt trung thứ tự (Inorder Traversal) "
194 | ]
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "metadata": {},
199 | "source": [
200 | ""
201 | ]
202 | },
203 | {
204 | "cell_type": "code",
205 | "execution_count": 321,
206 | "metadata": {},
207 | "outputs": [],
208 | "source": [
209 | "def inorderTraversal(tree):\n",
210 | " if tree == None:\n",
211 | " return\n",
212 | " inorderTraversal(tree.left)\n",
213 | " if tree.val != None:\n",
214 | " print(tree.val)\n",
215 | " inorderTraversal(tree.right)"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": 322,
221 | "metadata": {},
222 | "outputs": [
223 | {
224 | "name": "stdout",
225 | "output_type": "stream",
226 | "text": [
227 | "3\n",
228 | "1\n",
229 | "6\n",
230 | "4\n",
231 | "7\n",
232 | "0\n",
233 | "2\n",
234 | "8\n",
235 | "5\n"
236 | ]
237 | }
238 | ],
239 | "source": [
240 | "inorderTraversal(tree)"
241 | ]
242 | },
243 | {
244 | "cell_type": "markdown",
245 | "metadata": {},
246 | "source": [
247 | "#### 4. Duyệt hậu thứ tự (Inorder Traversal) "
248 | ]
249 | },
250 | {
251 | "cell_type": "markdown",
252 | "metadata": {},
253 | "source": [
254 | ""
255 | ]
256 | },
257 | {
258 | "cell_type": "code",
259 | "execution_count": 333,
260 | "metadata": {},
261 | "outputs": [],
262 | "source": [
263 | "def postorderTraversal(tree):\n",
264 | " if tree == None:\n",
265 | " return\n",
266 | " postorderTraversal(tree.left)\n",
267 | " postorderTraversal(tree.right)\n",
268 | " if tree.val != None:\n",
269 | " print(tree.val)"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 334,
275 | "metadata": {},
276 | "outputs": [
277 | {
278 | "name": "stdout",
279 | "output_type": "stream",
280 | "text": [
281 | "3\n",
282 | "6\n",
283 | "7\n",
284 | "4\n",
285 | "1\n",
286 | "8\n",
287 | "5\n",
288 | "2\n",
289 | "0\n"
290 | ]
291 | }
292 | ],
293 | "source": [
294 | "postorderTraversal(tree)"
295 | ]
296 | }
297 | ],
298 | "metadata": {
299 | "kernelspec": {
300 | "display_name": "Python 3",
301 | "language": "python",
302 | "name": "python3"
303 | },
304 | "language_info": {
305 | "codemirror_mode": {
306 | "name": "ipython",
307 | "version": 3
308 | },
309 | "file_extension": ".py",
310 | "mimetype": "text/x-python",
311 | "name": "python",
312 | "nbconvert_exporter": "python",
313 | "pygments_lexer": "ipython3",
314 | "version": "3.8.2"
315 | }
316 | },
317 | "nbformat": 4,
318 | "nbformat_minor": 2
319 | }
320 |
--------------------------------------------------------------------------------
/python-tutorials/dijkstra.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | ""
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | ""
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | ""
29 | ]
30 | },
31 | {
32 | "cell_type": "markdown",
33 | "metadata": {},
34 | "source": [
35 | ""
36 | ]
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "metadata": {},
41 | "source": [
42 | ""
43 | ]
44 | },
45 | {
46 | "cell_type": "markdown",
47 | "metadata": {},
48 | "source": [
49 | ""
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "metadata": {},
55 | "source": [
56 | ""
57 | ]
58 | },
59 | {
60 | "cell_type": "markdown",
61 | "metadata": {},
62 | "source": [
63 | ""
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "metadata": {},
69 | "source": [
70 | ""
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "metadata": {},
76 | "source": [
77 | ""
78 | ]
79 | },
80 | {
81 | "cell_type": "markdown",
82 | "metadata": {},
83 | "source": [
84 | ""
85 | ]
86 | },
87 | {
88 | "cell_type": "code",
89 | "execution_count": 1,
90 | "metadata": {},
91 | "outputs": [],
92 | "source": [
93 | "import collections"
94 | ]
95 | },
96 | {
97 | "cell_type": "code",
98 | "execution_count": 2,
99 | "metadata": {},
100 | "outputs": [],
101 | "source": [
102 | "import queue"
103 | ]
104 | },
105 | {
106 | "cell_type": "markdown",
107 | "metadata": {},
108 | "source": [
109 | "#### Danh sách cạnh kề"
110 | ]
111 | },
112 | {
113 | "cell_type": "code",
114 | "execution_count": 3,
115 | "metadata": {},
116 | "outputs": [],
117 | "source": [
118 | "N = 7"
119 | ]
120 | },
121 | {
122 | "cell_type": "code",
123 | "execution_count": 4,
124 | "metadata": {},
125 | "outputs": [],
126 | "source": [
127 | "edges = [\n",
128 | " [0, 2, 1],\n",
129 | " [0, 1, 2],\n",
130 | " [0, 6, 3],\n",
131 | " [1, 5, 10],\n",
132 | " [1, 4, 15],\n",
133 | " [2, 1, 4],\n",
134 | " [2, 3, 2],\n",
135 | " [3, 4, 3],\n",
136 | " [4, 5, 5],\n",
137 | " [6, 1, 3]\n",
138 | "]"
139 | ]
140 | },
141 | {
142 | "cell_type": "markdown",
143 | "metadata": {},
144 | "source": [
145 | "#### Khởi tạo graph"
146 | ]
147 | },
148 | {
149 | "cell_type": "code",
150 | "execution_count": 5,
151 | "metadata": {},
152 | "outputs": [],
153 | "source": [
154 | "graph = collections.defaultdict(list)"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": 6,
160 | "metadata": {},
161 | "outputs": [],
162 | "source": [
163 | "for edge in edges:\n",
164 | " src, des, weight = edge\n",
165 | " graph[src].append((des, weight))"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 7,
171 | "metadata": {},
172 | "outputs": [
173 | {
174 | "data": {
175 | "text/plain": [
176 | "defaultdict(list,\n",
177 | " {0: [(2, 1), (1, 2), (6, 3)],\n",
178 | " 1: [(5, 10), (4, 15)],\n",
179 | " 2: [(1, 4), (3, 2)],\n",
180 | " 3: [(4, 3)],\n",
181 | " 4: [(5, 5)],\n",
182 | " 6: [(1, 3)]})"
183 | ]
184 | },
185 | "execution_count": 7,
186 | "metadata": {},
187 | "output_type": "execute_result"
188 | }
189 | ],
190 | "source": [
191 | "graph"
192 | ]
193 | },
194 | {
195 | "cell_type": "markdown",
196 | "metadata": {},
197 | "source": [
198 | "#### Khởi tạo bảng độ dài từ gốc đến một điểm bất kỳ"
199 | ]
200 | },
201 | {
202 | "cell_type": "code",
203 | "execution_count": 8,
204 | "metadata": {},
205 | "outputs": [],
206 | "source": [
207 | "dist = collections.defaultdict(int)"
208 | ]
209 | },
210 | {
211 | "cell_type": "code",
212 | "execution_count": 9,
213 | "metadata": {},
214 | "outputs": [],
215 | "source": [
216 | "for i in range(N):\n",
217 | " dist[i] = float('inf')"
218 | ]
219 | },
220 | {
221 | "cell_type": "markdown",
222 | "metadata": {},
223 | "source": [
224 | "#### Khởi tạo bảng đánh dấu cha của một điểm bất kỳ trên đường đi"
225 | ]
226 | },
227 | {
228 | "cell_type": "code",
229 | "execution_count": 10,
230 | "metadata": {},
231 | "outputs": [],
232 | "source": [
233 | "path = collections.defaultdict(int)"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": 11,
239 | "metadata": {},
240 | "outputs": [],
241 | "source": [
242 | "for i in range(N):\n",
243 | " path[i] = -1"
244 | ]
245 | },
246 | {
247 | "cell_type": "markdown",
248 | "metadata": {},
249 | "source": [
250 | "#### Khởi tạo bảng độ dài từ gốc đến một điểm bất kỳ"
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 12,
256 | "metadata": {},
257 | "outputs": [],
258 | "source": [
259 | "class HeapNode:\n",
260 | " def __init__(self, id, weight):\n",
261 | " self.id = id\n",
262 | " self.weight = weight\n",
263 | " \n",
264 | " def __lt__(self, other):\n",
265 | " return self.weight <= other.weight"
266 | ]
267 | },
268 | {
269 | "cell_type": "code",
270 | "execution_count": 15,
271 | "metadata": {},
272 | "outputs": [],
273 | "source": [
274 | "def dijkstra(src, graph, dist, path):\n",
275 | " minHeap = queue.PriorityQueue()\n",
276 | "# src = 0\n",
277 | " dist[src] = 0\n",
278 | " minHeap.put(HeapNode(src, 0))\n",
279 | " while minHeap.qsize() > 0:\n",
280 | " u = minHeap.get()\n",
281 | " for v in graph[u.id]:\n",
282 | " vID, vWeight = v\n",
283 | " # Nếu tìm được đường nhỏ hơn từ gốc đến một điểm bất kỳ thì cập nhật lại độ dài và đường đi\n",
284 | " if u.weight + vWeight < dist[vID]:\n",
285 | " dist[vID] = u.weight + vWeight\n",
286 | " path[vID] = u.id\n",
287 | " minHeap.put(HeapNode(vID, u.weight + vWeight))\n",
288 | " "
289 | ]
290 | },
291 | {
292 | "cell_type": "code",
293 | "execution_count": 16,
294 | "metadata": {},
295 | "outputs": [],
296 | "source": [
297 | "dijkstra(0, graph, dist, path)"
298 | ]
299 | },
300 | {
301 | "cell_type": "code",
302 | "execution_count": 17,
303 | "metadata": {},
304 | "outputs": [
305 | {
306 | "data": {
307 | "text/plain": [
308 | "defaultdict(int, {0: 0, 1: 2, 2: 1, 3: 3, 4: 6, 5: 11, 6: 3})"
309 | ]
310 | },
311 | "execution_count": 17,
312 | "metadata": {},
313 | "output_type": "execute_result"
314 | }
315 | ],
316 | "source": [
317 | "dist"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": 18,
323 | "metadata": {},
324 | "outputs": [
325 | {
326 | "data": {
327 | "text/plain": [
328 | "defaultdict(int, {0: -1, 1: 0, 2: 0, 3: 2, 4: 3, 5: 4, 6: 0})"
329 | ]
330 | },
331 | "execution_count": 18,
332 | "metadata": {},
333 | "output_type": "execute_result"
334 | }
335 | ],
336 | "source": [
337 | "path"
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": null,
343 | "metadata": {},
344 | "outputs": [],
345 | "source": []
346 | }
347 | ],
348 | "metadata": {
349 | "kernelspec": {
350 | "display_name": "Python 3",
351 | "language": "python",
352 | "name": "python3"
353 | },
354 | "language_info": {
355 | "codemirror_mode": {
356 | "name": "ipython",
357 | "version": 3
358 | },
359 | "file_extension": ".py",
360 | "mimetype": "text/x-python",
361 | "name": "python",
362 | "nbconvert_exporter": "python",
363 | "pygments_lexer": "ipython3",
364 | "version": "3.8.2"
365 | }
366 | },
367 | "nbformat": 4,
368 | "nbformat_minor": 4
369 | }
370 |
--------------------------------------------------------------------------------
/python-tutorials/images/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/14.png
--------------------------------------------------------------------------------
/python-tutorials/images/14_base.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/14_base.png
--------------------------------------------------------------------------------
/python-tutorials/images/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/15.png
--------------------------------------------------------------------------------
/python-tutorials/images/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/16.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/1.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/2.0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/2.0.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/2.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/3.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/4.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/5.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/6.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/7.png
--------------------------------------------------------------------------------
/python-tutorials/images/BST/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/BST/8.png
--------------------------------------------------------------------------------
/python-tutorials/images/DP/fm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/DP/fm.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/1.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/10.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/11.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/12.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/13.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/14.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/15.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/16.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/2.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/3.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/4.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/5.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/6.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/7.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/8.png
--------------------------------------------------------------------------------
/python-tutorials/images/Heap/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Heap/9.png
--------------------------------------------------------------------------------
/python-tutorials/images/Linkedlist/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Linkedlist/3.png
--------------------------------------------------------------------------------
/python-tutorials/images/Linkedlist/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Linkedlist/5.png
--------------------------------------------------------------------------------
/python-tutorials/images/Stack/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/Stack/1.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/1.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/2.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/3.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/4.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/5.png
--------------------------------------------------------------------------------
/python-tutorials/images/binarySearch/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/binarySearch/6.png
--------------------------------------------------------------------------------
/python-tutorials/images/dfs/island.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dfs/island.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/1.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/11.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/12.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/13.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/2.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/3.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/4.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/5.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/6.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/7.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/8.png
--------------------------------------------------------------------------------
/python-tutorials/images/dijkstra/9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/python-tutorials/images/dijkstra/9.png
--------------------------------------------------------------------------------
/python-tutorials/loop.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "## Loop\n",
8 | "----"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "metadata": {},
14 | "source": [
15 | ""
16 | ]
17 | },
18 | {
19 | "cell_type": "markdown",
20 | "metadata": {},
21 | "source": [
22 | "Ảnh từ [PenJee](https://blog.penjee.com/top-5-animated-gifs-explain-loops-python/)"
23 | ]
24 | },
25 | {
26 | "cell_type": "markdown",
27 | "metadata": {},
28 | "source": [
29 | "### I. While Loop"
30 | ]
31 | },
32 | {
33 | "cell_type": "markdown",
34 | "metadata": {},
35 | "source": [
36 | "##### 1. While với hàm print"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 1,
42 | "metadata": {},
43 | "outputs": [
44 | {
45 | "name": "stdout",
46 | "output_type": "stream",
47 | "text": [
48 | "1\n",
49 | "3\n",
50 | "5\n",
51 | "7\n",
52 | "9\n"
53 | ]
54 | }
55 | ],
56 | "source": [
57 | "a = 1\n",
58 | "while a < 10:\n",
59 | " print(a)\n",
60 | " a += 2"
61 | ]
62 | },
63 | {
64 | "cell_type": "markdown",
65 | "metadata": {},
66 | "source": [
67 | "Biến a sẽ tăng lên 2 từ 1 cho đến 9 "
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "metadata": {},
73 | "source": [
74 | "##### 2. While với hàm if"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": 5,
80 | "metadata": {
81 | "scrolled": true
82 | },
83 | "outputs": [
84 | {
85 | "name": "stdout",
86 | "output_type": "stream",
87 | "text": [
88 | "5 is one of 0 factors\n",
89 | "5 is one of 5 factors\n",
90 | "5 is one of 10 factors\n",
91 | "5 is one of 15 factors\n"
92 | ]
93 | }
94 | ],
95 | "source": [
96 | "a = 0\n",
97 | "while a < 20:\n",
98 | " if a % 5 == 0:\n",
99 | " print (\"5 is one of %d factors\" %a)\n",
100 | " a += 1"
101 | ]
102 | },
103 | {
104 | "cell_type": "markdown",
105 | "metadata": {},
106 | "source": [
107 | "##### 3. While với break\n",
108 | "Thoát khỏi vòng lặp"
109 | ]
110 | },
111 | {
112 | "cell_type": "code",
113 | "execution_count": 6,
114 | "metadata": {},
115 | "outputs": [],
116 | "source": [
117 | "names = [\"Hoàng\", \"Hòa\", \"Dương\", \"Hùng\", \"Ngọc\", \"Lâm\", \"Đạt\"]"
118 | ]
119 | },
120 | {
121 | "cell_type": "code",
122 | "execution_count": 7,
123 | "metadata": {},
124 | "outputs": [],
125 | "source": [
126 | "names_length = len(names)"
127 | ]
128 | },
129 | {
130 | "cell_type": "markdown",
131 | "metadata": {},
132 | "source": [
133 | "Khá vỡ vòng lặp khi đến phần tử Ngọc"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": 12,
139 | "metadata": {},
140 | "outputs": [
141 | {
142 | "name": "stdout",
143 | "output_type": "stream",
144 | "text": [
145 | "Hoàng\n",
146 | "Hòa\n",
147 | "Dương\n",
148 | "Hùng\n",
149 | "Ngọc\n"
150 | ]
151 | }
152 | ],
153 | "source": [
154 | "a = 0\n",
155 | "while a < names_length:\n",
156 | " print names[a]\n",
157 | " if names[a] == \"Ngọc\":\n",
158 | " break\n",
159 | " a += 1"
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "metadata": {},
165 | "source": [
166 | "##### 4. While else"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "metadata": {},
172 | "source": [
173 | "Hàm else sẽ được thực hiện khi a không còn nhỏ hơn 10"
174 | ]
175 | },
176 | {
177 | "cell_type": "code",
178 | "execution_count": 15,
179 | "metadata": {},
180 | "outputs": [
181 | {
182 | "name": "stdout",
183 | "output_type": "stream",
184 | "text": [
185 | "1\n",
186 | "3\n",
187 | "5\n",
188 | "7\n",
189 | "9\n",
190 | "End while\n"
191 | ]
192 | }
193 | ],
194 | "source": [
195 | "a = 1\n",
196 | "while a < 10:\n",
197 | " print(a)\n",
198 | " a += 2\n",
199 | "else:\n",
200 | " print \"End while\""
201 | ]
202 | },
203 | {
204 | "cell_type": "markdown",
205 | "metadata": {},
206 | "source": [
207 | "### II. For In"
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "metadata": {},
213 | "source": [
214 | "##### 1. For với hàm print"
215 | ]
216 | },
217 | {
218 | "cell_type": "code",
219 | "execution_count": 16,
220 | "metadata": {},
221 | "outputs": [],
222 | "source": [
223 | "office_stuffs = [\"Ballpoint Pen\", \"Pencil\", \"Glue Stick\", \"Erasor\", \"Pencil sharpener\", \"Stapler\", \"Scissors\"]"
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "execution_count": 17,
229 | "metadata": {},
230 | "outputs": [
231 | {
232 | "name": "stdout",
233 | "output_type": "stream",
234 | "text": [
235 | "Ballpoint Pen\n",
236 | "Pencil\n",
237 | "Glue Stick\n",
238 | "Erasor\n",
239 | "Pencil sharpener\n",
240 | "Stapler\n",
241 | "Scissors\n"
242 | ]
243 | }
244 | ],
245 | "source": [
246 | "for item in office_stuffs:\n",
247 | " print item"
248 | ]
249 | },
250 | {
251 | "cell_type": "markdown",
252 | "metadata": {},
253 | "source": [
254 | "##### 2. For với hàm else\n",
255 | "Hàm else sẽ được thực hiện khi hàm lặp kết thúc"
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": 19,
261 | "metadata": {},
262 | "outputs": [],
263 | "source": [
264 | "other_stuffs = [\"Paper Clip\", \"Binder Clip\", \"Pushpin\", \"Sticky labels\", \"Ring Binder\"]"
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": 21,
270 | "metadata": {
271 | "scrolled": true
272 | },
273 | "outputs": [
274 | {
275 | "name": "stdout",
276 | "output_type": "stream",
277 | "text": [
278 | "Paper Clip\n",
279 | "Binder Clip\n",
280 | "Pushpin\n",
281 | "Sticky labels\n",
282 | "Ring Binder\n",
283 | "End for loop\n"
284 | ]
285 | }
286 | ],
287 | "source": [
288 | "for item in other_stuffs:\n",
289 | " print item\n",
290 | "else:\n",
291 | " print \"End for loop\""
292 | ]
293 | },
294 | {
295 | "cell_type": "markdown",
296 | "metadata": {},
297 | "source": [
298 | "### III. Đào sâu "
299 | ]
300 | },
301 | {
302 | "cell_type": "markdown",
303 | "metadata": {},
304 | "source": [
305 | "Đọc thêm [Functions creating iterators for efficient looping](https://docs.python.org/2/library/itertools.html)"
306 | ]
307 | }
308 | ],
309 | "metadata": {
310 | "kernelspec": {
311 | "display_name": "Python 2",
312 | "language": "python",
313 | "name": "python2"
314 | },
315 | "language_info": {
316 | "codemirror_mode": {
317 | "name": "ipython",
318 | "version": 2
319 | },
320 | "file_extension": ".py",
321 | "mimetype": "text/x-python",
322 | "name": "python",
323 | "nbconvert_exporter": "python",
324 | "pygments_lexer": "ipython2",
325 | "version": "2.7.14"
326 | }
327 | },
328 | "nbformat": 4,
329 | "nbformat_minor": 2
330 | }
331 |
--------------------------------------------------------------------------------
/python-tutorials/replace.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Replace String only use another String"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | ""
15 | ]
16 | },
17 | {
18 | "cell_type": "code",
19 | "execution_count": 2,
20 | "metadata": {},
21 | "outputs": [
22 | {
23 | "name": "stdout",
24 | "output_type": "stream",
25 | "text": [
26 | "This is the original message used\n",
27 | "Thos os thi urogonel missegi asid\n",
28 | "disa igessim lenogoru iht so sohT\n"
29 | ]
30 | }
31 | ],
32 | "source": [
33 | "names = \"This is the original message used\"\n",
34 | "names2 = \"\"\n",
35 | "for i in range(len(names)):\n",
36 | " if names[i] == \"a\":\n",
37 | " names2 = names2 + \"e\"\n",
38 | " elif names[i] == \"e\":\n",
39 | " names2 = names2 + \"i\"\n",
40 | " elif names[i] == \"i\":\n",
41 | " names2 = names2 + \"o\"\n",
42 | " elif names[i] == \"o\":\n",
43 | " names2 = names2 + \"u\"\n",
44 | " elif names[i] == \"u\":\n",
45 | " names2 = names2 + \"a\"\n",
46 | " else:\n",
47 | " names2 = names2 + names[i]\n",
48 | "print(names)\n",
49 | "print(names2)\n",
50 | "print(names2[::-1])"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "metadata": {},
56 | "source": [
57 | ""
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 4,
63 | "metadata": {},
64 | "outputs": [],
65 | "source": [
66 | "# ignore special cases\n",
67 | "\n",
68 | "def get_num_str(n):\n",
69 | " chars = []\n",
70 | " if n >2:\n",
71 | " for num in range(1, n-1):\n",
72 | " chars.append(str(num))\n",
73 | " chars.append(str(num+2))\n",
74 | " return \"-\".join(chars)"
75 | ]
76 | },
77 | {
78 | "cell_type": "code",
79 | "execution_count": 5,
80 | "metadata": {},
81 | "outputs": [
82 | {
83 | "data": {
84 | "text/plain": [
85 | "'1-3'"
86 | ]
87 | },
88 | "execution_count": 5,
89 | "metadata": {},
90 | "output_type": "execute_result"
91 | }
92 | ],
93 | "source": [
94 | "get_num_str(3)"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": 6,
100 | "metadata": {},
101 | "outputs": [
102 | {
103 | "data": {
104 | "text/plain": [
105 | "'1-3-2-4-3-5-4-6'"
106 | ]
107 | },
108 | "execution_count": 6,
109 | "metadata": {},
110 | "output_type": "execute_result"
111 | }
112 | ],
113 | "source": [
114 | "get_num_str(6)"
115 | ]
116 | },
117 | {
118 | "cell_type": "code",
119 | "execution_count": null,
120 | "metadata": {},
121 | "outputs": [],
122 | "source": []
123 | }
124 | ],
125 | "metadata": {
126 | "kernelspec": {
127 | "display_name": "Python 3",
128 | "language": "python",
129 | "name": "python3"
130 | },
131 | "language_info": {
132 | "codemirror_mode": {
133 | "name": "ipython",
134 | "version": 3
135 | },
136 | "file_extension": ".py",
137 | "mimetype": "text/x-python",
138 | "name": "python",
139 | "nbconvert_exporter": "python",
140 | "pygments_lexer": "ipython3",
141 | "version": "3.8.2"
142 | }
143 | },
144 | "nbformat": 4,
145 | "nbformat_minor": 2
146 | }
147 |
--------------------------------------------------------------------------------
/python-tutorials/sort.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "### Bubble Sort"
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "Ý tưởng rất đơn giản: \n",
15 | " - Ta lặp qua từng phần tử một\n",
16 | " - Tại một phần tử\n",
17 | " - Ta lặp qua các phần tử còn lại\n",
18 | " - Nếu phần tử nào trong các phần tử còn lại mà nhỏ hơn phần tử hiện tại thì thay thế phần tử hiện tại bằng phần tử nhỏ hơn đó"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 95,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "numbers = [20, 10, 16, 6, 89]"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 99,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "for i in range(0, len(numbers)):\n",
37 | " for j in range(i+1, len(numbers)):\n",
38 | " if numbers[j] < numbers[i]:\n",
39 | " temp = numbers[j]\n",
40 | " numbers[j] = numbers[i]\n",
41 | " numbers[i] = temp\n",
42 | " "
43 | ]
44 | },
45 | {
46 | "cell_type": "code",
47 | "execution_count": 100,
48 | "metadata": {
49 | "scrolled": true
50 | },
51 | "outputs": [
52 | {
53 | "data": {
54 | "text/plain": [
55 | "[6, 10, 16, 20, 89]"
56 | ]
57 | },
58 | "execution_count": 100,
59 | "metadata": {},
60 | "output_type": "execute_result"
61 | }
62 | ],
63 | "source": [
64 | "numbers"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {},
70 | "source": [
71 | "Độ phức tạp: **O(N^2)**"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "### Insertion Sort"
79 | ]
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {},
84 | "source": [
85 | "Ý tưởng:\n",
86 | " - Ta lặp qua từng phần tử một:\n",
87 | " - Tại một phần tử\n",
88 | " - Ta lặp qua các phần tử ở trước phần tử này:\n",
89 | " - Tìm vị trí mà phần tử này lớn hơn các phần tử bên trên và nhỏ hơn các phần tử bên phải\n",
90 | " - Di chuyển tất cả các phần tử bên phải lên một bậc\n",
91 | " - Đặt phần tử này vào vị trí đó (Đây chính là ý nghĩa Insertion(chèn))"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": 124,
97 | "metadata": {},
98 | "outputs": [],
99 | "source": [
100 | "numbers = [100, 20, 10, 16, 6, 89]"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": 125,
106 | "metadata": {},
107 | "outputs": [],
108 | "source": [
109 | "for i in range(1, len(numbers)):\n",
110 | " j = i - 1\n",
111 | " curr = numbers[i]\n",
112 | " while j >= 0 and curr < numbers[j]:\n",
113 | " numbers[j + 1] = numbers[j]\n",
114 | " j -= 1\n",
115 | " numbers[j+1] = curr"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 126,
121 | "metadata": {},
122 | "outputs": [
123 | {
124 | "data": {
125 | "text/plain": [
126 | "[6, 10, 16, 20, 89, 100]"
127 | ]
128 | },
129 | "execution_count": 126,
130 | "metadata": {},
131 | "output_type": "execute_result"
132 | }
133 | ],
134 | "source": [
135 | "numbers"
136 | ]
137 | },
138 | {
139 | "cell_type": "markdown",
140 | "metadata": {},
141 | "source": [
142 | "Độ phức tạp: **O(N^2)**"
143 | ]
144 | },
145 | {
146 | "cell_type": "markdown",
147 | "metadata": {},
148 | "source": [
149 | "### Merge Sort"
150 | ]
151 | },
152 | {
153 | "cell_type": "markdown",
154 | "metadata": {},
155 | "source": [
156 | "Merge sort sử dụng giải thuật **divide and conquer.**"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": 85,
162 | "metadata": {},
163 | "outputs": [],
164 | "source": [
165 | "numbers = [20, 10, 16, 6, 89, 1, 5, 9, 100]"
166 | ]
167 | },
168 | {
169 | "cell_type": "code",
170 | "execution_count": 86,
171 | "metadata": {},
172 | "outputs": [],
173 | "source": [
174 | "# Divide\n",
175 | "def divide(numbers, start, end):\n",
176 | " if start == end:\n",
177 | " return [numbers[start]]\n",
178 | " else:\n",
179 | " middle = (start + end) // 2\n",
180 | " left_numbers = divide(numbers, start, middle) \n",
181 | " right_numbers = divide(numbers, middle+1, end)\n",
182 | " return conquer(numbers, left_numbers, right_numbers)"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": 87,
188 | "metadata": {},
189 | "outputs": [],
190 | "source": [
191 | "# Sort\n",
192 | "def conquer(numbers, left_numbers, right_numbers):\n",
193 | " new_nums = []\n",
194 | " i = 0\n",
195 | " j = 0\n",
196 | " while i < len(left_numbers) and j < len(right_numbers):\n",
197 | " if left_numbers[i] < right_numbers[j]:\n",
198 | " new_nums.append(left_numbers[i])\n",
199 | " i += 1\n",
200 | " else:\n",
201 | " new_nums.append(right_numbers[j])\n",
202 | " j += 1\n",
203 | " if i < len(left_numbers):\n",
204 | " new_nums += left_numbers[i:]\n",
205 | " \n",
206 | " if j < len(right_numbers):\n",
207 | " new_nums += right_numbers[j:]\n",
208 | " return new_nums"
209 | ]
210 | },
211 | {
212 | "cell_type": "code",
213 | "execution_count": 88,
214 | "metadata": {},
215 | "outputs": [
216 | {
217 | "data": {
218 | "text/plain": [
219 | "[1, 5, 6, 9, 10, 16, 20, 89, 100]"
220 | ]
221 | },
222 | "execution_count": 88,
223 | "metadata": {},
224 | "output_type": "execute_result"
225 | }
226 | ],
227 | "source": [
228 | "divide(numbers, 0, len(numbers)-1)"
229 | ]
230 | },
231 | {
232 | "cell_type": "markdown",
233 | "metadata": {},
234 | "source": [
235 | "Độ phức tạp: **O(Nlog(N))**"
236 | ]
237 | }
238 | ],
239 | "metadata": {
240 | "kernelspec": {
241 | "display_name": "Python 3",
242 | "language": "python",
243 | "name": "python3"
244 | },
245 | "language_info": {
246 | "codemirror_mode": {
247 | "name": "ipython",
248 | "version": 3
249 | },
250 | "file_extension": ".py",
251 | "mimetype": "text/x-python",
252 | "name": "python",
253 | "nbconvert_exporter": "python",
254 | "pygments_lexer": "ipython3",
255 | "version": "3.8.2"
256 | }
257 | },
258 | "nbformat": 4,
259 | "nbformat_minor": 4
260 | }
261 |
--------------------------------------------------------------------------------
/tensorflow/constant_addition.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | ""
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 103,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import tensorflow as tf"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {},
22 | "source": [
23 | "x và y được gọi là **Tensor**"
24 | ]
25 | },
26 | {
27 | "cell_type": "code",
28 | "execution_count": 104,
29 | "metadata": {},
30 | "outputs": [],
31 | "source": [
32 | "x = tf.constant(4) "
33 | ]
34 | },
35 | {
36 | "cell_type": "code",
37 | "execution_count": 105,
38 | "metadata": {},
39 | "outputs": [],
40 | "source": [
41 | "y = tf.constant(5) "
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "**Chú ý:** x và y **chưa có giá trị** cho đến khi phiên(session) được khởi tạo. Bạn có thể dùng hàm x.eval() để lấy giá trị khỏi Tensor."
49 | ]
50 | },
51 | {
52 | "cell_type": "code",
53 | "execution_count": 106,
54 | "metadata": {},
55 | "outputs": [],
56 | "source": [
57 | "z = tf.add(x, y) # Hàm cộng"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 107,
63 | "metadata": {},
64 | "outputs": [
65 | {
66 | "name": "stdout",
67 | "output_type": "stream",
68 | "text": [
69 | "9\n"
70 | ]
71 | }
72 | ],
73 | "source": [
74 | "with tf.Session() as sess:\n",
75 | " # x, y ở đây mới bắt đầu có giá trị\n",
76 | " result = sess.run(z)\n",
77 | " print(z.eval())\n",
78 | " "
79 | ]
80 | }
81 | ],
82 | "metadata": {
83 | "kernelspec": {
84 | "display_name": "Python 3",
85 | "language": "python",
86 | "name": "python3"
87 | },
88 | "language_info": {
89 | "codemirror_mode": {
90 | "name": "ipython",
91 | "version": 3
92 | },
93 | "file_extension": ".py",
94 | "mimetype": "text/x-python",
95 | "name": "python",
96 | "nbconvert_exporter": "python",
97 | "pygments_lexer": "ipython3",
98 | "version": "3.6.6"
99 | }
100 | },
101 | "nbformat": 4,
102 | "nbformat_minor": 2
103 | }
104 |
--------------------------------------------------------------------------------
/tensorflow/hidden_layer.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "import tensorflow as tf"
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 29,
15 | "metadata": {},
16 | "outputs": [],
17 | "source": [
18 | "import numpy as np"
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 2,
24 | "metadata": {},
25 | "outputs": [],
26 | "source": [
27 | "x = tf.placeholder(tf.float32, (100, 784))"
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 15,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "W = tf.Variable(tf.random_uniform((784, 100), -1, 1))"
37 | ]
38 | },
39 | {
40 | "cell_type": "code",
41 | "execution_count": 17,
42 | "metadata": {},
43 | "outputs": [],
44 | "source": [
45 | "b = tf.Variable(tf.zeros((100)))"
46 | ]
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "metadata": {},
51 | "source": [
52 | ""
53 | ]
54 | },
55 | {
56 | "cell_type": "markdown",
57 | "metadata": {},
58 | "source": [
59 | ""
60 | ]
61 | },
62 | {
63 | "cell_type": "code",
64 | "execution_count": 19,
65 | "metadata": {},
66 | "outputs": [],
67 | "source": [
68 | "h = tf.nn.relu(tf.matmul(x, W) + b)"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 21,
74 | "metadata": {},
75 | "outputs": [
76 | {
77 | "name": "stdout",
78 | "output_type": "stream",
79 | "text": [
80 | "Tensor(\"Relu:0\", shape=(100, 100), dtype=float32)\n"
81 | ]
82 | }
83 | ],
84 | "source": [
85 | "# Kiểm tra triều của h/\n",
86 | "print(h)"
87 | ]
88 | },
89 | {
90 | "cell_type": "code",
91 | "execution_count": 26,
92 | "metadata": {},
93 | "outputs": [],
94 | "source": [
95 | "sess = tf.Session()"
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "execution_count": 27,
101 | "metadata": {},
102 | "outputs": [],
103 | "source": [
104 | "sess.run(tf.initialize_all_variables())"
105 | ]
106 | },
107 | {
108 | "cell_type": "markdown",
109 | "metadata": {},
110 | "source": [
111 | "### **sess.run(fetches, feeds)**\n",
112 | "\n",
113 | " **fetches**: Liệt kê tất cả **các đỉnh (nodes)** bạn muốn trả về giá trị, trong trường hợp này tôi muốn trả về giá trị của h\n",
114 | " \n",
115 | " **feeds**: một dictionary chứa tên biến đầu vào và giá trị của nó truyền vào trong Graph, ở đây tôi có biến **x** và giá trị ngẫu nhiên khởi tạo bằng hàm np.random.random."
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 38,
121 | "metadata": {},
122 | "outputs": [
123 | {
124 | "data": {
125 | "text/plain": [
126 | "array([[ 0. , 9.201424 , 0. , ..., 0. , 0. ,\n",
127 | " 0. ],\n",
128 | " [ 0. , 13.298065 , 0. , ..., 0. , 1.2823265,\n",
129 | " 0. ],\n",
130 | " [ 0. , 4.288911 , 0. , ..., 6.110207 , 3.9334798,\n",
131 | " 0. ],\n",
132 | " ...,\n",
133 | " [ 0. , 4.820806 , 0. , ..., 4.610816 , 4.833015 ,\n",
134 | " 6.31176 ],\n",
135 | " [ 0. , 0. , 0. , ..., 9.902921 , 1.8157439,\n",
136 | " 0. ],\n",
137 | " [ 0. , 7.734725 , 0. , ..., 4.4870186, 0. ,\n",
138 | " 1.8016957]], dtype=float32)"
139 | ]
140 | },
141 | "execution_count": 38,
142 | "metadata": {},
143 | "output_type": "execute_result"
144 | }
145 | ],
146 | "source": [
147 | "sess.run(h, {x: np.random.random((100, 784))})"
148 | ]
149 | },
150 | {
151 | "cell_type": "code",
152 | "execution_count": null,
153 | "metadata": {},
154 | "outputs": [],
155 | "source": []
156 | }
157 | ],
158 | "metadata": {
159 | "kernelspec": {
160 | "display_name": "Python 3",
161 | "language": "python",
162 | "name": "python3"
163 | },
164 | "language_info": {
165 | "codemirror_mode": {
166 | "name": "ipython",
167 | "version": 3
168 | },
169 | "file_extension": ".py",
170 | "mimetype": "text/x-python",
171 | "name": "python",
172 | "nbconvert_exporter": "python",
173 | "pygments_lexer": "ipython3",
174 | "version": "3.6.6"
175 | }
176 | },
177 | "nbformat": 4,
178 | "nbformat_minor": 2
179 | }
180 |
--------------------------------------------------------------------------------
/tensorflow/matrix_summation.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | ""
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import tensorflow as tf"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 18,
22 | "metadata": {},
23 | "outputs": [],
24 | "source": [
25 | "a = tf.constant([\n",
26 | " [1, 2, 3],\n",
27 | " [4, 5, 6]\n",
28 | "])"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 19,
34 | "metadata": {},
35 | "outputs": [],
36 | "source": [
37 | "b = tf.constant([\n",
38 | " [3, 4, 5],\n",
39 | " [6, 7, 8]\n",
40 | "])"
41 | ]
42 | },
43 | {
44 | "cell_type": "code",
45 | "execution_count": 20,
46 | "metadata": {},
47 | "outputs": [
48 | {
49 | "data": {
50 | "text/plain": [
51 | ""
52 | ]
53 | },
54 | "execution_count": 20,
55 | "metadata": {},
56 | "output_type": "execute_result"
57 | }
58 | ],
59 | "source": [
60 | "matrix_1"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 21,
66 | "metadata": {},
67 | "outputs": [
68 | {
69 | "data": {
70 | "text/plain": [
71 | ""
72 | ]
73 | },
74 | "execution_count": 21,
75 | "metadata": {},
76 | "output_type": "execute_result"
77 | }
78 | ],
79 | "source": [
80 | "matrix_2"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 23,
86 | "metadata": {},
87 | "outputs": [],
88 | "source": [
89 | "c = tf.add(a, b)"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 24,
95 | "metadata": {},
96 | "outputs": [
97 | {
98 | "name": "stdout",
99 | "output_type": "stream",
100 | "text": [
101 | "[[ 4 6 8]\n",
102 | " [10 12 14]]\n"
103 | ]
104 | }
105 | ],
106 | "source": [
107 | "with tf.Session() as sess:\n",
108 | " sess.run(c)\n",
109 | " print(c.eval())"
110 | ]
111 | }
112 | ],
113 | "metadata": {
114 | "kernelspec": {
115 | "display_name": "Python 3",
116 | "language": "python",
117 | "name": "python3"
118 | },
119 | "language_info": {
120 | "codemirror_mode": {
121 | "name": "ipython",
122 | "version": 3
123 | },
124 | "file_extension": ".py",
125 | "mimetype": "text/x-python",
126 | "name": "python",
127 | "nbconvert_exporter": "python",
128 | "pygments_lexer": "ipython3",
129 | "version": "3.6.6"
130 | }
131 | },
132 | "nbformat": 4,
133 | "nbformat_minor": 2
134 | }
135 |
--------------------------------------------------------------------------------
/tensorflow/variable_addition.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | ""
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": 1,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": [
16 | "import tensorflow as tf"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 2,
22 | "metadata": {},
23 | "outputs": [],
24 | "source": [
25 | "a = tf.Variable(10) # 10: giá trị sẽ được khởi tạo ban đầu"
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 3,
31 | "metadata": {},
32 | "outputs": [],
33 | "source": [
34 | "b = tf.Variable(29) # 29: giá trị sẽ được khởi tạo ban đầu"
35 | ]
36 | },
37 | {
38 | "cell_type": "markdown",
39 | "metadata": {},
40 | "source": [
41 | "Một lần nữa chý ý a, b chưa có giá trị đến khi nó **được khởi tạo trong phiên(session)**, tức là hàm initialize_all_variables() được chạy"
42 | ]
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": 18,
47 | "metadata": {},
48 | "outputs": [],
49 | "source": [
50 | "# Hàm khởi tạo giá trị ban đầu cho các biến x, y\n",
51 | "# Chú ý, khác với lập trình thông thường, đây mới dừng lại việc định nghĩa hàm\n",
52 | "# Hàm initialize_all_variables() chưa thực sự chạy, x và y node chưa hề có giá trị\n",
53 | "init = tf.initialize_all_variables() "
54 | ]
55 | },
56 | {
57 | "cell_type": "code",
58 | "execution_count": 19,
59 | "metadata": {},
60 | "outputs": [
61 | {
62 | "name": "stdout",
63 | "output_type": "stream",
64 | "text": [
65 | "39\n"
66 | ]
67 | }
68 | ],
69 | "source": [
70 | "# Chạy Graph\n",
71 | "with tf.Session() as sess:\n",
72 | " # initialize_all_variables lúc này mới chạy thực sự -> các node x và y mới bắt đầu có giá trị\n",
73 | " sess.run(init)\n",
74 | " sess.run(c) # Chạy Graph\n",
75 | " print(c.eval())"
76 | ]
77 | }
78 | ],
79 | "metadata": {
80 | "kernelspec": {
81 | "display_name": "Python 3",
82 | "language": "python",
83 | "name": "python3"
84 | },
85 | "language_info": {
86 | "codemirror_mode": {
87 | "name": "ipython",
88 | "version": 3
89 | },
90 | "file_extension": ".py",
91 | "mimetype": "text/x-python",
92 | "name": "python",
93 | "nbconvert_exporter": "python",
94 | "pygments_lexer": "ipython3",
95 | "version": "3.6.6"
96 | }
97 | },
98 | "nbformat": 4,
99 | "nbformat_minor": 2
100 | }
101 |
--------------------------------------------------------------------------------
/tf2.0/1.inference.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/1.inference.jpg
--------------------------------------------------------------------------------
/tf2.0/5. Transfer Learning VGG.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "https://github.com/mjiansun/cifar10-vgg16/blob/master/vgg.py"
8 | ]
9 | },
10 | {
11 | "cell_type": "code",
12 | "execution_count": null,
13 | "metadata": {},
14 | "outputs": [],
15 | "source": []
16 | }
17 | ],
18 | "metadata": {
19 | "kernelspec": {
20 | "display_name": "Python 3",
21 | "language": "python",
22 | "name": "python3"
23 | },
24 | "language_info": {
25 | "codemirror_mode": {
26 | "name": "ipython",
27 | "version": 3
28 | },
29 | "file_extension": ".py",
30 | "mimetype": "text/x-python",
31 | "name": "python",
32 | "nbconvert_exporter": "python",
33 | "pygments_lexer": "ipython3",
34 | "version": "3.7.4"
35 | }
36 | },
37 | "nbformat": 4,
38 | "nbformat_minor": 2
39 | }
40 |
--------------------------------------------------------------------------------
/tf2.0/README.MD:
--------------------------------------------------------------------------------
1 | ### Mục lục
2 | ----
3 | - 1. [Cài đặt và train model nhận diện thời trang](./setup-and-first-code.ipynb) (Hoàn tất)
4 | - 2. [Tiền xử lý dữ liệu dạng bảng](./data-processing.ipynb) (Hoàn tất)
5 | - 3. 3 cách xây dựng Deep Learning models
6 | - a. [Sử dụng Subclassing](./2.SubclassingModel.ipynb)
7 | - b. [Sử dụng Sequential API](./1.SequentialModel.ipynb)
--------------------------------------------------------------------------------
/tf2.0/TFv2 API.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "from __future__ import absolute_import, division, print_function, unicode_literals\n",
10 | "import tensorflow as tf"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 4,
16 | "metadata": {},
17 | "outputs": [],
18 | "source": [
19 | "W = tf.Variable(tf.ones(shape=(2,2)), name=\"W\")\n",
20 | "b = tf.Variable(tf.zeros(shape=(2)), name=\"b\")\n",
21 | "\n",
22 | "@tf.function\n",
23 | "def forward(x):\n",
24 | " return W * x + b"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "execution_count": 5,
30 | "metadata": {},
31 | "outputs": [
32 | {
33 | "name": "stdout",
34 | "output_type": "stream",
35 | "text": [
36 | "tf.Tensor(\n",
37 | "[[1. 0.]\n",
38 | " [1. 0.]], shape=(2, 2), dtype=float32)\n"
39 | ]
40 | }
41 | ],
42 | "source": [
43 | "out_a = forward([1,0])\n",
44 | "print(out_a)"
45 | ]
46 | },
47 | {
48 | "cell_type": "code",
49 | "execution_count": null,
50 | "metadata": {},
51 | "outputs": [],
52 | "source": []
53 | }
54 | ],
55 | "metadata": {
56 | "kernelspec": {
57 | "display_name": "Python 3",
58 | "language": "python",
59 | "name": "python3"
60 | },
61 | "language_info": {
62 | "codemirror_mode": {
63 | "name": "ipython",
64 | "version": 3
65 | },
66 | "file_extension": ".py",
67 | "mimetype": "text/x-python",
68 | "name": "python",
69 | "nbconvert_exporter": "python",
70 | "pygments_lexer": "ipython3",
71 | "version": "3.7.4"
72 | }
73 | },
74 | "nbformat": 4,
75 | "nbformat_minor": 2
76 | }
77 |
--------------------------------------------------------------------------------
/tf2.0/img/0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/img/0.png
--------------------------------------------------------------------------------
/tf2.0/img/arc.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/img/arc.jpg
--------------------------------------------------------------------------------
/tf2.0/img/cifa.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/img/cifa.png
--------------------------------------------------------------------------------
/tf2.0/img/onehot.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/img/onehot.jpeg
--------------------------------------------------------------------------------
/tf2.0/img/training.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/img/training.png
--------------------------------------------------------------------------------
/tf2.0/saved_model.h5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/saved_model.h5
--------------------------------------------------------------------------------
/tf2.0/tf 2.0 inference.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/tf 2.0 inference.jpg
--------------------------------------------------------------------------------
/tf2.0/traffic-signs-classification/README.MD:
--------------------------------------------------------------------------------
1 | ### Mục lục
2 | ----
3 | - 1. [Cài đặt và train model nhận diện thời trang](./setup-and-first-code.ipynb) (Hoàn tất)
4 | - 2. [Tiền xử lý dữ liệu dạng bảng](./data-processing.ipynb) (Hoàn tất)
5 | - 3. 3 cách xây dựng Deep Learning models
6 | - a. [Sử dụng Subclassing](./2.SubclassingModel.ipynb)
7 | - b. [Sử dụng Sequential API](./1.SequentialModel.ipynb)
8 | - c. [Xây dựng model nhận diện biển báo giao thông](./traffic-signs-classification/NgocNetForSignsClassification.ipynb)
--------------------------------------------------------------------------------
/tf2.0/traffic-signs-classification/img/arch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/traffic-signs-classification/img/arch.png
--------------------------------------------------------------------------------
/tf2.0/traffic-signs-classification/img/data.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/traffic-signs-classification/img/data.png
--------------------------------------------------------------------------------
/tf2.0/traffic-signs-classification/ngocnet.h5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/tf2.0/traffic-signs-classification/ngocnet.h5
--------------------------------------------------------------------------------
/visualize/multi-dimension/3D-point.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": []
9 | }
10 | ],
11 | "metadata": {
12 | "kernelspec": {
13 | "display_name": "Python 3",
14 | "language": "python",
15 | "name": "python3"
16 | }
17 | },
18 | "nbformat": 4,
19 | "nbformat_minor": 4
20 | }
21 |
--------------------------------------------------------------------------------
/visualize/pandas/README.MD:
--------------------------------------------------------------------------------
1 | ### Pandas
2 | - Một số tính năng của Pandas
3 | - Tiện lợi cho việc xử lý data dưới dạng bảng
4 | - Được xây dựng trên Numpy nên rất mạnh mẽ
5 | - Cài đặt
6 | - **pip install pandas**
7 | - Thực hành
8 | - [Series - Chuỗi data]("./Series.ipynb")
9 | - DataFrame
10 | - Phân biệt: Một cách hiểu đơn giản là Series là hiển thị dữ liệu dạng key-value dưới dạng 1 cột còn Dataframe sẽ là ghép nhiều cột series thành một bảng.
11 |
12 | 
13 |
14 | [Nguồn ảnh](https://www.learndatasci.com/tutorials/python-pandas-tutorial-complete-introduction-for-beginners/)
15 |
16 | - [Khởi tạo DataFrame và truy xuất dữ liệu](./DataFrameCreateRead.ipynb)
--------------------------------------------------------------------------------
/visualize/pandas/images/1.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/1.PNG
--------------------------------------------------------------------------------
/visualize/pandas/images/10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/10.png
--------------------------------------------------------------------------------
/visualize/pandas/images/11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/11.png
--------------------------------------------------------------------------------
/visualize/pandas/images/2-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/2-1.png
--------------------------------------------------------------------------------
/visualize/pandas/images/2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/2.png
--------------------------------------------------------------------------------
/visualize/pandas/images/3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/3.png
--------------------------------------------------------------------------------
/visualize/pandas/images/4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/4.png
--------------------------------------------------------------------------------
/visualize/pandas/images/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/5.png
--------------------------------------------------------------------------------
/visualize/pandas/images/6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/6.png
--------------------------------------------------------------------------------
/visualize/pandas/images/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/7.png
--------------------------------------------------------------------------------
/visualize/pandas/images/7.pnh.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/7.pnh.png
--------------------------------------------------------------------------------
/visualize/pandas/images/8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/8.png
--------------------------------------------------------------------------------
/visualize/pandas/images/demo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ProtonX-AI/learn-machine-learning-in-two-months/e103ed39fa10dfa4bcae45c1bb51fcb2e1cf9c50/visualize/pandas/images/demo.png
--------------------------------------------------------------------------------