SVM successful one

import numpy as np
from sklearn.svm import SVR
from  sklearn.model_selection import GridSearchCV

import matplotlib.pyplot as plt

if __name__ == "__main__":
    N = 50
    np.random.seed(0)
    x = np.sort(np.random.uniform(0, 6, N), axis=0)
    y = 2 * np.cos(x) + 0.1 * np.random.randn(N)
    x = x.reshape(-1, 1)

    model = SVR(kernel='rbf')
    c = np.logspace(-2, 2, 10)
    gamma = np.logspace(-2, 2, 10)
    svr = GridSearchCV(model, param_grid={'C': c, 'gamma': gamma}, cv=5)
    svr.fit(x, y)
    print('C和gamma的最优参数:\n', svr.best_params_)

    x_test = np.linspace(x.min(), x.max(), 100).reshape(-1, 1)
    y_predict = svr.predict(x_test)

    sp = svr.best_estimator_.support_
    plt.figure(facecolor='w')
    plt.scatter(x[sp], y[sp], c='b', marker='o', label='Support Vectors')
    plt.plot(x_test, y_predict, 'r--', label='RBF Kernel')
    plt.plot(x, y, 'g-')
    plt.legend(loc='best')
    plt.title('SVR with grid_search C & gamma')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.grid(True)
    plt.tight_layout()
    plt.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yyd__

你的打赏和鼓励我会珍惜!

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

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

打赏作者

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

抵扣说明:

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

余额充值