machine learning
scikit-learn
ensemble learning
regression
python programming

Ensemble of different kinds of regressors using scikit-learn or any other python framework

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

Combining different types of regressors can improve robustness when no single model family captures the whole signal. In scikit-learn, the two most common ways to do that are averaging predictions from heterogeneous models with VotingRegressor or learning how to combine them with StackingRegressor.

Why Mix Different Regressors

Different regressors have different biases:

  • linear models capture simple additive structure
  • tree ensembles capture nonlinear interactions
  • kernel methods can fit smooth nonlinear patterns

If their errors are not identical, combining them can outperform any single model. The key phrase is not "more models" but "diverse models whose mistakes differ".

Simple Averaging with VotingRegressor

VotingRegressor fits several base regressors and averages their predictions.

python
1from sklearn.datasets import make_regression
2from sklearn.ensemble import RandomForestRegressor, VotingRegressor
3from sklearn.linear_model import Ridge
4from sklearn.neighbors import KNeighborsRegressor
5from sklearn.model_selection import train_test_split
6from sklearn.metrics import mean_squared_error
7
8X, y = make_regression(n_samples=500, n_features=10, noise=20, random_state=42)
9X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
10
11ensemble = VotingRegressor([
12    ("ridge", Ridge()),
13    ("rf", RandomForestRegressor(n_estimators=200, random_state=42)),
14    ("knn", KNeighborsRegressor(n_neighbors=8)),
15])
16
17ensemble.fit(X_train, y_train)
18pred = ensemble.predict(X_test)
19
20print(mean_squared_error(y_test, pred))

This is the easiest heterogeneous ensemble in scikit-learn. It works well when the base models are reasonable and their errors are complementary.

Stacking for a Learned Combination

Averaging treats every model equally. Stacking lets a meta-model learn how to combine base predictions.

python
1from sklearn.ensemble import RandomForestRegressor, StackingRegressor
2from sklearn.linear_model import Ridge, LinearRegression
3from sklearn.svm import SVR
4
5stack = StackingRegressor(
6    estimators=[
7        ("ridge", Ridge()),
8        ("rf", RandomForestRegressor(n_estimators=200, random_state=42)),
9        ("svr", SVR()),
10    ],
11    final_estimator=LinearRegression()
12)
13
14stack.fit(X_train, y_train)
15pred = stack.predict(X_test)
16print(mean_squared_error(y_test, pred))

This is often stronger than plain averaging, but it also adds complexity and a greater risk of overfitting if the base models are not validated carefully.

Choose Base Models Deliberately

Good ensembles are not random bags of algorithms. Choose models that bring meaningfully different structure.

A practical starting combination is often:

  • one linear model such as Ridge
  • one tree ensemble such as RandomForestRegressor or GradientBoostingRegressor
  • one distance- or kernel-based model such as KNeighborsRegressor or SVR

That gives diversity without making the system impossible to tune.

If all base regressors are just small variations of the same tree model, the ensemble may add little beyond more compute time.

Evaluate the Ensemble Honestly

Always compare the ensemble against its strongest individual members.

python
1models = {
2    "ridge": Ridge(),
3    "rf": RandomForestRegressor(n_estimators=200, random_state=42),
4    "knn": KNeighborsRegressor(n_neighbors=8),
5    "ensemble": ensemble,
6}
7
8for name, model in models.items():
9    model.fit(X_train, y_train)
10    pred = model.predict(X_test)
11    mse = mean_squared_error(y_test, pred)
12    print(name, mse)

This matters because ensembles are not automatically better. Sometimes one strong model is simpler and just as accurate.

Other Frameworks Exist, But scikit-learn Is Enough for Most Cases

Libraries such as XGBoost, LightGBM, CatBoost, and mlxtend can also participate in heterogeneous ensembles, but if your goal is simply "combine different regressors", scikit-learn already covers the main patterns well.

Use another framework when you need:

  • a stronger single gradient-boosted model
  • specialized categorical handling
  • custom training workflows beyond sklearn's estimator interface

Do not leave sklearn unless there is a concrete reason.

Common Pitfalls

  • Assuming an ensemble is better just because it contains more models.
  • Combining highly similar regressors and getting little diversity benefit.
  • Skipping comparison against the best single model.
  • Using stacking without enough validation, which can overfit.
  • Forgetting that ensemble complexity increases training time, tuning cost, and maintenance.

Summary

  • Heterogeneous regression ensembles work best when the base models have different error patterns.
  • 'VotingRegressor is the simplest way to average several kinds of regressors.'
  • 'StackingRegressor can learn a stronger combination, but it is more complex.'
  • Compare the ensemble against individual base models instead of assuming it wins.
  • Use diversity and validation, not algorithm collection, as the core design principle.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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