Stacking Regressor is an ensemble learning technique that combines multiple regression models via a meta-regressor. The base level models are trained based on the complete training set, then the meta-regressor is fitted based on the outputs—predictions of these base models. The goal is to blend different regression models to improve the overall prediction accuracy compared to any single model in the ensemble.
Stacking involves two or more base regressors and a meta-regressor. The base regressors are individual models that learn to predict the target variable. The meta-regressor then learns how to best combine these predictions into a final prediction. This approach leverages the strengths and mitigates the weaknesses of individual models.
Let be a training set of samples, where is the feature vector, and is the corresponding target value. Suppose we have base regressors, . The stacking regressor works in two main steps:
The final prediction for a new sample is obtained by first predicting with the base regressors to get , then inputting into the meta-regressor to get the final prediction .
estimators
: List[Estimator]
final_estimator
: Estimator
, default = LinearRegressor()
pass_original
: bool
, default = Falsecv
: int
, default = 5fold_type
: FoldType
, default = KFold
shuffle
: bool
, default = Truerandom_state
: int
, default = None**kwargs
: Dict[str, Any]
Test on the synthesized dataset of the curve :
from luma.ensemble.stack import StackingRegressor
from luma.regressor.neighbors import KNNRegressor
from luma.regressor.svm import KernelSVR
from luma.regressor.tree import DecisionTreeRegressor
from luma.regressor.poly import PolynomialRegressor
from luma.regressor.linear import KernelRidgeRegressor
from luma.preprocessing.scaler import StandardScaler
from luma.metric.regression import MeanSquaredError
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
X = np.linspace(-5, 5, 500).reshape(-1, 1)
y = (np.cos(2 * X) * np.sin(np.exp(X / 2))).flatten()
y += 0.2 * np.random.randn(500)
sc = StandardScaler()
y_trans = sc.fit_transform(y)
estimators = [
PolynomialRegressor(deg=9, alpha=0.01, regularization='l2'),
KNNRegressor(n_neighbors=10),
KernelSVR(C=1.0, gamma=3.0, kernel='rbf'),
DecisionTreeRegressor(max_depth=7, random_state=42),
KernelRidgeRegressor(alpha=1.0, gamma=1.0, kernel='rbf')
]
stack = StackingRegressor(estimators=estimators,
pass_original=False,
cv=5,
shuffle=True,
verbose=True)
stack.fit(X, y_trans)
y_pred = stack.predict(X)
score = stack.score(X, y_trans, metric=MeanSquaredError)
fig = plt.figure(figsize=(10, 5))
plt.scatter(X, y_trans,
s=10, c='black', alpha=0.3,
label=r'$y=\cos{2x}\cdot\sin{e^{x/2}}+\epsilon$')
for est in stack:
est_pred = est.predict(X)
plt.plot(X, est_pred,
alpha=0.5, label=f'{type(est).__name__}')
plt.fill_between(X.flatten(), est_pred, y_pred, alpha=0.05)
plt.plot(X, y_pred, lw=2, c='blue', label='Predicted Plot')
plt.legend()
plt.xlabel('x')
plt.ylabel('y (Standardized)')
plt.title(f'StackingRegressor [MSE: {score:.4f}]')
plt.tight_layout()
plt.show()
- Breiman, Leo. "Stacked regressions." Machine learning 24.1 (1996): 49-64.
- Wolpert, David H. "Stacked generalization." Neural networks 5.2 (1992): 241-259.