วิธีการ Ensemble

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Lisa Stuart

Data Scientist

เทคนิค Ensemble Learning

  • Bootstrap Aggregation
  • Boosting
  • Model stacking
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

การวัดค่าความผิดพลาด

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

ต้นไม้ขนาดเล็ก

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

ต้นไม้ขนาดสูง

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

ต้นไม้ขนาดใหญ่

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

โมเดลเชิงเส้น

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Bias

ข้อสมมติความสัมพันธ์เชิงเส้น (ไม่ถูกต้อง)

  • Bias สูง
  • Underfitting
  • โมเดลสรุปผลได้ไม่ดี
  • ความซับซ้อนที่เพิ่มขึ้นช่วยลด Bias
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

โมเดลที่ซับซ้อน

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Variance

โมเดลที่มีความซับซ้อนสูง:

  • Variance สูง
  • Overfitting
  • โมเดลสรุปผลได้ไม่ดี
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

การแลกเปลี่ยนระหว่าง Bias และ Variance

1 ที่มา: Elements of Statistical Learning โดย Trevor Hastie, Robert Tibshirani และ Jerome Friedman
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Bagging (Bootstrap Aggregation)

  • ตัวอย่าง Bootstrapped
    • เลือกชุดย่อยแบบ replacement
    • ข้อมูลแถวเดิมอาจถูกเลือกซ้ำได้
  • สร้างโมเดลสำหรับแต่ละตัวอย่าง
  • เฉลี่ยผลลัพธ์
  • ลด Variance

1 https://medium.com/@rrfd/boosting-bagging-and-stacking-ensemble-methods-with-sklearn-and-mlens-a455c0c982de
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Boosting

  • สร้างโมเดลหลายตัวตามลำดับ
  • การทำนายที่ผิดพลาดจะถูกให้น้ำหนักเพิ่ม
  • ลด Bias

1 https://blog.bigml.com/2017/03/14/introduction-to-boosted-trees/
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Model Stacking

  • การทำนายของ Model 1
  • การทำนายของ Model 2...
  • การทำนายของ Model N
  • นำมา Stack เพื่อโมเดลที่แม่นยำที่สุด
    • ใช้ผลทำนายของโมเดลพื้นฐาน (Model N) เป็น input ของโมเดลระดับที่ 2

1 http://supunsetunga.blogspot.com/
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

แพ็กเกจ Vecstack

# import modules
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import AdaBoostClassifier
from xgboost import XGBClassifier
from vecstack import stacking

# Create list: stacked_models
stacked_models = [BaggingClassifier(n_estimators=25, random_state=123), AdaBoostClassifier(n_estimators=25, random_state=123)]

# Stack the models: stack_train, stack_test
stack_train, stack_test = stacking(stacked_models, X_train, y_train, X_test, regression=False, mode='oof_pred_bag', 
                                   needs_proba=False, metric=accuracy_score, n_folds=4, stratified=True, shuffle=True, random_state=0, verbose=2)

# Initialize and fit 2nd level model
final_model = XGBClassifier(random_state=123, n_jobs=-1, learning_rate=0.1, n_estimators=10, max_depth=3)
final_model_fit = final_model.fit(stack_train, y_train)

# Predict: stacked_pred
stacked_pred = final_model.predict(stack_test)

# Final prediction score
print('Final prediction score: [%.8f]' % accuracy_score(y_test, stacked_pred))
1 https://towardsdatascience.com/automate-stacking-in-python-fc3e7834772e
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

ฟังก์ชัน Ensemble

อัลกอริทึม ฟังก์ชัน
Bootstrap Aggregation sklearn.ensemble.BaggingClassifier()
Boosting sklearn.ensemble.AdaBoostClassifier()
XGBoost xgboost.XGBClassifier()
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Bagging vs Boosting

เทคนิค Bias Variance
Bootstrap aggregation (Bagging) เพิ่ม ลด
Boosting ลด เพิ่ม
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

คำถาม MCQ: เทคนิค Ensemble หลัก

ข้อใดต่อไปนี้ถูกต้องเกี่ยวกับเทคนิคหลัก 3 ประการของ Ensemble Methods ใน Machine Learning? เลือกข้อที่ ถูกต้อง:

  • วิธี Boosting ช่วยลด Variance ของโมเดล
  • วิธี Boosting เพิ่มความสามารถในการทำนายของ Classifier
  • Bootstrap Aggregation หรือ Bagging ช่วยลด Bias ของโมเดล
  • Model Stacking นำผลทำนายจากโมเดลแต่ละตัวมารวมกันเพื่อสร้างโมเดลที่แม่นยำขึ้น
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

คำถาม MCQ: เทคนิค Ensemble หลัก — เฉลย

ข้อใดต่อไปนี้ถูกต้องเกี่ยวกับเทคนิคหลัก 3 ประการของ Ensemble Methods ใน Machine Learning? คำตอบที่ถูกต้องคือ:

  • Model Stacking นำผลทำนายจากโมเดลแต่ละตัวมารวมกันเพื่อสร้างโมเดลที่แม่นยำขึ้น (โมเดลสุดท้ายที่ได้จากการรวมผลทำนายของหลายโมเดลมักให้ผลดีกว่าแต่ละโมเดลเดี่ยว)
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

คำถาม MCQ: เทคนิค Ensemble หลัก — คำตอบที่ไม่ถูกต้อง

ข้อใดต่อไปนี้ถูกต้องเกี่ยวกับเทคนิคหลัก 3 ประการของ Ensemble Methods ใน Machine Learning?

  • วิธี Boosting ช่วยลด Variance ของโมเดล (Boosting ลด Bias ของโมเดล ซึ่งในขณะเดียวกันอาจทำให้ Variance เพิ่มขึ้น เพื่อหาจุดสมดุลที่ดีที่สุดในการสรุปผลของโมเดล)
  • วิธี Boosting เพิ่มความสามารถในการทำนายของ Classifier (Boosting ลด Bias ของโมเดล ซึ่งอาจหรืออาจไม่เพิ่มความสามารถในการทำนายของ Classifier ก็ได้)
  • Bootstrap Aggregation หรือ Bagging ช่วยลด Bias ของโมเดล (Bagging ลด Variance ของโมเดล)
ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

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

ฝึกตอบคำถามสัมภาษณ์ Machine Learning ด้วย Python

Preparing Video For Download...