支持向量机——线性分类SVM

本文详细介绍了sklearn.svm.LinearSVC类的使用方法及其参数设置,包括不同损失函数、罚项形式和罚项系数C的影响,并通过实例展示了如何加载数据、训练模型及评估模型效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

模型原型
sklearn.svm.LinearSVC(penalty=’l2’,loss=’squared_hinge’,dual=True,tol=0.0001,C=1.0,multi_class=’ovr’, fit_intercept=True,intercept_scaling=1,class_weight=None,verbose=0,random_state=None,max_iter=1000)
参数

  • penalty:罚项的范数
  • loss:损失函数
    • ’hinge’:合页损失函数(标准SVM的损失函数)
    • ‘squared_hinge’:合页损失函数的平方
  • dual:
    • True:解决对偶问题
    • False:解决原始问题
  • tol
  • C:罚项参数
  • multi_class:指定多类分类问题的策略
    • ’ovr’:采用one-vs-rest分类策略
    • ‘crammer_singer’:多类联合分类,很少用(计算量大,且精度不会更佳)
  • fit_intercept:是否计算截距(常数项)
  • intercept_scaling:一个人工特征,该特征对所有实例都是常数值(人工特征也参与了罚项的计算)
  • class_weight
  • verbose:表示是否开启verbose输出
  • random_state
  • max_iter

属性

  • coef_
  • intercept_

方法

  • fit(X,y)
  • predict(X)
  • score(X,y[,sample_weight])
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets,linear_model,cross_validation,svm

加载数据

def load_data_classfication():
    iris=datasets.load_iris()
    X_train=iris.data
    y_train=iris.target
    return cross_validation.train_test_split(X_train,y_train,test_size=0.25,random_state=0,stratify=y_train)

使用LinearSVC类

def test_LinearSVC(*data):
    X_train,X_test,y_train,y_test=data
    cls=svm.LinearSVC()
    cls.fit(X_train,y_train)
    print('Coefficients:%s,intercept %s'%(cls.coef_,cls.intercept_))
    print('Score:%.2f'%cls.score(X_test,y_test))

X_train,X_test,y_train,y_test=load_data_classfication()
test_LinearSVC(X_train,X_test,y_train,y_test)

损失函数的影响

def test_LinearSVC_loss(*data):
    X_train,X_test,y_train,y_test=data
    losses=['hinge','squared_hinge']
    for loss in losses:
        cls=svm.LinearSVC(loss=loss)
        cls.fit(X_train,y_train)
        print('Loss:%s'%loss)
        print('Coefficients:%s,intercept %s'%(cls.coef_,cls.intercept_))
        print('Score:%.2f'%cls.score(X_test,y_test))

test_LinearSVC_loss(X_train,X_test,y_train,y_test)

罚项形式的影响

def test_LinearSVC_L12(*data):
    X_train,X_test,y_train,y_test=data
    L12=['l1','l2']
    for p in L12:
        cls=svm.LinearSVC(penalty=p,dual=False)
        cls.fit(X_train,y_train)
        print('penalty:%s'%p)
        print('Coefficients:%s,intercept %s'%(cls.coef_,cls.intercept_))
        print('Score:%.2f'%cls.score(X_test,y_test))

test_LinearSVC_L12(X_train,X_test,y_train,y_test)

罚项系数C的影响

def test_LinearSVC_C(*data):
    X_train,X_test,y_train,y_test=data
    Cs=np.logspace(-2,1)
    train_scores=[]
    test_scores=[]
    for C in Cs:
        cls=svm.LinearSVC(C=C)
        cls.fit(X_train,y_train)
        train_scores.append(cls.score(X_train,y_train))
        test_scores.append(cls.score(X_test,y_test))

    fig=plt.figure()
    ax=fig.add_subplot(1,1,1)
    ax.plot(Cs,train_scores,label='Training score')
    ax.plot(Cs,test_scores,label='Testing score')
    ax.set_xlabel(r'C')
    ax.set_ylabel(r'score')
    ax.set_xscale('log')
    ax.set_title('LinearSVC')
    ax.legend(loc='best')
    plt.show()

test_LinearSVC_C(X_train,X_test,y_train,y_test)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值