I just reviewed very good example of fitting StackingRegressor from mlxtend package.

from mlxtend.regressor import StackingRegressor 
from sklearn.linear_model import LinearRegression 
from sklearn.linear_model import Ridge 
from sklearn.svm import SVR 
import matplotlib.pyplot as plt 
import numpy as np

# Generating a sample dataset 
X = np.sort(5 * np.random.rand(40, 1), axis=0) 
y = np.sin(X).ravel()
y[::5] += 3 * (0.5 - np.random.rand(8))

# Initializing models

lr = LinearRegression() 
svr_lin = SVR(kernel='linear') 
ridge = Ridge(random_state=1) 
svr_rbf = SVR(kernel='rbf')

stregr = StackingRegressor(regressors=[svr_lin, lr, ridge], 

# Training the stacking regressor

stregr.fit(X, y) 

# Evaluate and visualize the fit

print("Mean Squared Error: %.4f"
      % np.mean((stregr.predict(X) - y) ** 2)) 
print('Variance Score: %.4f' % stregr.score(X, y))

with plt.style.context(('seaborn-whitegrid')):
    plt.scatter(X, y, c='lightgray')
    plt.plot(X, stregr.predict(X), c='darkgreen', lw=2)


enter image description here

But, when I changed one line of generating the dataset, like:

y = np.sin(X).ravel() * np.cos(X).ravel()

I got totally bad fit of the StackerRegressor. enter image description here

Please don't block that question and help me to understand. Thank you!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.