การวินิจฉัยปัญหา Bias และ Variance

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

การประเมิน Generalization Error

  • เราประเมิน generalization error ของโมเดลได้อย่างไร?

  • ไม่สามารถทำได้โดยตรง เพราะ:

    • $f$ ไม่เป็นที่รู้จัก

    • โดยทั่วไปมีชุดข้อมูลเพียงชุดเดียว

    • noise ไม่สามารถคาดเดาได้

Machine Learning with Tree-Based Models in Python

การประเมิน Generalization Error

วิธีแก้ไข:

  • แบ่งข้อมูลเป็น training set และ test set
  • fit $\hat{f}$ กับ training set
  • ประเมิน error ของ $\hat{f}$ บน test set ที่ยังไม่เคยเห็น
  • generalization error ของ $\hat{f} \approx$ test set error ของ $\hat{f}$
Machine Learning with Tree-Based Models in Python

การประเมินโมเดลที่ดีขึ้นด้วย Cross-Validation

  • ไม่ควรแตะต้อง test set จนกว่าจะมั่นใจในประสิทธิภาพของ $\hat{f}$

  • การประเมิน $\hat{f}$ บน training set ให้ค่าที่มี bias เพราะ $\hat{f}$ เห็นข้อมูลทั้งหมดแล้ว

  • วิธีแก้ไข $\rightarrow$ Cross-Validation (CV):

    • K-Fold CV

    • Hold-Out CV

Machine Learning with Tree-Based Models in Python

K-Fold CV

KFoldCV

Machine Learning with Tree-Based Models in Python

K-Fold CV

CVerror

Machine Learning with Tree-Based Models in Python

การวินิจฉัยปัญหา Variance

  • หาก $\hat{f}$ มีปัญหา high variance:

    CV error ของ $\hat{f}$ > training set error ของ $\hat{f}$

  • กล่าวได้ว่า $\hat{f}$ เกิด overfitting กับ training set วิธีแก้ไข:
    • ลดความซับซ้อนของโมเดล
    • เช่น ลด max depth, เพิ่ม min samples per leaf, ...
    • เก็บข้อมูลเพิ่มเติม ...
Machine Learning with Tree-Based Models in Python

การวินิจฉัยปัญหา Bias

  • หาก $\hat{f}$ มีปัญหา high bias:

    CV error ของ $\hat{f} \approx$ training set error ของ $\hat{f} >>$ ค่า error ที่ต้องการ

  • กล่าวได้ว่า $\hat{f}$ เกิด underfitting กับ training set วิธีแก้ไข:

    • เพิ่มความซับซ้อนของโมเดล
    • เช่น เพิ่ม max depth, ลด min samples per leaf, ...
    • เก็บ feature ที่เกี่ยวข้องเพิ่มเติม
Machine Learning with Tree-Based Models in Python

K-Fold CV ใน sklearn บน Auto Dataset

from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import cross_val_score

# Set seed for reproducibility SEED = 123 # Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=SEED)
# Instantiate decision tree regressor and assign it to 'dt' dt = DecisionTreeRegressor(max_depth=4, min_samples_leaf=0.14, random_state=SEED)
Machine Learning with Tree-Based Models in Python

K-Fold CV ใน sklearn บน Auto Dataset

# Evaluate the list of MSE ontained by 10-fold CV 
# Set n_jobs to -1 in order to exploit all CPU cores in computation
MSE_CV = - cross_val_score(dt, X_train, y_train, cv= 10, 
                           scoring='neg_mean_squared_error',
                           n_jobs = -1)

# Fit 'dt' to the training set dt.fit(X_train, y_train) # Predict the labels of training set y_predict_train = dt.predict(X_train) # Predict the labels of test set y_predict_test = dt.predict(X_test)
Machine Learning with Tree-Based Models in Python
# CV MSE  
print('CV MSE: {:.2f}'.format(MSE_CV.mean()))
CV MSE: 20.51
# Training set MSE
print('Train MSE: {:.2f}'.format(MSE(y_train, y_predict_train)))
Train MSE: 15.30
# Test set MSE
print('Test MSE: {:.2f}'.format(MSE(y_test, y_predict_test)))
Test MSE: 20.92
Machine Learning with Tree-Based Models in Python

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

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...