支持向量机——线性回归SVR

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model,cross_validation,svm

加载数据

def load_data_regression():
    diabetes=datasets.load_diabetes()
    return cross_validation.train_test_split(diabetes.data,
        diabetes.target,test_size=0.25,random_state=0)

使用LinearSVR类

def test_LinearSVR(*data):
    X_train,X_test,y_train,y_test=data
    regr=svm.LinearSVR()
    regr.fit(X_train,y_train)
    print('Coefficients:%s,\nintercept %s'%(regr.coef_,regr.intercept_))
    print('Score:%.2f'%regr.score(X_test,y_test))

X_train,X_test,y_train,y_test=load_data_regression()
test_LinearSVR(X_train,X_test,y_train,y_test)

损失函数类型的影响

def test_LinearSVR_loss(*data):
    X_train,X_test,y_train,y_test=data
    losses=['epsilon_insensitive','squared_epsilon_insensitive']
    for loss in losses:
        regr=svm.LinearSVR(loss=loss)
        regr.fit(X_train,y_train)
        print('loss:%s'%loss)
        print('Coefficients:%s,\n
            intercept %s'%(regr.coef_,regr.intercept_))
        print('Score:%.2f'%regr.score(X_test,y_test))

test_LinearSVR_loss(X_train,X_test,y_train,y_test)

ε的影响

def test_LinearSVR_epsilon(*data):
    X_train,X_test,y_train,y_test=data
    epsilons=np.logspace(-2,2)
    train_scores=[]
    test_scores=[]
    for epsilon in epsilons:
        regr=svm.LinearSVR(epsilon=epsilon,
            loss='squared_epsilon_insensitive')
        regr.fit(X_train,y_train)
        train_scores.append(regr.score(X_train,y_train))
        test_scores.append(regr.score(X_test,y_test))
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    ax.plot(epsilons,train_scores,label='Training score',marker='+')
    ax.plot(epsilons,test_scores,label='Testing score',marker='o')
    ax.set_title('LinearSVR_epsilon')
    ax.set_xscale('log')
    ax.set_xlabel(r'$\epsilon$')
    ax.set_ylabel('score')
    ax.set_ylim(-1,1.05)
    ax.legend(loc='best',framealpha=0.5)
    plt.show()

test_LinearSVR_epsilon(X_train,X_test,y_train,y_test)

罚项系数C的影响

def test_LinearSVR_C(*data):
    X_train,X_test,y_train,y_test=data
    Cs=np.logspace(-1,2)
    train_scores=[]
    test_scores=[]
    for C in Cs:
        regr=svm.LinearSVR(epsilon=0.1,
            loss='squared_epsilon_insensitive',C=C)
        regr.fit(X_train,y_train)
        train_scores.append(regr.score(X_train,y_train))
        test_scores.append(regr.score(X_test,y_test))
    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    ax.plot(Cs,train_scores,label='Training score',marker='+')
    ax.plot(Cs,test_scores,label='Testing score',marker='o')
    ax.set_title('LinearSVR_C')
    ax.set_xscale('log')
    ax.set_xlabel(r'C')
    ax.set_ylabel('score')
    ax.set_ylim(-1,1.05)
    ax.legend(loc='best',framealpha=0.5)
    plt.show()

test_LinearSVR_C(X_train,X_test,y_train,y_test)
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值