3

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

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

我已经阅读了谷歌,但似乎我无法解决我的问题。

    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()`.
4

1 回答 1

0

我对您的代码进行了一些修改,如下所示:

  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']))
于 2021-07-06T14:05:43.757 回答