การปรับ Hyperparameter ของ CART

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

Hyperparameter

โมเดล Machine Learning:

  • พารามิเตอร์: เรียนรู้จากข้อมูล

    • ตัวอย่าง CART: จุดแบ่งของโหนด, ฟีเจอร์ที่ใช้แบ่ง, ...
  • Hyperparameter: ไม่ได้เรียนรู้จากข้อมูล กำหนดก่อนการฝึก

    • ตัวอย่าง CART: max_depth, min_samples_leaf, เกณฑ์การแบ่ง ...
Machine Learning with Tree-Based Models in Python

Hyperparameter Tuning คืออะไร?

  • ปัญหา: ค้นหา Hyperparameter ที่เหมาะสมที่สุดสำหรับอัลกอริทึมการเรียนรู้

  • วิธีแก้: หา Hyperparameter ที่ให้โมเดลดีที่สุด

  • โมเดลที่ดีที่สุด: ให้ คะแนน สูงที่สุด

  • คะแนน: ใน sklearn ค่าเริ่มต้นคือความแม่นยำ (classification) และ $R^2$ (regression)

  • ใช้ Cross Validation เพื่อประเมินประสิทธิภาพการ Generalization

Machine Learning with Tree-Based Models in Python

ทำไมต้องปรับ Hyperparameter?

  • ใน sklearn Hyperparameter เริ่มต้นของโมเดลอาจไม่เหมาะกับทุกปัญหา

  • ควรปรับ Hyperparameter เพื่อให้โมเดลทำงานได้ดีที่สุด

Machine Learning with Tree-Based Models in Python

แนวทางการปรับ Hyperparameter

  • Grid Search

  • Random Search

  • Bayesian Optimization

  • Genetic Algorithms

  • ....

Machine Learning with Tree-Based Models in Python

Grid Search Cross Validation

  • กำหนด Grid ของค่า Hyperparameter แบบไม่ต่อเนื่องด้วยตนเอง

  • กำหนด Metric สำหรับวัดประสิทธิภาพโมเดล

  • ค้นหาอย่างละเอียดทั่วทั้ง Grid

  • สำหรับแต่ละชุด Hyperparameter ให้ประเมินคะแนน CV ของโมเดล

  • Hyperparameter ที่ดีที่สุดคือชุดที่ให้คะแนน CV สูงที่สุด

Machine Learning with Tree-Based Models in Python

Grid Search Cross Validation: ตัวอย่าง

  • Grid ของ Hyperparameter:
    • max_depth = {2,3,4},
    • min_samples_leaf = {0.05, 0.1}
  • Hyperparameter space = { (2,0.05) , (2,0.1) , (3,0.05), ... }
  • CV scores = { $score_{(2,0.05)}$ , ... }
  • Hyperparameter ที่ดีที่สุด = ชุดที่ให้คะแนน CV สูงที่สุด
Machine Learning with Tree-Based Models in Python

ตรวจสอบ Hyperparameter ของ CART ใน sklearn

# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier

# Set seed to 1 for reproducibility
SEED = 1

# Instantiate a DecisionTreeClassifier 'dt'
dt = DecisionTreeClassifier(random_state=SEED)

Machine Learning with Tree-Based Models in Python

ตรวจสอบ Hyperparameter ของ CART ใน sklearn

# Print out 'dt's hyperparameters
print(dt.get_params())
        {'class_weight': None,
         'criterion': 'gini',
         'max_depth': None,
         'max_features': None,
         'max_leaf_nodes': None,
         'min_impurity_decrease': 0.0,
         'min_impurity_split': None,
         'min_samples_leaf': 1,
         'min_samples_split': 2,
         'min_weight_fraction_leaf': 0.0,
         'presort': False,
         'random_state': 1,
         'splitter': 'best'}
Machine Learning with Tree-Based Models in Python
# Import GridSearchCV
from sklearn.model_selection import GridSearchCV

# Define the grid of hyperparameters 'params_dt' params_dt = { 'max_depth': [3, 4,5, 6], 'min_samples_leaf': [0.04, 0.06, 0.08], 'max_features': [0.2, 0.4,0.6, 0.8] }
# Instantiate a 10-fold CV grid search object 'grid_dt' grid_dt = GridSearchCV(estimator=dt, param_grid=params_dt, scoring='accuracy', cv=10, n_jobs=-1)
# Fit 'grid_dt' to the training data grid_dt.fit(X_train, y_train)
Machine Learning with Tree-Based Models in Python

ดึง Hyperparameter ที่ดีที่สุด

# Extract best hyperparameters from 'grid_dt'
best_hyperparams = grid_dt.best_params_
print('Best hyerparameters:\n', best_hyperparams)
Best hyerparameters:
  {'max_depth': 3, 'max_features': 0.4, 'min_samples_leaf': 0.06}
# Extract best CV score from 'grid_dt'
best_CV_score = grid_dt.best_score_
print('Best CV accuracy'.format(best_CV_score))
Best CV accuracy: 0.938
Machine Learning with Tree-Based Models in Python

ดึงโมเดลที่ดีที่สุด

# Extract best model from 'grid_dt'
best_model = grid_dt.best_estimator_

# Evaluate test set accuracy test_acc = best_model.score(X_test,y_test) # Print test set accuracy print("Test set accuracy of best model: {:.3f}".format(test_acc))
Test set accuracy of best model: 0.947
Machine Learning with Tree-Based Models in Python

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

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...