Bagging

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

วิธี Ensemble

Voting Classifier

  • ชุดข้อมูลฝึกชุดเดียวกัน
  • อัลกอริทึม $\neq$ กัน

Bagging

  • อัลกอริทึมเดียว
  • ชุดย่อยของข้อมูลฝึก $\neq$ กัน
Machine Learning with Tree-Based Models in Python

Bagging

  • Bagging: Bootstrap Aggregation

  • ใช้เทคนิคที่เรียกว่า bootstrap

  • ลด variance ของโมเดลแต่ละตัวใน ensemble

Machine Learning with Tree-Based Models in Python

Bootstrap

bootstrap

Machine Learning with Tree-Based Models in Python

Bagging: การฝึกโมเดล

train-bagg

Machine Learning with Tree-Based Models in Python

Bagging: การทำนาย

predict-bagg

Machine Learning with Tree-Based Models in Python

Bagging: การจำแนกและการถดถอย

การจำแนก (Classification):

  • รวมผลการทำนายด้วย majority voting
  • BaggingClassifier ใน scikit-learn

การถดถอย (Regression):

  • รวมผลการทำนายด้วยการหาค่าเฉลี่ย
  • BaggingRegressor ใน scikit-learn
Machine Learning with Tree-Based Models in Python

BaggingClassifier ใน sklearn (ชุดข้อมูล Breast-Cancer)

# Import models and utility functions
from sklearn.ensemble import BaggingClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

# Set seed for reproducibility
SEED = 1

# 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,
                                                    stratify=y,
                                                    random_state=SEED)
Machine Learning with Tree-Based Models in Python
# Instantiate a classification-tree 'dt'
dt = DecisionTreeClassifier(max_depth=4, min_samples_leaf=0.16, random_state=SEED)

# Instantiate a BaggingClassifier 'bc' bc = BaggingClassifier(base_estimator=dt, n_estimators=300, n_jobs=-1)
# Fit 'bc' to the training set bc.fit(X_train, y_train) # Predict test set labels y_pred = bc.predict(X_test) # Evaluate and print test-set accuracy accuracy = accuracy_score(y_test, y_pred) print('Accuracy of Bagging Classifier: {:.3f}'.format(accuracy))
Accuracy of Bagging Classifier: 0.936
Machine Learning with Tree-Based Models in Python

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

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...