我想在 SVC 模型中执行 GridSearchCV,但它使用一对多策略。对于后一部分,我可以这样做:
model_to_set = OneVsRestClassifier(SVC(kernel="poly"))
我的问题是参数。假设我想尝试以下值:
parameters = {"C":[1,2,4,8], "kernel":["poly","rbf"],"degree":[1,2,3,4]}
为了执行 GridSearchCV,我应该执行以下操作:
cv_generator = StratifiedKFold(y, k=10)
model_tunning = GridSearchCV(model_to_set, param_grid=parameters, score_func=f1_score, n_jobs=1, cv=cv_generator)
但是,然后我执行它我得到:
Traceback (most recent call last):
File "/.../main.py", line 66, in <module>
argclass_sys.set_model_parameters(model_name="SVC", verbose=3, file_path=PATH_ROOT_MODELS)
File "/.../base.py", line 187, in set_model_parameters
model_tunning.fit(self.feature_encoder.transform(self.train_feats), self.label_encoder.transform(self.train_labels))
File "/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.py", line 354, in fit
return self._fit(X, y)
File "/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.py", line 392, in _fit
for clf_params in grid for train, test in cv)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 473, in __call__
self.dispatch(function, args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 296, in dispatch
job = ImmediateApply(func, args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/externals/joblib/parallel.py", line 124, in __init__
self.results = func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sklearn/grid_search.py", line 85, in fit_grid_point
clf.set_params(**clf_params)
File "/usr/local/lib/python2.7/dist-packages/sklearn/base.py", line 241, in set_params
% (key, self.__class__.__name__))
ValueError: Invalid parameter kernel for estimator OneVsRestClassifier
基本上,由于 SVC 位于 OneVsRestClassifier 内,并且这是我发送到 GridSearchCV 的估计器,因此无法访问 SVC 的参数。
为了完成我想要的,我看到了两个解决方案:
- 在创建 SVC 时,以某种方式告诉它不要使用一对一的策略,而是使用一对多的策略。
- 以某种方式指示 GridSearchCV 参数对应于 OneVsRestClassifier 内的估计器。
我还没有找到一种方法来做任何提到的替代方案。你知道是否有办法做到这些吗?或者,也许您可以建议另一种方法来获得相同的结果?
谢谢!