Boosting is an ensemble learning method that combines multiple weak learners (typically simple models like decision stumps) sequentially to create a strong learner. Each weak learner is trained on a modified version of the data, where more weight is given to data points misclassified by previous learners. The final prediction is a weighted combination of the predictions of all weak learners, where better-performing learners are given higher weights. Boosting effectively reduces bias and often variance as well, leading to improved overall performance.
Imagine training a model to classify spam emails. Boosting might begin with a simple decision stump (a one-level decision tree) that classifies emails based on the presence of a single word. Subsequent weak learners then concentrate on emails misclassified by the previous learners. The first learner might misclassify emails containing "free" but not other spam indicators. The next learner would then give higher weight to these emails, learning to identify other spam words or combinations thereof. The final spam classification is a weighted combination of the classifications of all weak learners. For example if we combine five weak learners with weights 0.1, 0.3, 0.2, 0.2 and 0.2, we can see that the second learner is deemed more effective and thus contributes more towards the final prediction.
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier #Decision trees are commonly used as weak learners
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
# Generate sample data
X, y = make_classification(n_samples=1000, n_features=10, random_state=42)
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a decision stump (weak learner)
stump = DecisionTreeClassifier(max_depth=1, random_state=42)
# Create an AdaBoost classifier (using the decision stump as the base estimator)
boosting_classifier = AdaBoostClassifier(base_estimator=stump, n_estimators=50, random_state=42) # n_estimators = number of weak learners
# Train the boosting classifier
boosting_classifier.fit(X_train, y_train)
# Make predictions on the test set
y_pred = boosting_classifier.predict(X_test)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
# Accessing estimator weights (importance of different weak learners)
estimator_weights = boosting_classifier.estimator_weights_
print(f"Estimator Weights:\\\\n{estimator_weights}")
# Accessing individual estimators
estimator = boosting_classifier.estimators_[0] #access the first decision stump
print(f"First estimator:\\\\n{estimator}")