การเรียนรู้ของ Classification Tree

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

องค์ประกอบของ Decision Tree

  • Decision Tree: โครงสร้างข้อมูลที่จัดเรียงเป็นลำดับชั้นของโหนด

  • โหนด: คำถามหรือการทำนาย

Machine Learning with Tree-Based Models in Python

องค์ประกอบของ Decision Tree

โหนดมี 3 ประเภท:

  • Root: ไม่มี โหนดพ่อแม่ เป็นคำถามที่แตกออกเป็น สอง โหนดลูก

  • Internal node: มี หนึ่ง โหนดพ่อแม่ เป็นคำถามที่แตกออกเป็น สอง โหนดลูก

  • Leaf: มี หนึ่ง โหนดพ่อแม่ ไม่มี โหนดลูก --> การทำนาย

Machine Learning with Tree-Based Models in Python

การทำนาย

DT ที่มีป้ายกำกับ

Machine Learning with Tree-Based Models in Python

Information Gain (IG)

ไดอะแกรม IG

Machine Learning with Tree-Based Models in Python

Information Gain (IG)

สูตร IG

เกณฑ์วัดความไม่บริสุทธิ์ของโหนด $I (node)$:

  • gini index,
  • entropy. ...
Machine Learning with Tree-Based Models in Python

การเรียนรู้ของ Classification Tree

  • โหนดเติบโตแบบ recursive

  • ในแต่ละโหนด แบ่งข้อมูลโดยอิงจาก:

    • ฟีเจอร์ $f$ และจุดแบ่ง $sp$ เพื่อให้ $IG(\text{node})$ มีค่าสูงสุด
  • ถ้า $IG (\text{node})$= 0 ให้กำหนดโหนดนั้นเป็น leaf

    ...

Machine Learning with Tree-Based Models in Python
# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
# Import train_test_split
from sklearn.model_selection import train_test_split
# Import accuracy_score
from sklearn.metrics import accuracy_score
# Split dataset into 80% train, 20% test
X_train, X_test, y_train, y_test= train_test_split(X, y, 
                                                   test_size=0.2, 
                                                   stratify=y,
                                                   random_state=1)
# Instantiate dt, set 'criterion' to 'gini'
dt = DecisionTreeClassifier(criterion='gini', random_state=1)
Machine Learning with Tree-Based Models in Python

เกณฑ์ข้อมูลใน scikit-learn

# Fit dt to the training set
dt.fit(X_train,y_train)

# Predict test-set labels
y_pred= dt.predict(X_test)

# Evaluate test-set accuracy
accuracy_score(y_test, y_pred)
0.92105263157894735
Machine Learning with Tree-Based Models in Python

มาฝึกกันเถอะ!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...