估算器管道 (SVR) 的参数无效

Invalid parameter for estimator Pipeline (SVR)

我有一个包含 100 列连续特征和一个连续标签的数据集,我想 运行 SVR;提取相关特征,调整超参数,然后交叉验证适合我数据的模型。

我写了这段代码:

X_train, X_test, y_train, y_test = train_test_split(scaled_df, target, test_size=0.2)
    
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)

# define the pipeline to evaluate
model = SVR()
fs = SelectKBest(score_func=mutual_info_regression)
pipeline = Pipeline(steps=[('sel',fs), ('svr', model)])

# define the grid
grid = dict()

#How many features to try
grid['estimator__sel__k'] = [i for i in range(1, X_train.shape[1]+1)]


# define the grid search
#search = GridSearchCV(pipeline, grid, scoring='neg_mean_squared_error', n_jobs=-1, cv=cv)
search = GridSearchCV(
        pipeline,
#        estimator=SVR(kernel='rbf'),
        param_grid={
            'estimator__svr__C': [0.1, 1, 10, 100, 1000],
            'estimator__svr__epsilon': [0.0001, 0.0005,  0.001, 0.005,  0.01, 0.05, 1, 5, 10],
            'estimator__svr__gamma': [0.0001, 0.0005,  0.001, 0.005,  0.01, 0.05, 1, 5, 10]
        },
        scoring='neg_mean_squared_error',
        verbose=1,
        n_jobs=-1)

for param in search.get_params().keys():
    print(param)

# perform the search
results = search.fit(X_train, y_train)

# summarize best
print('Best MAE: %.3f' % results.best_score_)
print('Best Config: %s' % results.best_params_)

# summarize all
means = results.cv_results_['mean_test_score']
params = results.cv_results_['params']
for mean, param in zip(means, params):
    print(">%.3f with: %r" % (mean, param))

我收到错误:

ValueError: Invalid parameter estimator for estimator Pipeline(memory=None,
         steps=[('sel',
                 SelectKBest(k=10,
                             score_func=<function mutual_info_regression at 0x7fd2ff649cb0>)),
                ('svr',
                 SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1,
                     gamma='scale', kernel='rbf', max_iter=-1, shrinking=True,
                     tol=0.001, verbose=False))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.

当我按照错误消息中的建议打印 estimator.get_params().keys() 时,我得到:

cv
error_score
estimator__memory
estimator__steps
estimator__verbose
estimator__sel
estimator__svr
estimator__sel__k
estimator__sel__score_func
estimator__svr__C
estimator__svr__cache_size
estimator__svr__coef0
estimator__svr__degree
estimator__svr__epsilon
estimator__svr__gamma
estimator__svr__kernel
estimator__svr__max_iter
estimator__svr__shrinking
estimator__svr__tol
estimator__svr__verbose
estimator
iid
n_jobs
param_grid
pre_dispatch
refit
return_train_score
scoring
verbose
Fitting 5 folds for each of 405 candidates, totalling 2025 fits

但是当我更改行时:

pipeline = Pipeline(steps=[('sel',fs), ('svr', model)])

至:

pipeline = Pipeline(steps=[('estimator__sel',fs), ('estimator__svr', model)])

我收到错误:

ValueError: Estimator names must not contain __: got ['estimator__sel', 'estimator__svr']

谁能解释一下我做错了什么,即如何将 pipeline/feature 选择步骤合并到 GridSearchCV 中?

作为旁注,如果我在 GridSearchCV 中注释掉 pipeline,并取消注释 estimator=SVR(kernal='rbf'),单元格 运行s 没有问题,但在那种情况下,我想我我没有将功能选择纳入其中,因为它没有在任何地方调用。我看过一些以前的 SO 问题,例如,但他们似乎没有回答这个具体问题。

有没有更简洁的写法?

第一条错误消息是关于 pipeline 参数的,而不是 search 参数,并且表明您的 param_grid 是错误的,而不是管道步骤名称。 运行 pipeline.get_params().keys() 应该会显示正确的参数名称。您的网格应该是:

        param_grid={
            'svr__C': [0.1, 1, 10, 100, 1000],
            'svr__epsilon': [0.0001, 0.0005,  0.001, 0.005,  0.01, 0.05, 1, 5, 10],
            'svr__gamma': [0.0001, 0.0005,  0.001, 0.005,  0.01, 0.05, 1, 5, 10]
        },

我不知道如何用普通 SVR 代替管道运行;你的参数网格也没有指定正确的东西...