2017-04-09 65 views
0

我正在运行GridSearchCV,其中OneVsRestClasssifer使用SVC作为估算值​​。这是我PipelineGridSearchCV参数方面:GridSearchCV是用rbf内核和不同程度计算SVC吗?

pipeline = Pipeline([ 
    ('clf', OneVsRestClassifier(SVC(verbose=True), n_jobs=1)), 
    ]) 

parameters = { 
    "clf__estimator__C": [0.1, 1], 
    "clf__estimator__kernel": ['poly', 'rbf'], 
    "clf__estimator__degree": [2, 3], 
} 

grid_search_tune = GridSearchCV(pipeline, parameters, cv=2, n_jobs=8, verbose=10) 
grid_search_tune.fit(train_x, train_y) 

根据SVC的文档degree参数仅用于由poly内核:

http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html

度:INT,可选(默认= 3)

多项式内核的程度 函数('poly')。被所有其他内核忽略。

,但是当我看到我的GridSearchCV的输出似乎它的计算与一个rbf内核的不同值degree参数每个SVC配置不同的运行。

[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=2 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=poly, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=3 
[CV] clf__estimator__kernel=rbf, clf__estimator__C=0.1, clf__estimator__degree=3 

当内核设置为rbf时,不应该忽略度数的所有值吗?

回答

1

此处显示的输出仅为GridSearchCV传递给内部估计器的参数的不同组合,即SVC。但是否使用它们取决于SVC。在这种情况下,SVC不会抛出任何错误,但也不会使用degree。你应该打印你怀疑的所有组合的分数。他们应该是平等的。这会告诉你degree参数未被使用。

注意:确保设置GridSearchCVrandom_state复制测试。

说明: GridSearchCV的工作是对参数,列车数据只是传递给估计器,用于拟合,然后使用该测试数据进行打分,并且导致这导致最佳得分的参数的组合。

当参数的不兼容组合传递给估计器时,它取决于实现,参数是被忽略还是引发错误。

例如,在LogisticRegression,有两个参数:

penalty : str, ‘l1’ or ‘l2’, default: ‘l2’ 
     Used to specify the norm used in the penalization. 

solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’. 
     Algorithm to use in the optimization problem. 
     ‘newton-cg’, ‘lbfgs’ and ‘sag’ only handle L2 penalty. 

正如你可以看到,如果我用l1处罚与​​解算器,它会导致不兼容。所以估计者可能会选择忽略惩罚参数或者抛出错误。在这种情况下,它会引发错误。

+0

感谢您的回答,我必须检查分数,但我怀疑对于每个不同的度数值,运行一个SVC(kernel ='rbf')正在计算,如果是真的,这是浪费时间,因为所有的分数应该是相同的。 GridSearchCV应该是“聪明的”,足以放弃这些分数,我会写关于它的sklearn邮件列表。 –

+1

@DavidBatista是的。相同的运行将针对不同的度数值进行计算。确定邮件列表。我们可以将字典更改为只有兼容组合的字典列表。像'tuned_pa​​rameters'在:http://scikit-learn.org/stable/auto_examples/model_selection/grid_search_digits.html#sphx-glr-auto-examples-model-selection-grid-search-digits-py –

+0

啊,很好,这是一个简单的解决方案!谢谢 :) –