具有参数弹性值误差的交叉 Val 分数

Cross Val Score with Elastic Value Error for parameters

我尝试使用 cross_val_score() 函数和嵌套交叉验证在 python 中实现一个简单的弹性网络回归,但它不允许我传递参数。它一直从我的 l1_ratio 的无效参数中声明一个 ValueError,我不明白为什么它介于 0 和 1 之间。

ValueError: Invalid parameter l1_ratio for estimator Pipeline(steps=[('preprocessor',
                 ColumnTransformer(remainder='passthrough',
                                   transformers=[('cat', OneHotEncoder(), [0]),
                                                 ('num', StandardScaler(),
                                                  slice(1, 37, None))])),
                ('model', ElasticNet(random_state=42))]).
Check the list of available parameters with `estimator.get_params().keys()`.

我的代码:

cv_outer=KFold(10, shuffle=True)
cv_inner=KFold(10, shuffle=True)
models_params = {
    'en': (LREN(random_state=42), # Elastic Net
        {'l1_ratio': [0,0.25,0.5,0.75,1]
         ,'alpha':[1e-2,1e-1,1,1e1]})
# My first column is Categorical, the other 36 are numerical
preprocessor = ColumnTransformer(
                    transformers=[
                        ('cat', OneHotEncoder(), [0])
                        ,('num', StandardScaler(), slice(1,37))
                    ]
                    ,remainder = 'passthrough')
# Store Results
average_scores = dict()
for name, (model, params) in models_params.items():
    mymodel = Pipeline(steps = [('preprocessor', preprocessor),
                                ('model', model)
                                ])
        # this object is a regressor that also happens to choose
        # its hyperparameters automatically using `inner_cv`
    optimize_hparams = GridSearchCV(
            estimator = mymodel, param_grid=params, n_jobs = -1,
            cv=cv_inner, scoring='neg_mean_absolute_error')
# estimate generalization error on the outer-fold splits of the data
    outer_folds_scores = cross_val_score(
        optimize_hparams,
        X, y, cv=cv_outer, scoring='neg_mean_absolute_error')

您可以尝试定义您的网格,例如:

models_params = {
        {'model__l1_ratio': [0,0.25,0.5,0.75,1],
         'model__alpha':[1e-2,1e-1,1,1e1]}
    }