machine learning
adaboost
scikit-learn
base estimator
python tutorial

How to use adaboost with different base estimator in scikit-learn?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

AdaBoost (Adaptive Boosting) is an ensemble method that combines multiple weak learners into a strong classifier. Each iteration trains a new weak learner that focuses on the samples the previous learners misclassified, and the final prediction is a weighted vote across all learners. While AdaBoost is most commonly used with decision tree stumps (trees of depth 1), scikit-learn lets you plug in almost any classifier as the base estimator. Choosing the right base estimator can have a significant impact on model accuracy, training speed, and generalization.

This article shows how to use AdaBoost with several different base estimators in scikit-learn, compares their performance, and covers the constraints you need to be aware of.

How AdaBoost Works

AdaBoost trains learners sequentially. On each round:

  1. A weak learner is fitted on the training data using sample weights.
  2. The learner's error rate is calculated based on misclassified samples.
  3. The learner is assigned a weight proportional to its accuracy (better learners get higher weight).
  4. Sample weights are updated so that misclassified samples receive higher weight, forcing the next learner to focus on them.

The final prediction combines all learners using their weights. For classification, this is a weighted majority vote. For regression, it is a weighted median.

Default Base Estimator: Decision Tree Stump

By default, AdaBoostClassifier uses DecisionTreeClassifier(max_depth=1), also known as a decision stump.

python
1from sklearn.ensemble import AdaBoostClassifier
2from sklearn.datasets import make_classification
3from sklearn.model_selection import cross_val_score
4
5X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
6
7# Default: decision stump
8ada_default = AdaBoostClassifier(n_estimators=100, random_state=42)
9scores = cross_val_score(ada_default, X, y, cv=5, scoring='accuracy')
10print(f"Decision Stump: {scores.mean():.4f} +/- {scores.std():.4f}")

Decision stumps work well because they are weak enough to benefit from boosting but fast to train. They are the standard baseline for AdaBoost.

Using a Deeper Decision Tree

Increasing the tree depth gives each learner more capacity. This can improve performance on complex datasets but increases the risk of overfitting.

python
1from sklearn.tree import DecisionTreeClassifier
2
3ada_deep_tree = AdaBoostClassifier(
4    estimator=DecisionTreeClassifier(max_depth=3),
5    n_estimators=100,
6    learning_rate=0.5,
7    random_state=42
8)
9
10scores = cross_val_score(ada_deep_tree, X, y, cv=5, scoring='accuracy')
11print(f"Decision Tree (depth=3): {scores.mean():.4f} +/- {scores.std():.4f}")

Note that the parameter name changed from base_estimator to estimator in scikit-learn 1.2. Use estimator for current versions.

When using stronger base learners, reduce the learning_rate to prevent overfitting. The learning rate shrinks the contribution of each learner, acting as a form of regularization.

Using Logistic Regression

Logistic Regression can serve as a base estimator when the decision boundary is roughly linear. However, there is an important constraint: the base estimator must support sample weights through a sample_weight parameter in its fit method. Logistic Regression in scikit-learn does support this.

python
1from sklearn.linear_model import LogisticRegression
2
3ada_lr = AdaBoostClassifier(
4    estimator=LogisticRegression(max_iter=200),
5    n_estimators=50,
6    learning_rate=1.0,
7    algorithm='SAMME',
8    random_state=42
9)
10
11scores = cross_val_score(ada_lr, X, y, cv=5, scoring='accuracy')
12print(f"Logistic Regression: {scores.mean():.4f} +/- {scores.std():.4f}")

Note the algorithm='SAMME' parameter. Logistic Regression does not natively produce the probability estimates that the default SAMME.R algorithm requires in older scikit-learn versions. Setting algorithm='SAMME' uses the discrete boosting variant that only requires class predictions.

Using Support Vector Machines

SVM with a linear kernel can be used as a base estimator. Since SVC supports sample_weight, it works directly with AdaBoost.

python
1from sklearn.svm import SVC
2
3ada_svm = AdaBoostClassifier(
4    estimator=SVC(kernel='linear', probability=True),
5    n_estimators=50,
6    learning_rate=1.0,
7    algorithm='SAMME',
8    random_state=42
9)
10
11scores = cross_val_score(ada_svm, X, y, cv=5, scoring='accuracy')
12print(f"SVM (linear): {scores.mean():.4f} +/- {scores.std():.4f}")

SVM-based AdaBoost can be slow because SVM training is more expensive than decision tree training. For large datasets, this combination may not be practical.

Using K-Nearest Neighbors

KNN does not support sample_weight natively in its fit method. This means you cannot use it directly with AdaBoost in scikit-learn. Attempting to do so will raise an error.

python
1from sklearn.neighbors import KNeighborsClassifier
2
3# This will raise a TypeError because KNN does not support sample_weight
4try:
5    ada_knn = AdaBoostClassifier(
6        estimator=KNeighborsClassifier(n_neighbors=5),
7        n_estimators=50,
8        random_state=42
9    )
10    cross_val_score(ada_knn, X, y, cv=5)
11except TypeError as e:
12    print(f"Error: {e}")

If you need to boost KNN, you would have to create a custom wrapper that resamples the dataset according to sample weights instead of passing weights to the estimator.

Using Extra Trees

ExtraTreesClassifier (a single extra tree, not the ensemble) can be used as a base estimator. Extra trees use random split points, which adds additional randomness and can prevent overfitting.

python
1from sklearn.tree import ExtraTreeClassifier
2
3ada_extra = AdaBoostClassifier(
4    estimator=ExtraTreeClassifier(max_depth=2),
5    n_estimators=100,
6    learning_rate=0.8,
7    random_state=42
8)
9
10scores = cross_val_score(ada_extra, X, y, cv=5, scoring='accuracy')
11print(f"Extra Tree (depth=2): {scores.mean():.4f} +/- {scores.std():.4f}")

Full Comparison Example

Here is a complete script that compares multiple base estimators on the same dataset.

python
1from sklearn.ensemble import AdaBoostClassifier
2from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
3from sklearn.linear_model import LogisticRegression
4from sklearn.datasets import make_classification
5from sklearn.model_selection import cross_val_score
6
7X, y = make_classification(n_samples=2000, n_features=20, n_informative=15,
8                           random_state=42)
9
10estimators = {
11    'Stump (depth=1)': DecisionTreeClassifier(max_depth=1),
12    'Tree (depth=3)': DecisionTreeClassifier(max_depth=3),
13    'Extra Tree (depth=2)': ExtraTreeClassifier(max_depth=2),
14    'Logistic Regression': LogisticRegression(max_iter=200),
15}
16
17for name, base in estimators.items():
18    ada = AdaBoostClassifier(
19        estimator=base,
20        n_estimators=100,
21        learning_rate=0.8,
22        algorithm='SAMME',
23        random_state=42
24    )
25    scores = cross_val_score(ada, X, y, cv=5, scoring='accuracy')
26    print(f"{name:30s} Accuracy: {scores.mean():.4f} +/- {scores.std():.4f}")

AdaBoost for Regression

AdaBoost also supports regression through AdaBoostRegressor. The default base estimator is DecisionTreeRegressor(max_depth=3).

python
1from sklearn.ensemble import AdaBoostRegressor
2from sklearn.tree import DecisionTreeRegressor
3from sklearn.datasets import make_regression
4from sklearn.model_selection import cross_val_score
5
6X, y = make_regression(n_samples=1000, n_features=20, random_state=42)
7
8ada_reg = AdaBoostRegressor(
9    estimator=DecisionTreeRegressor(max_depth=4),
10    n_estimators=100,
11    learning_rate=0.5,
12    random_state=42
13)
14
15scores = cross_val_score(ada_reg, X, y, cv=5, scoring='r2')
16print(f"AdaBoost Regressor R2: {scores.mean():.4f}")

Common Pitfalls

Using a base estimator that does not support sample_weight. AdaBoost relies on weighted samples to focus on misclassified instances. If the base estimator's fit method does not accept sample_weight, scikit-learn will raise a TypeError. Check the estimator's documentation before using it.

Setting too many estimators with a strong base learner. If your base estimator is already a strong learner (for example, a tree with depth 10), using 500 boosting rounds will almost certainly overfit. Stronger base learners need fewer iterations and a lower learning rate.

Ignoring the learning_rate parameter. The learning rate controls how much each new estimator contributes to the ensemble. A lower rate (0.01 to 0.5) requires more estimators but often produces better generalization. The default of 1.0 works well only with weak base learners like stumps.

Using base_estimator instead of estimator. The base_estimator parameter was deprecated in scikit-learn 1.2 and removed in 1.4. Use estimator instead to avoid deprecation warnings or errors.

Not scaling features for linear base learners. Decision trees are invariant to feature scaling, but Logistic Regression and SVM are not. When using those as base estimators, standardize your features first with StandardScaler.

Summary

AdaBoost in scikit-learn supports any classifier or regressor that implements sample_weight in its fit method. Decision stumps remain the most popular choice because they are fast and weak enough to benefit from boosting. Deeper trees, logistic regression, SVM, and extra trees are all valid alternatives, each with different trade-offs between speed, capacity, and overfitting risk. When switching to a stronger base estimator, reduce the learning rate and the number of estimators to maintain good generalization.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.