【问题标题】:How to use GridSearchCV for OneVsRestClassifier with SVC estimator?如何使用带有 SVC 估计器的 OneVsRestClassifier 的 GridSearchCV?
【发布时间】:2020-03-02 13:30:03
【问题描述】:

我正在尝试将 OneVsRestClassifier 与 SVC 一起用于图像的多分类问题 - 我从 CellProfiler 获得了图像的数字特征。我想使用 GridSearchCV 来查找要使用的超参数,但我卡住了。

有人对此有解决方案/建议吗?

我已通过 Google 阅读,但似乎无法解决我的问题。

    grid = GridSearchCV(pipe, scoring='f1',
                       param_grid=param_grid, cv=5,
                       return_train_score=True,
                       iid=False,
                       n_jobs=-1
                       )
    grid.fit(X_train, np.ravel(y_train))
    return grid
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.pipeline import Pipeline
from sklearn.svm import SVC
from sklearn.multiclass import OneVsRestClassifier
from sklearn.metrics import classification_report

pipe = make_pipeline(StandardScaler(),
                     OneVsRestClassifier(SVC(probability=True)))

param_grid = {
    'estimator__C': [0.001, 0.01, 0.1, 1, 10, 100],
    'estimator__kernel': ['linear', 'rbf', 'poly'],
    'estimator__degree': [2, 3, 4, 5, 7, 10],
    'estimator__gamma': [0.01, 0.02, 0.03, 0.04, 0.05, 1]
}

clf = grid_search_fit(pipe, param_grid)

preds = clf.predict(X_test)
print(classification_report(y_test, preds, target_names = ['empty', 'good', 'blurred']))
ValueError: Invalid parameter estimator for estimator Pipeline(memory=None,
         steps=[('standardscaler',
                 StandardScaler(copy=True, with_mean=True, with_std=True)),
                ('onevsrestclassifier',
                 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=True,
                                                   random_state=None,
                                                   shrinking=True, tol=0.001,
                                                   verbose=False),
                                     n_jobs=None))],
         verbose=False). Check the list of available parameters with `estimator.get_params().keys()`.

【问题讨论】:

    标签: python classification svc gridsearchcv


    【解决方案1】:

    我对你的代码做了如下修改:

    1. 删除了选项 iid=False
    2. 我稍微改变了 Pipeline 和 GridSearchCV 的形状

    修改后的代码如下。您可以或多或少地像这样构建 Pipeline 和 Gridsearch。

    from sklearn.preprocessing import StandardScaler
    from sklearn.pipeline import make_pipeline
    from sklearn.pipeline import Pipeline
    from sklearn.svm import SVC
    from sklearn.multiclass import OneVsRestClassifier
    from sklearn.metrics import classification_report
    
    pipe = Pipeline([
        ("scale", StandardScaler()),
        ('classify', OneVsRestClassifier(SVC(probability=True)))
    ])
        
    param_grid = {
        'classify__estimator__C': [0.001, 0.01, 0.1, 1, 10, 100],
        'classify__estimator__kernel': ['linear', 'rbf', 'poly'],
        'classify__estimator__degree': [2, 3, 4, 5, 7, 10],
        'classify__estimator__gamma': [0.01, 0.02, 0.03, 0.04, 0.05, 1]
    }
    
    grid_search = GridSearchCV(
        pipe, param_grid, cv=5, scoring='f1', verbose=1, return_train_score=True, n_jobs=-1)
    
    grid_search = grid_search.fit(X_train, np.ravel(y_train))
    
    preds = clf.predict(X_test)
    print(classification_report(y_test, preds, target_names = ['empty', 'good', 'blurred']))
    

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2016-07-22
      • 2012-09-19
      • 2021-01-12
      • 2016-02-20
      • 2020-01-15
      • 2020-08-26
      • 2015-06-06
      • 2020-07-14
      相关资源
      最近更新 更多