我正在运行一个使用OneVsRestClasssifer
和SVC
作为估计器的GridSearchCV
。这是我的Pipeline
和GridSearchCV
参数的设置:
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
degree : int, optional (default=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
时,不应该忽略所有degree
值吗?
回答:
这里显示的输出只是GridSearchCV
传递给内部估计器(即SVC
)的不同参数组合。但它们是否被使用取决于SVC
。在这种情况下,SVC
不会抛出任何错误,但也不会使用degree
。你应该打印出所有你怀疑的参数组合的分数。它们应该是相等的。这将告诉你degree
参数未被使用。
注意:请确保设置GridSearchCV
的random_state
以重复测试。
解释:GridSearchCV的工作只是传递参数,将训练数据传递给估计器进行拟合,然后使用测试数据进行评分,并返回产生最佳分数的参数组合。
当向估计器传递不兼容的参数组合时,是否忽略参数或引发错误取决于实现方式。
例如,在LogisticRegression中有两个参数:
penalty : str, ‘l1’ or ‘l2’, default: ‘l2’ 用于指定惩罚中使用的范数。solver : {‘newton-cg’, ‘lbfgs’, ‘liblinear’, ‘sag’}, default: ‘liblinear’. 用于优化问题的算法。 ‘newton-cg’, ‘lbfgs’和‘ sag’只能处理L2惩罚。
如你所见,如果我使用l1
惩罚和newton-cg
求解器,会导致不兼容。因此,估计器可能会完全忽略惩罚参数或抛出错误。在这种情况下,它会抛出错误。