我正在尝试将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()`.
答案 0 :(得分:0)
我对您的代码做了如下修改:
更改后的代码如下。您可以或多或少像这样构建流水线和网格搜索。
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']))