网格搜索GridSearchCV

本文介绍了如何利用scikit-learn的GridSearchCV进行超参数调优,以提高SVM分类器的性能。通过创建Pipeline结合StandardScaler预处理和SVC分类器,对多项式核函数的参数进行网格搜索。最终找到最佳参数组合:degree为10,coef0为1,C为10。
摘要由CSDN通过智能技术生成

网格搜索用来寻找模型最佳的超参数

环境
  • python==3.7
  • scikit-learn==0.24.1

使用方式
import numpy as np
from sklearn import datasets
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

iris = datasets.load_iris()

X = iris["data"][:, (2, 3)]
y = (iris["target"] == 2).astype(np.float64)

poly_svm = Pipeline([
    ("preprocess", StandardScaler()),
    ("svm_clf", SVC(kernel="poly"))
])


para = {"svm_clf__degree": [3, 10],
        "svm_clf__coef0": [1],
        "svm_clf__C": [5, 10]}
gs = GridSearchCV(poly_svm, para, cv=5)
gs.fit(X, y)
print(gs.best_params_)

输出为: {‘svm_clf__C’: 10, ‘svm_clf__coef0’: 1, ‘svm_clf__degree’: 10}

这里用到了Pipeline, 对模型网格搜索时,需要在超参数名前面加模型名字+两个下划线(svm_clf__)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuhengshi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值