GridSearch在OneVsRestClassifier中查找一个估计量 [英] GridSearch for an estimator inside a OneVsRestClassifier
问题描述
我想在SVC模型中执行GridSearchCV,但这使用了一对多"策略.对于后一部分,我可以这样做:
I want to perform GridSearchCV in a SVC model, but that uses the one-vs-all strategy. For the latter part, I can just do this:
model_to_set = OneVsRestClassifier(SVC(kernel="poly"))
我的问题是参数.假设我想尝试以下值:
My problem is with the parameters. Let's say I want to try the following values:
parameters = {"C":[1,2,4,8], "kernel":["poly","rbf"],"degree":[1,2,3,4]}
为了执行GridSearchCV,我应该做类似的事情:
In order to perform GridSearchCV, I should do something like:
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的参数.
Basically, since the SVC is inside a OneVsRestClassifier and that's the estimator I send to the GridSearchCV, the SVC's parameters can't be accessed.
为了完成我想要的,我看到了两种解决方案:
In order to accomplish what I want, I see two solutions:
- 在创建SVC时,请以某种方式告诉它不要使用一对一策略,而是使用一对多策略.
- 以某种方式指示GridSearchCV,该参数对应于OneVsRestClassifier中的估计器.
我还没有找到一种方法来做上述提到的任何一种选择.您知道是否有一种方法可以做到吗?也许您可以建议另一种获得相同结果的方法?
I'm yet to find a way to do any of the mentioned alternatives. Do you know if there's a way to do any of them? Or maybe you could suggest another way to get to the same result?
谢谢!
推荐答案
在网格搜索中使用嵌套估计量时,可以使用__
作为分隔符来确定参数的范围.在这种情况下,SVC模型将作为名为estimator
的属性存储在OneVsRestClassifier
模型内:
When you use nested estimators with grid search you can scope the parameters with __
as a separator. In this case the SVC model is stored as an attribute named estimator
inside the OneVsRestClassifier
model:
from sklearn.datasets import load_iris
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import f1_score
iris = load_iris()
model_to_set = OneVsRestClassifier(SVC(kernel="poly"))
parameters = {
"estimator__C": [1,2,4,8],
"estimator__kernel": ["poly","rbf"],
"estimator__degree":[1, 2, 3, 4],
}
model_tunning = GridSearchCV(model_to_set, param_grid=parameters,
score_func=f1_score)
model_tunning.fit(iris.data, iris.target)
print model_tunning.best_score_
print model_tunning.best_params_
结果是:
0.973290762737
{'estimator__kernel': 'poly', 'estimator__C': 1, 'estimator__degree': 2}
这篇关于GridSearch在OneVsRestClassifier中查找一个估计量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!