如果param_grid中的kernel =线性,如何选择线性SVC而不是SVC?

发布于 2025-01-21 15:17:45 字数 690 浏览 2 评论 0原文

我有以下方法来创建GRID_CV_OBJECT。其中hyperpam_grid = {“ c”:c,“ kernel”:kernel,“ gamma”:gamma:gamma,“ geger”:deg}

grid_cv_object = GridSearchCV(
        estimator = SVC(cache_size=cache_size),
        param_grid = hyperpam_grid,
        cv = cv_splits,
        scoring = make_scorer(matthews_corrcoef), # a callable returning single value, binary and multiclass labels are supported
        n_jobs = -1, # use all processors
        verbose = 10,
        refit = refit
    )

这里的内核可以是('rbf','linear','poly')

如何为“线性”内核执行线性选择?由于这嵌入到hyperparam_grid中,因此我不确定如何创建这种“ Switch”。

如果可能的话,我只是不想拥有2个单独的grid_cv_objects。

I have the following way to create the grid_cv_object. Where hyperpam_grid={"C":c, "kernel":kernel, "gamma":gamma, "degree":degree}.

grid_cv_object = GridSearchCV(
        estimator = SVC(cache_size=cache_size),
        param_grid = hyperpam_grid,
        cv = cv_splits,
        scoring = make_scorer(matthews_corrcoef), # a callable returning single value, binary and multiclass labels are supported
        n_jobs = -1, # use all processors
        verbose = 10,
        refit = refit
    )

Here kernel can be ('rbf', 'linear', 'poly') for example.

How can I enforce the selection of LinearSVC for the 'linear' kernel? Since this is embedded in hyperparam_grid I'm not sure how to create this sort of "switch".

I just don't want to have 2 separate grid_cv_objects if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁绊已千年 2025-01-28 15:17:45

尝试在以下形式的

from sklearn.dummy import DummyClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline

search_spaces = [
    {'svm': [SVC(kernel='rbf')],
     'svm__gamma': ('scale', 'auto'),
     'svm__C': (0.1, 1.0, 10.0)},
    {'svm': [SVC(kernel='poly')],
     'svm__degree': (2, 3),
     'svm__C': (0.1, 1.0, 10.0)},
    {'svm': [LinearSVC()],  # Linear kernel
     'svm__C': (0.1, 1.0, 10.)}
]
svm_pipe = Pipeline([('svm', DummyClassifier())])
grid = GridSearchCV(svm_pipe, search_spaces)

讨论中制作参数网格:

  1. 我们将不同的内核与不同实例的svc分开。这样,GridSearchCV将无法估计,例如,svc(kernel ='poly')带有不同的gamma s,这些 s s被忽略。 >'poly',仅针对rbf

  2. 当您要求时,linearsvc(实际上任何其他模型),而不是svc(kernel ='Linearear'),以估算线性SVM。<<<<<<<<<<<<<<<<<<<<< /p>

  3. 最佳估计器将为grid.best_estimator_.named_steps ['svm']

Try making parameter grids in the following form

from sklearn.dummy import DummyClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline

search_spaces = [
    {'svm': [SVC(kernel='rbf')],
     'svm__gamma': ('scale', 'auto'),
     'svm__C': (0.1, 1.0, 10.0)},
    {'svm': [SVC(kernel='poly')],
     'svm__degree': (2, 3),
     'svm__C': (0.1, 1.0, 10.0)},
    {'svm': [LinearSVC()],  # Linear kernel
     'svm__C': (0.1, 1.0, 10.)}
]
svm_pipe = Pipeline([('svm', DummyClassifier())])
grid = GridSearchCV(svm_pipe, search_spaces)

Discussion:

  1. We separate different kernels with different instances of SVC. This way, GridSearchCV will not estimate, say, SVC(kernel='poly') with different gammas, which are ignored for 'poly' and are designated only for rbf.

  2. As you request, LinearSVC (and in fact any other model), not SVC(kernel='linear'), is separated to estimate a linear svm.

  3. Best estimator will be grid.best_estimator_.named_steps['svm'].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文