Python 估算器管道(SVR)的参数无效
我有一个包含100列连续特征和一个连续标签的数据集,我想运行SVR;提取相关性特征,调整超参数,然后交叉验证适合我的数据的模型 我写了这段代码:Python 估算器管道(SVR)的参数无效,python,machine-learning,scikit-learn,svm,pipeline,Python,Machine Learning,Scikit Learn,Svm,Pipeline,我有一个包含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
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()`.
ValueError: Estimator names must not contain __: got ['estimator__sel', 'estimator__svr']
但当我改变路线时:
pipeline = Pipeline(steps=[('sel',fs), ('svr', model)])
致:
我得到一个错误:
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()`.
ValueError: Estimator names must not contain __: got ['estimator__sel', 'estimator__svr']
有人能解释一下我做错了什么,即如何将管道/功能选择步骤组合到GridSearchCV中
作为旁注,如果我在GridSearchCV中注释掉管道
,并取消注释estimator=SVR(kernal='rbf')
,单元格运行时不会出现问题,但在这种情况下,我假定我没有将特征选择合并到中,因为它不会在任何地方调用。我以前见过一些SO问题,例如,但它们似乎没有回答这个具体问题
是否有更简洁的方法来编写此消息?第一条错误消息是关于
管道
参数,而不是搜索
参数,并指示您的参数网格
不正确,而不是管道步骤名称。运行pipeline.get_params().keys()
应该会显示正确的参数名称。您的网格应该是:
参数网格={
“svr____________________________________,
“svr__ε”:[0.0001,0.0005,0.001,0.005,0.01,0.05,1,5,10],
“svr__γ”:[0.0001,0.0005,0.001,0.005,0.01,0.05,1,5,10]
},
我不知道如何用普通SVR代替管道运行;您的参数网格也没有指定正确的内容…谢谢。当我将param_网格更改为您的代码时,它会运行(在y_pred和y_test之间有一个非常糟糕的Spearman)。如果真的是这样的话,那没关系,但我想检查一下,这不是因为模型制作不当。我不明白“我不知道如何用普通SVR代替管道运行;您的参数网格也没有在那里指定正确的内容…'。你是说这个代码不应该运行吗?我把代码单独放在一个文件中,只是在它前面读入了一些数据,当我打印
y_pred=results.predict(X_test)时;打印y_pred[0:10]
,打印10个预测。@Slowat_Kela我指的是你的“旁注”段落。如果estimator=SVR(…)
,则在参数网格
中使用estimator_usvr_uc
应失败,错误与您最初报告的错误相同;在这个版本中,参数名应该是C
。哦,对不起,这是我的错,我不清楚。我有estimator=SVR(…)
in,当我在param网格中有C、epsilon和gamma时对不起(不是estimator\u SVR\u C
)。抱歉,我不清楚,我的意思是,如果我只使用普通SVR,我可以让这段代码正常运行,但如果我将其交换到管道,则无法运行。