我想在OnevsRest分类器上进行网格搜索,而我的模型是SVC,但是它显示了以下有关使用网格搜索的错误-如何解决?
代码-
from sklearn.model_selection import GridSearchCV
# defining parameter range
param_grid = {'C': [0.1, 1, 10, 100, 1000],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['rbf']}
svc_model_orc = OneVsRestClassifier(SVC())
grid = GridSearchCV(svc_model_orc, param_grid, refit = True, verbose = 3)
# fitting the model for grid search
grid.fit(X_train, y_train)
# svc_pred_train=grid.predict(X_train)
# svc_pred_test = grid.predict(X_valid)
# print(accuracy_score(y_train, svc_pred_train))
# print(f1_score(y_train, svc_pred_train, average='weighted'))
# print(accuracy_score(y_valid, svc_pred_test))
# print(f1_score(y_valid, svc_pred_test, average='weighted'))
错误-
ValueError: Invalid parameter C for estimator OneVsRestClassifier(estimator=SVC(C=1.0, cache_size=200, class_weight=None,
coef0=0.0, decision_function_shape='ovr',
degree=3, gamma='auto_deprecated',
kernel='rbf', max_iter=-1, probability=False,
random_state=None, shrinking=True, tol=0.001,
verbose=False),
n_jobs=None). Check the list of available parameters with `estimator.get_params().keys()`.
由于您正在对嵌套估计量执行GridSearch
(即使您只有一个,但是OneVsRestClassifier
适合每个类的分类器),所以您需要使用语法estimator__some_parameter
定义参数。
[在具有嵌套对象的情况下,例如在管道中,这是GridSerach
期望访问不同模型的参数(即<component>__<parameter>
)的语法。在这种情况下,您需要为每个模型命名,然后将其参数设置为SVC__some_parameter
,例如,对于SVC参数。但是对于这种情况,分类器位于estimator
下,请注意,实际模型是通过estimator
属性访问的:
print(svc_model_orc.estimator)
SVC(C=1.0, break_ties=False, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='scale', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
因此,在这种情况下,您应该将参数网格设置为:
param_grid = {'estimator__C': [0.1, 1, 10, 100, 1000],
'estimator__gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'estimator__kernel': ['rbf']}