Data Mining Exercise 8
Decision Tree - Information Gain vs Gini Index การเลือก Attribute ที่ดีที่สุด
Warning
This article is a work in progress and may contain incomplete information or inaccuracies. Please verify details from reliable sources.
Data Mining Exercise 8
📌 ข้อควรระวัง
ในตารางข้อมูลข้อ 5 ช่อง Class Label มีบางช่องที่เป็น "ค่าว่าง" (ไม่มีเครื่องหมาย +) ผมจะขอสมมติว่าค่าว่างเหล่านั้นคือ คลาสลบ (-) เพื่อให้เป็น Binary Class Problem ตามโจทย์
| A | B | Class Label |
|---|---|---|
| T | F | + |
| T | T | + |
| T | T | + |
| T | F | - |
| T | T | + |
| F | F | - |
| F | F | - |
| F | F | - |
| T | T | - |
| T | F | - |
ข้อ 5: การเลือก Attribute A หรือ B
สรุปข้อมูล (Data Summary)
จำนวนข้อมูลทั้งหมด 10 แถว
- Class (+): 4 คน (แถว 1, 2, 3, 5)
- Class (-): 6 คน (แถว 4, 6, 7, 8, 9, 10)
5a. คำนวณ Information Gain (เลือก A หรือ B?)
สูตร Entropy
Entropy ของ Parent (P)
Information Gain ของ Attribute A
A = T (7 คน): มี (+)4, (-)3
A = F (3 คน): มี (+)0, (-)3 (บริสุทธิ์)
Weighted Entropy:
Information Gain(A):
Information Gain ของ Attribute B
B = T (4 คน): มี (+)3, (-)1
B = F (6 คน): มี (+)1, (-)5
Weighted Entropy:
Information Gain(B):
สรุป 5a
✅ เลือก Attribute A
เพราะ Gain(A) = 0.2815 > Gain(B) = 0.2566
5b. คำนวณ Gain in Gini Index (เลือก A หรือ B?)
สูตร Gini Index
Gini ของ Parent (P)
Gini ของ Attribute A
A = T:
A = F:
Weighted Gini:
Gain Gini(A):
Gini ของ Attribute B
B = T:
B = F:
Weighted Gini:
Gain Gini(B):
สรุป 5b
✅ เลือก Attribute B
เพราะ Gain Gini(B) = 0.1632 > Gain Gini(A) = 0.1377
5c. Explanation - สามารถเลือกคนละ Attribute ได้หรือไม่?
คำตอบ
✅ เป็นไปได้ (Yes)
ดังที่เห็นในตัวอย่างนี้ (Info Gain เลือก A แต่ Gini เลือก B)
เหตุผล
Information Gain (Entropy):
- มักจะลำเอียงไปทาง Attribute ที่แบ่งแล้วได้กลุ่มย่อยที่ "บริสุทธิ์ที่สุด" (Purity)
- แม้ว่ากลุ่มนั้นจะเล็ก (เช่น A=F มีแค่ 3 คน แต่บริสุทธิ์ 100% ทำให้ Entropy ลดลงฮวบ)
Gini Index:
- มักจะชอบการแบ่งที่ได้กลุ่ม "ขนาดใหญ่และมีความบริสุทธิ์พอประมาณ" มากกว่ากลุ่มเล็กๆ ที่บริสุทธิ์มาก
- Gini จะไม่ลดลงเร็วเท่า Entropy เมื่อเจอ Pure node เล็กๆ
ข้อ 6: Gini Index ของการแตก Node
| P | C1 | C2 | |
|---|---|---|---|
| Class 0 | 7 | 3 | 4 |
| Class 1 | 3 | 0 | 3 |
ข้อมูล
- Parent (P): Class 0 = 7, Class 1 = 3 (รวม 10)
- Child 1 (C1): Class 0 = 3, Class 1 = 0 (รวม 3) → บริสุทธิ์
- Child 2 (C2): Class 0 = 4, Class 1 = 3 (รวม 7)
6a. Calculate Gini index of node P
6b. Calculate Weighted Gini index & Decision
Gini ของ Child 1 (C1)
กลุ่มนี้มีแต่ Class 0 (บริสุทธิ์)
Gini ของ Child 2 (C2)
Weighted Gini
สรุปการตัดสินใจ (Decision)
📊 ผลการเปรียบเทียบ
- Gini เดิม: 0.42
- Gini ใหม่ (Weighted): 0.343
- ค่าลดลง (Gain): 0.42 - 0.343 = 0.077
✅ ควรพิจารณา (Yes)
เพราะค่า Gini ลดลง แปลว่าข้อมูลมีความบริสุทธิ์เพิ่มขึ้น (Impurity ลดลง) โดยเฉพาะ Node C1 ที่แยกออกมาได้บริสุทธิ์ 100%