【问题标题】:How to implement grid searchcv for onevsrestclassifier of SVC classifierSVC分类器的onevsrestclassifier如何实现grid searchcv
【发布时间】:2020-08-26 19:38:46
【问题描述】:

我想在 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()`.

【问题讨论】:

    标签: machine-learning classification grid-search multiclass-classification svc


    【解决方案1】:

    由于您在嵌套估计器上执行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']}  
    

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 2019-06-22
      • 2020-01-03
      • 2013-04-30
      • 2017-01-28
      • 2012-08-19
      • 2016-02-20
      • 2019-07-14
      • 2017-03-14
      相关资源
      最近更新 更多