sklearn中支持向量机(SVM)用于回归

摘要:本文使用SVM进行回归;

00 获取sklearn中糖尿病患者数据

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, svm
diabetes=datasets.load_diabetes()
train_x=diabetes.data[:342,:]
train_y=diabetes.target[:342]
test_x=diabetes.data[342:,:]
test_y=diabetes.target[342:]


01 SVM线性回归

regre=svm.LinearSVR()
regre.fit(train_x,train_y)
regre.score(test_x,test_y)
Out[36]: -0.3826827941256945

regre.coef_
Out[37]:
array([ 2.49261883,  0.25752362,  6.21448813,  4.90032452,  2.70896776,
        2.61597544, -4.9194174 ,  5.41655006,  6.48684744,  3.7077186 ])regre.intercept_
Out[38]: array([103.12986397])

regre.n_iter_
Out[39]: 9

考察参数loss的影响:

 regre=svm.LinearSVR(loss='epsilon_insensitive')
regre.fit(train_x,train_y)
regre.score(test_x,test_y)
Out[40]: -0.38589592979512455

regre=svm.LinearSVR(loss='squared_epsilon_insensitive')
regre.fit(train_x,train_y)
regre.score(test_x,test_y)
Out[41]: 0.5006695289879384

考察参数epsilon的影响:

sklearn中支持向量机(SVM)用于回归的图1考察参数C的影响:

cs=np.logspace(-1,2)
scor=[]
for C in cs:
    regre=svm.LinearSVR(epsilon=0.01,loss='squared_epsilon_insensitive',C=C,max_iter=10000)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(cs,scor)
plt.xscale('log')

sklearn中支持向量机(SVM)用于回归的图2


02 SVM非线性回归

当kernel=‘linear‘’’时:

regre=svm.SVR(kernel='linear')
regre.fit(train_x,train_y)
regre.score(test_x,test_y)
Out[44]: 0.001143175015592801

当kernel=‘‘poly’’时,考察参数 degree,gamma,C的影响:

degrees=range(1,20)
scor=[]
for i in degrees:   
    regre=svm.SVR(kernel='poly',degree=i,gamma='auto',coef0=1)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(degrees,scor)
plt.ylim(-1,1)
plt.show()

sklearn中支持向量机(SVM)用于回归的图3

gammas=range(1,40)
scor=[]
for i in gammas:
    regre=svm.SVR(kernel='poly',degree=3,gamma=i,coef0=1)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(gammas,scor)
plt.ylim(0,1)
plt.show()

sklearn中支持向量机(SVM)用于回归的图4coef0s=range(0,20)
scor=[]
for i in coef0s:
    regre=svm.SVR(kernel='poly',degree=3,gamma=20,coef0=i)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(coef0s,scor)
plt.ylim(0,1)  
plt.show()

sklearn中支持向量机(SVM)用于回归的图5当kernel=‘‘rbf’’时,参数gamma的影响:

gammas=range(1,20)
scor=[]
for i in gammas:
    regre=svm.SVR(kernel='rbf',gamma=i)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(gammas,scor)
plt.ylim(-1,1)  
plt.show()

sklearn中支持向量机(SVM)用于回归的图6当kernel=‘sigmoid’时,考察gamma,coef0的影响:

gammas=np.logspace(-1,3,20)
scor=[]
for i in gammas:
    regre=svm.SVR(kernel='sigmoid',gamma=i,coef0=0.01)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(gammas,scor)
plt.ylim(-1,1)  
plt.xscale('log')
plt.show()

sklearn中支持向量机(SVM)用于回归的图7coef0s=np.linspace(0,5,20)
scor=[]
for i in coef0s:
    regre=svm.SVR(kernel='sigmoid',gamma=10,coef0=i)
    regre.fit(train_x,train_y)
    scor.append(regre.score(test_x,test_y))
plt.plot(coef0s,scor)
plt.ylim(-1,1)
plt.show()

sklearn中支持向量机(SVM)用于回归的图8

默认 最新
当前暂无评论,小编等你评论哦!
点赞 评论 收藏
关注