SVM---sklearn实现多核

import numpy as np
from sklearn.svm import SVR
import matplotlib.pyplot as plt
%# % matplotlib inline(再jupyter中实现可加上)
#自定义样本点rand,并且生成sin值
x = np.random.rand(40,1)*5
X_train = np.sort(x,axis = 0)
print(“X_train:%s”%X_train)

y_train = np.sin(X_train)
print(“y_train:%s”%y_train)

y_train[::5]+= np.random.randn(8,1)
print(“y_train:%s”%y_train)
plt.scatter(X_train,y_train)
plt.ylim(-2.5,2.5)

svr_linear = SVR(kernel=‘linear’)
svr_rbf = SVR(kernel=‘rbf’)#默认设置
svr_poly = SVR(kernel=‘poly’)

#数据的预测
%# x_test= np.arange(0,5,0.01).reshape((-1,1))
x_test= np.arange(0,5,0.01)[:,np.newaxis]
%# print(“X_train.shape:%s”%X_train.shape[1])
%# print(“y_train.shape:%s”%y_train.shape[1])
print(“x_test.shape:%s”%x_test.shape[0])
linear_y_ = svr_linear.fit(X_train,y_train).predict(x_test)
rbf_y_ = svr_rbf.fit(X_train,y_train).predict(x_test)
poly_y = svr_poly.fit(X_train,y_train).predict(x_test)

#绘制图形,观察三种支持向量机内核不同
plt.figure(figsize=(8,6))
plt.scatter(X_train,y_train)
plt.plot(x_test,linear_y_, ‘r+’,label=“linear”)
plt.plot(x_test,rbf_y_,‘cyan’,label = ‘rbf’)
plt.plot(x_test,poly_y,‘purple’,label=‘poly’)

plt.legend()

plt.show()

注:线性的linear可以画出分割线,但是高维的就没法画出分割面,所以一般SVM预测都是可以画出预测的线,分割线无法画出。那如何才能知道哪个核函数最好,这就需要测试之后才能知道。哪个测试出来的准确率高就用哪个
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值