Why the 'weight' of the AveragingModels don't work when using cross_val_score? Whatever the 'weight' input is, the result is same.(score1=score2)But my lasso, ENet, KRR haves different predictions. What's the problem?

import numpy as np
from sklearn.linear_model import Lasso,ElasticNet
from sklearn.kernel_ridge import KernelRidge
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.preprocessing import RobustScaler
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import cross_val_score

lasso = make_pipeline(RobustScaler(), Lasso(alpha =0.00035, random_state=1)) 
ENet = make_pipeline(RobustScaler(), ElasticNet(alpha=0.0004, l1_ratio=0.885)) 
KRR = KernelRidge(alpha=[0.004], kernel='polynomial', degree= 0.39, coef0=2.5)

class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
    def __init__(self, models ,weight=[]):
        self.models = models
        if weight: 
            self.weig=(np.array([weight])/sum(weight)).T
        else:
            self.weig=np.array([[1/len(models)]]*len(models))
    def fit(self, X, y):
        self.models_ = [clone(model) for model in self.models]
        for model in self.models_:
            model.fit(X, y)
        return self    
    def predict(self, X):
        self.multi_predictions = np.column_stack([model.predict(X) for model in self.models_])
        return ([email protected]).squeeze()


def rmsle_cv(model):
    rmse= np.sqrt(-cross_val_score(model, train.values, y.values, scoring="neg_mean_squared_error", cv = 5))
    return rmse

averaged_models1 = AveragingModels(models = (ridge,lasso,ENet),weight=[1,0,0])  
score1 = rmsle_cv(averaged_models1)   
averaged_models2 = AveragingModels(models = (ridge,lasso,ENet),weight=[1,1,1]) 
score2 = rmsle_cv(averaged_models2)  

print(score1.mean(),score1.std(),score2.mean(),score2.std())

0 Answers