Bagging

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

集成方法(Ensemble Methods)

投票分類器(Voting Classifier)

  • 相同訓練集,
  • $\neq$ 演算法。

Bagging

  • 相同演算法,
  • $\neq$ 訓練集子集。
Machine Learning with Tree-Based Models in Python

Bagging

  • Bagging:自助聚合(Bootstrap Aggregation)。

  • 使用稱為 bootstrap 的技術。

  • 降低集成中個別模型的變異。

Machine Learning with Tree-Based Models in Python

自助法(Bootstrap)

bootstrap

Machine Learning with Tree-Based Models in Python

Bagging:訓練

訓練中的 Bagging

Machine Learning with Tree-Based Models in Python

Bagging:預測

Bagging 預測

Machine Learning with Tree-Based Models in Python

Bagging:分類與回歸

分類

  • 以多數決聚合預測。
  • 在 scikit-learn 中使用 BaggingClassifier

回歸

  • 以平均值聚合預測。
  • 在 scikit-learn 中使用 BaggingRegressor
Machine Learning with Tree-Based Models in Python

sklearn 的 Bagging 分類器(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...