thanhtai435 commited on
Commit
3bbfd14
·
verified ·
1 Parent(s): ff20955

Add chapter-05-data-mining/README.md

Browse files
Files changed (1) hide show
  1. chapter-05-data-mining/README.md +391 -0
chapter-05-data-mining/README.md ADDED
@@ -0,0 +1,391 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Chương 5: Thuật toán Khai thác Dữ liệu Cốt lõi
2
+ ## Core Data Mining Algorithms
3
+
4
+ ---
5
+
6
+ ## 📚 Mục tiêu học tập (CLO3, CLO4, CLO5, CLO7, CLO8)
7
+
8
+ Sau khi hoàn thành chương này, sinh viên có thể:
9
+ - Implement thuật toán Apriori và FP-Growth cho Association Rules
10
+ - Xây dựng Decision Tree (ID3, C4.5) cho bài toán Classification
11
+ - Áp dụng Naive Bayes cho phân loại văn bản/dữ liệu
12
+ - Thực hiện K-Means và K-Medoids cho Clustering
13
+ - Sử dụng DBSCAN cho density-based clustering
14
+ - Phân tích outlier bằng phương pháp thống kê và khoảng cách
15
+
16
+ ---
17
+
18
+ ## 5.1. Khai thác Luật kết hợp (Association Rules)
19
+
20
+ ### Các khái niệm cơ bản
21
+
22
+ ```
23
+ Transaction Database:
24
+ TID | Items
25
+ ─────┼──────────────────────
26
+ T1 | {Bread, Milk, Butter}
27
+ T2 | {Bread, Milk}
28
+ T3 | {Milk, Eggs}
29
+ T4 | {Bread, Milk, Eggs}
30
+ T5 | {Bread, Butter}
31
+
32
+ Itemset: Tập các items (VD: {Bread, Milk})
33
+
34
+ Support(X) = P(X) = count(X) / N
35
+ VD: Support({Bread, Milk}) = 3/5 = 0.6 (60%)
36
+
37
+ Confidence(X → Y) = P(Y|X) = Support(X∪Y) / Support(X)
38
+ VD: Confidence({Bread} → {Milk}) = Support({Bread,Milk}) / Support({Bread})
39
+ = 3/5 / 4/5 = 0.75 (75%)
40
+
41
+ Lift(X → Y) = Confidence(X→Y) / Support(Y)
42
+ VD: Lift({Bread}→{Milk}) = 0.75 / 0.8 = 0.9375
43
+
44
+ Lift > 1: X và Y có quan hệ positive
45
+ Lift = 1: X và Y independent
46
+ Lift < 1: X và Y có quan hệ negative
47
+ ```
48
+
49
+ ---
50
+
51
+ ## 5.2. Thuật toán Apriori
52
+
53
+ ### Nguyên lý Apriori
54
+
55
+ > **Nếu một itemset không frequent, thì mọi superset của nó cũng không frequent.**
56
+ >
57
+ > Ngược lại: mọi subset của frequent itemset đều phải frequent.
58
+
59
+ ```
60
+ Apriori Algorithm (min_support = 0.4):
61
+
62
+ Bước 1: Frequent 1-itemsets (C1 → L1)
63
+ ┌──────────┬──────────┬─────────┐
64
+ │ Itemset │ Support │ Frequent│
65
+ ├──────────┼──────────┼─────────┤
66
+ │ {Bread} │ 4/5=0.80 │ ✅ │
67
+ │ {Milk} │ 4/5=0.80 │ ✅ │
68
+ │ {Butter} │ 2/5=0.40 │ ✅ │
69
+ │ {Eggs} │ 2/5=0.40 │ ✅ │
70
+ └──────────┴──────────┴─────────┘
71
+
72
+ Bước 2: Generate candidates C2 từ L1, scan DB
73
+ ┌────────────────┬──────────┬─────────┐
74
+ │ Itemset │ Support │ Frequent│
75
+ ├────────────────┼──────────┼─────────┤
76
+ │ {Bread, Milk} │ 3/5=0.60 │ ✅ │
77
+ │ {Bread,Butter} │ 2/5=0.40 │ ✅ │
78
+ │ {Bread, Eggs} │ 1/5=0.20 │ ❌ prune│
79
+ │ {Milk, Butter} │ 1/5=0.20 │ ❌ prune│
80
+ │ {Milk, Eggs} │ 2/5=0.40 │ ✅ │
81
+ │ {Butter, Eggs} │ 0/5=0.00 │ ❌ prune│
82
+ └────────────────┴──────────┴─────────┘
83
+
84
+ Bước 3: C3 từ L2 → {Bread, Milk, Butter}?
85
+ Check subsets: {Bread,Butter}✅ {Milk,Butter}❌ → Prune!
86
+ → L3 = ∅ → STOP
87
+
88
+ Kết quả Frequent Itemsets:
89
+ L1: {Bread}, {Milk}, {Butter}, {Eggs}
90
+ L2: {Bread,Milk}, {Bread,Butter}, {Milk,Eggs}
91
+ ```
92
+
93
+ ### FP-Growth (cải tiến Apriori)
94
+
95
+ ```
96
+ FP-Growth vs Apriori:
97
+
98
+ Apriori: FP-Growth:
99
+ - Scan DB nhiều lần - Scan DB chỉ 2 lần
100
+ - Generate candidates - KHÔNG generate candidates
101
+ - Chậm với large DB - Nhanh hơn nhiều
102
+ - Dễ hiểu - Phức tạp hơn
103
+
104
+ FP-Growth xây dựng FP-Tree (compact representation)
105
+ rồi mine frequent patterns trực tiếp từ tree.
106
+ ```
107
+
108
+ ---
109
+
110
+ ## 5.3. Classification: Decision Trees
111
+
112
+ ### Information Gain và Entropy
113
+
114
+ ```
115
+ Entropy(S) = -Σ p_i * log2(p_i)
116
+
117
+ VD: Dataset có 9 positive, 5 negative
118
+ Entropy = -9/14 * log2(9/14) - 5/14 * log2(5/14) = 0.940
119
+
120
+ Information Gain(S, A) = Entropy(S) - Σ |Sv|/|S| * Entropy(Sv)
121
+
122
+ Chọn attribute có Information Gain cao nhất để split!
123
+ ```
124
+
125
+ ### ID3 Algorithm
126
+
127
+ ```
128
+ ID3 Decision Tree cho Olist (đơn giản hóa):
129
+
130
+ Target: Is customer satisfied? (review_score >= 4)
131
+
132
+ ┌──────────────┐
133
+ │delivery_days │
134
+ │ IG = 0.32 │
135
+ └──────┬───────┘
136
+
137
+ ┌───────────┼───────────┐
138
+ │ │ │
139
+ ≤ 7 days 8-14 days > 14 days
140
+ │ │ │
141
+ ┌────▼────┐ ┌────▼────┐ ┌────▼────┐
142
+ │ price │ │Satisfied│ │ NOT │
143
+ │IG=0.15 │ │ (68%) │ │Satisfied│
144
+ └────┬────┘ └─────────┘ │ (82%) │
145
+ │ └─────────┘
146
+ ┌────┼────┐
147
+ │ │
148
+ ≤ 200 > 200
149
+ │ │
150
+ ┌────▼────┐ ┌──▼──────┐
151
+ │Satisfied│ │ Check │
152
+ │ (90%) │ │ freight │
153
+ └─────────┘ └─────────┘
154
+
155
+ ID3: Chỉ dùng categorical (discretize nếu cần)
156
+ C4.5: Hỗ trợ continuous attributes, handling missing values
157
+ ```
158
+
159
+ ### C4.5 Improvements over ID3
160
+
161
+ | Feature | ID3 | C4.5 |
162
+ |---------|-----|------|
163
+ | Attributes | Categorical only | Categorical + Continuous |
164
+ | Missing values | Not handled | Uses probability weighting |
165
+ | Overfitting | Not addressed | Post-pruning (error-based) |
166
+ | Splitting criterion | Information Gain | **Gain Ratio** = IG / SplitInfo |
167
+ | Multi-way split | Yes | Yes + Binary split for continuous |
168
+
169
+ ```
170
+ Gain Ratio = Information_Gain(A) / SplitInformation(A)
171
+
172
+ SplitInfo(A) = -Σ |Sv|/|S| * log2(|Sv|/|S|)
173
+
174
+ → Penalize attributes with many values (fix IG bias)
175
+ ```
176
+
177
+ ---
178
+
179
+ ## 5.4. Naive Bayes Classification
180
+
181
+ ### Bayes' Theorem
182
+
183
+ ```
184
+ P(C|X) = P(X|C) * P(C) / P(X)
185
+
186
+ Posterior = Likelihood × Prior / Evidence
187
+
188
+ Naive assumption: features are conditionally independent
189
+ P(X|C) = P(x1|C) × P(x2|C) × ... × P(xn|C)
190
+
191
+ Ví dụ: Predict review_score ≥ 4 (satisfied)
192
+
193
+ Given: delivery_days=5, price=150, state=SP
194
+
195
+ P(satisfied | features) ∝ P(delivery=5|sat) × P(price=150|sat) × P(SP|sat) × P(sat)
196
+ P(unsatisfied | features) ∝ P(delivery=5|unsat) × P(price=150|unsat) × P(SP|unsat) × P(unsat)
197
+
198
+ Chọn class có probability cao hơn.
199
+ ```
200
+
201
+ ### Types of Naive Bayes
202
+
203
+ | Type | Use case | P(x|C) |
204
+ |------|----------|--------|
205
+ | **Gaussian NB** | Continuous features | Normal distribution |
206
+ | **Multinomial NB** | Text/Count data | Multinomial distribution |
207
+ | **Bernoulli NB** | Binary features | Bernoulli distribution |
208
+ | **Complement NB** | Imbalanced text | Complement of class |
209
+
210
+ ---
211
+
212
+ ## 5.5. K-Means Clustering
213
+
214
+ ### Algorithm
215
+
216
+ ```
217
+ K-Means Algorithm:
218
+
219
+ Input: K (số clusters), Data points
220
+
221
+ 1. Khởi tạo K centroids (random hoặc K-Means++)
222
+ 2. REPEAT:
223
+ a. ASSIGN: Gán mỗi point vào cluster có centroid gần nhất
224
+ b. UPDATE: Tính lại centroid = mean of all points trong cluster
225
+ 3. UNTIL centroids không đổi (hoặc max iterations)
226
+
227
+ Iteration 0: Iteration 1: Converged:
228
+ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
229
+ │ * │ │ * . . │ │ * . . │
230
+ │ . . C1 │ │ . C1 │ │ .C1 . │
231
+ │ . . . │ │ . . . │ │ . . . │
232
+ │ C2 │ │ │ │ │
233
+ │ . . . . │ │ . . C2. . │ │ . . C2. . │
234
+ │ . . │ │ . . │ │ . . │
235
+ └──────────────┘ └──────────────┘ └──────────────┘
236
+ C = Centroid, . = Data point
237
+ ```
238
+
239
+ ### K-Means++: Khởi tạo thông minh
240
+
241
+ ```
242
+ K-Means++ (Arthur & Vassilvitskii, 2007):
243
+
244
+ 1. Chọn centroid đầu tiên random
245
+ 2. Cho mỗi centroid tiếp theo:
246
+ - Tính D(x) = khoảng cách từ x đến centroid gần nhất
247
+ - Chọn point mới với xác suất ∝ D(x)²
248
+ → Centroids xa nhau → converge nhanh hơn
249
+ ```
250
+
251
+ ### Chọn K tối ưu
252
+
253
+ ```
254
+ Elbow Method: Silhouette Method:
255
+
256
+ Inertia Silhouette Score
257
+ │ │
258
+ │\ │ *
259
+ │ \ │ * *
260
+ │ \ │ * *
261
+ │ \ │* *
262
+ │ \_____ │
263
+ │ ↑ │
264
+ │ Elbow │
265
+ │ \_____ │
266
+ └─────────────── K └─────────────── K
267
+
268
+ K* = point where adding K* = max silhouette
269
+ more clusters gives s(i) = (b(i)-a(i)) / max(a(i),b(i))
270
+ diminishing returns a(i) = avg distance to same cluster
271
+ b(i) = avg distance to nearest other cluster
272
+ ```
273
+
274
+ ---
275
+
276
+ ## 5.6. DBSCAN (Density-Based Clustering)
277
+
278
+ ### Nguyên lý hoạt động
279
+
280
+ ```
281
+ DBSCAN Parameters:
282
+ - ε (epsilon): Bán kính neighborhood
283
+ - MinPts: Số điểm tối thiểu trong ε-neighborhood
284
+
285
+ 3 loại điểm:
286
+ ┌──────────────────────────────────────┐
287
+ │ │
288
+ │ Core Point (≥ MinPts trong ε) │
289
+ │ ┌─────┐ │
290
+ │ │ ●──┼──. . . (≥5 points) │
291
+ │ │ ε │ │
292
+ │ └─────┘ │
293
+ │ │
294
+ │ Border Point (< MinPts nhưng │
295
+ │ trong ε của Core Point) │
296
+ │ ○ ← border │
297
+ │ ↓ │
298
+ │ ┌─────┐ │
299
+ │ │ ● │ core │
300
+ │ └─────┘ │
301
+ │ │
302
+ │ Noise Point (không thuộc cluster) │
303
+ │ ✕ ← noise/outlier │
304
+ │ │
305
+ └──────────────────────────────────────┘
306
+
307
+ DBSCAN Algorithm:
308
+ 1. Cho mỗi point chưa visited:
309
+ a. Đánh dấu visited
310
+ b. Tìm ε-neighborhood
311
+ c. Nếu ≥ MinPts → Core point → Tạo/mở rộng cluster
312
+ d. Nếu < MinPts → Tạm đánh dấu noise (có thể thành border sau)
313
+
314
+ DBSCAN vs K-Means:
315
+ ┌────────────────┬─────────────┬──────────────┐
316
+ │ Feature │ K-Means │ DBSCAN │
317
+ ├────────────────┼─────────────┼──────────────┤
318
+ │ # Clusters │ Phải chỉ K │ Tự phát hiện │
319
+ │ Cluster shape │ Spherical │ Arbitrary │
320
+ │ Outlier handle │ Không │ Có (noise) │
321
+ │ Scalability │ O(nKt) │ O(n²) worst │
322
+ │ Parameters │ K │ ε, MinPts │
323
+ └────────────────┴─────────────┴──────────────┘
324
+ ```
325
+
326
+ ---
327
+
328
+ ## 5.7. Phân cụm Phân cấp (Hierarchical Clustering)
329
+
330
+ ```
331
+ Agglomerative (Bottom-up):
332
+
333
+ Start: Mỗi point = 1 cluster
334
+
335
+ Step 1: {A}{B}{C}{D}{E} Merge closest: A,B
336
+ Step 2: {A,B}{C}{D}{E} Merge closest: D,E
337
+ Step 3: {A,B}{C}{D,E} Merge closest: C,{D,E}
338
+ Step 4: {A,B}{C,D,E} Merge closest: {A,B},{C,D,E}
339
+ Step 5: {A,B,C,D,E} Done!
340
+
341
+ Dendrogram:
342
+ ─────────────────────────
343
+ │ ┌───────────┤
344
+ │ ┌─────┤ │
345
+ │ │ │ ┌──────┤
346
+ │ ┌──┤ │ │ ┌───┤
347
+ ─ A B C D E
348
+
349
+ Linkage Methods:
350
+ - Single: min distance between clusters
351
+ - Complete: max distance
352
+ - Average: mean distance (UPGMA)
353
+ - Ward's: minimize variance increase
354
+ ```
355
+
356
+ ---
357
+
358
+ ## 5.8. Outlier Detection
359
+
360
+ ### Phương pháp phát hiện Outlier
361
+
362
+ | Method | Type | Mô tả |
363
+ |--------|------|-------|
364
+ | **Z-Score** | Statistical | \|z\| > 3 → outlier |
365
+ | **Modified Z-Score** | Statistical (robust) | Uses MAD instead of std |
366
+ | **IQR** | Statistical | < Q1-1.5IQR or > Q3+1.5IQR |
367
+ | **Isolation Forest** | ML-based | Random splits, outliers isolated faster |
368
+ | **LOF** | Distance-based | Local Outlier Factor, density-based |
369
+ | **DBSCAN noise** | Density-based | Points classified as noise |
370
+ | **Mahalanobis Distance** | Statistical | Accounts for correlation |
371
+
372
+ ---
373
+
374
+ ## 🔬 Labs
375
+
376
+ - [`lab-05-apriori.py`](lab-05-apriori.py) — Apriori & FP-Growth trên Olist
377
+ - [`lab-05-decision-tree.py`](lab-05-decision-tree.py) — Decision Tree Classification
378
+ - [`lab-05-naive-bayes.py`](lab-05-naive-bayes.py) — Naive Bayes Classification
379
+ - [`lab-05-kmeans.py`](lab-05-kmeans.py) — K-Means Customer Segmentation
380
+ - [`lab-05-dbscan.py`](lab-05-dbscan.py) — DBSCAN Anomaly Detection
381
+
382
+ ---
383
+
384
+ ## 📝 Câu hỏi ôn tập
385
+
386
+ 1. Tính Support, Confidence và Lift cho rule {A} → {B} biết: N=100, count(A)=40, count(B)=50, count(A∪B)=25
387
+ 2. Giải thích nguyên lý Apriori. Tại sao nó giúp giảm số lượng candidates?
388
+ 3. So sánh Information Gain (ID3) v�� Gain Ratio (C4.5). Tại sao Gain Ratio tốt hơn?
389
+ 4. K-Means có nhược điểm gì? K-Means++ cải thiện như thế nào?
390
+ 5. DBSCAN phát hiện outlier như thế nào? So sánh với K-Means về handling outliers.
391
+ 6. Cho dataset: [1,2,3,100,2,3,1,2,500]. Dùng IQR method, xác định outliers.