GridSearchCV 是用 rbf 内核和不同程度计算 SVC 吗?

数据挖掘 机器学习 scikit-学习 支持向量机 超参数
2022-02-27 23:51:22

我正在运行一个GridSearchCV使用OneVsRestClasssifer作为SVC估计器的。这是我PipelineGridSearchCV参数的方面:

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它似乎正在为每个配置计算不同的运行,每个SVC配置都有一个rbf内核和不同的degree参数值。

[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。试试下面的代码并将其传递给 param_grid 参数。

param_grid = [
  {'C': [1, 10, 100, 1000], 'kernel': ['linear']},
  {'C': [1, 10, 100, 1000], 'gamma': [0.001, 0.0001], 'kernel': ['rbf']},
 ]

您可以浏览 GridSearchCV 文档以获取特定示例。看看这里的例子(看3.2.1)