Ensemble Techniques in Machine Learning
Ensemble techniques are methods in machine learning that combine the predictions of multiple models to produce a more accurate and robust final prediction. These techniques can help reduce variance, bias, and improve the overall performance of models. Here’s a detailed look at the main types of ensemble techniques, their use cases, and examples for each:
1. Bagging (Bootstrap Aggregating)
Use: Bagging reduces variance by training multiple instances of the same model on different subsets of the training data and then averaging their predictions. If working on a Binary Classification problem — the majority voting classifier will be considered the final prediction value.
Each model is called base learner & base learners in the bagging runs parallelly.
Every model/base learner takes a subset of the training dataset to make predictions individually.
Types and Examples:
1 . Random Forests:
An ensemble of decision trees, where each tree is trained on a random subset of the data and features.
- Example:
RandomForestClassifier
in Scikit-Learn.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=100)
model.fit(X_train, y_train)
2. Boosting
Use: Boosting reduces bias by training models sequentially, where each new model focuses on the errors made by the previous models.
Each model is called weak learner and all models run sequentially to get strong learner.
It takes dataset from previous model that are wrongly predicted & passes on to next model for better prediction.
Types and Examples:
1 . AdaBoost (Adaptive Boosting):
Combines weak classifiers, typically decision trees, by focusing on the instances that previous classifiers got wrong.
- Example:
AdaBoostClassifier
in Scikit-Learn.
from sklearn.ensemble import AdaBoostClassifier
model = AdaBoostClassifier(n_estimators=50)
model.fit(X_train, y_train)
2 . Gradient Boosting:
Sequentially adds models to minimize the residual errors of the previous models using gradient descent.
- Example:
GradientBoostingClassifier
in Scikit-Learn.
from sklearn.ensemble import GradientBoostingClassifier
model = GradientBoostingClassifier(n_estimators=100, learning_rate=0.1)
model.fit(X_train, y_train)
3. XGBoost (Extreme Gradient Boosting):
An optimized implementation of gradient boosting with additional regularization.
- Example:
XGBClassifier
in XGBoost library.
from xgboost import XGBClassifier
model = XGBClassifier(n_estimators=100, learning_rate=0.1)
model.fit(X_train, y_train)
4. LightGBM (Light Gradient Boosting Machine):
Focuses on leaf-wise growth and efficient handling of large datasets.
- Example:
LGBMClassifier
in LightGBM library.
from lightgbm import LGBMClassifier
model = LGBMClassifier(n_estimators=100, learning_rate=0.1)
model.fit(X_train, y_train)
3. Stacking (Stacked Generalization)
Use: Stacking combines multiple models (base learners) by training a meta-model to make final predictions based on the outputs of the base learners.
Types and Examples:
1. Simple Stacking:
Using a logistic regression as the meta-model to combine predictions from different models.
- Example: Using Scikit-Learn’s
StackingClassifier
.
from sklearn.ensemble import StackingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
base_learners = [
('dt', DecisionTreeClassifier()),
('svm', SVC(probability=True))
]
meta_model = LogisticRegression()
model = StackingClassifier(estimators=base_learners, final_estimator=meta_model)
model.fit(X_train, y_train)
4. Voting
Use: Voting combines the predictions of multiple models by averaging (regression) or using majority voting (classification).
Types and Examples:
- Hard Voting:
Each classifier votes for a class, and the class with the majority votes is chosen.
- Example: Using Scikit-Learn’s
VotingClassifier
.
from sklearn.ensemble import VotingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
model1 = LogisticRegression()
model2 = DecisionTreeClassifier()
model3 = SVC()
model = VotingClassifier(estimators=[
('lr', model1),
('dt', model2),
('svc', model3)
], voting='hard')
model.fit(X_train, y_train)
2. Soft Voting:
Each classifier outputs a probability for each class, and the class with the highest average probability is chosen.
- Example: Using Scikit-Learn’s
VotingClassifier
withvoting='soft'
.
model = VotingClassifier(estimators=[
('lr', model1),
('dt', model2),
('svc', model3)
], voting='soft')
model.fit(X_train, y_train)
Summary
- Bagging: Reduces variance; example: Random Forests.
- Boosting: Reduces bias; examples: AdaBoost, Gradient Boosting, XGBoost, LightGBM.
- Stacking: Combines multiple models; example: Stacking with Logistic Regression as meta-model.
- Voting: Combines predictions by voting; examples: Hard Voting, Soft Voting.
Ensemble techniques are powerful tools in machine learning that leverage the strengths of multiple models to achieve better performance and robustness compared to individual models.