Bagging

Machine Learning with Tree-Based Models in Python

Elie Kawerk

Data Scientist

Ensemble Methods

Voting Classifier

  • same training set,
  • $\neq$ algorithms.

Bagging

  • one algorithm,
  • $\neq$ subsets of the training set.
Machine Learning with Tree-Based Models in Python

Bagging

  • Bagging: Bootstrap Aggregation.

  • Uses a technique known as the bootstrap.

  • Reduces variance of individual models in the ensemble.

Machine Learning with Tree-Based Models in Python

Bootstrap

bootstrap

Machine Learning with Tree-Based Models in Python

Bagging: Training

train-bagg

Machine Learning with Tree-Based Models in Python

Bagging: Prediction

predict-bagg

Machine Learning with Tree-Based Models in Python

Bagging: Classification & Regression

Classification:

  • Aggregates predictions by majority voting.
  • BaggingClassifier in scikit-learn.

Regression:

  • Aggregates predictions through averaging.
  • BaggingRegressor in scikit-learn.
Machine Learning with Tree-Based Models in Python

Bagging Classifier in sklearn (Breast-Cancer dataset)

# 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

Let's practice!

Machine Learning with Tree-Based Models in Python

Preparing Video For Download...