【机器学习】西瓜书_周志华,习题集6.2,使用LIBSVM在西瓜数据集3.0a上分别用线性核和高斯核训练一个SVM,并比较其支持向量的差别。

【机器学习】西瓜书_周志华,习题集6.2,使用LIBSVM在西瓜数据集3.0a上分别用线性核和高斯核训练一个SVM,并比较其支持向量的差别。

参考资料:

LibSVM: https://www.csie.ntu.edu.tw/~cjlin/libsvm/

github: https://github.com/cjlin1/libsvm

1. 环境:Windows10 + python3.5.4
2. 安装LibSVM库:

尝试直接用pip install LibSVM失败…

参考大神的链接可行: https://blog.csdn.net/he99774/article/details/80388612

3.数据处理:

参考大神的链接可行: https://blog.csdn.net/kobesdu/article/details/8944851

这里我将西瓜书上的数据集3.0a分成了train.txt和test.txt,不同的分法导致最后的预测结果不同,也说明了数据选择的重要性。。

train.txt

1 1:.774 2:.376
1 1:.608 2:.318
1 1:.403 2:.237
1 1:.437 2:.211
-1 1:.243 2:.267
-1 1:.343 2:.099
-1 1:.657 2:.198
-1 1:.593 2:.042

test.txt

1 1:.697 2:.46
1 1:.634 2:.264
1 1:.556 2:.215
1 1:.481 2:.149
-1 1:.666 2:.091
-1 1:.245 2:.057
-1 1:.639 2:.161
-1 1:.36 2:.37
-1 1:.719 2:.103

4.代码实现:
from svmutil import *

train_name = 'train.txt'
test_name = 'test.txt'
linear_name = 'linear.model'
gauss_name = 'gauss.model'

y,x = svm_read_problem(train_name)
y1,x1 = svm_read_problem(test_name)

model_1 = svm_train(y,x,'-t 0')  ### linear
model_2 = svm_train(y,x,'-t 2')   ### radial basis function

svm_save_model(linear_name,model_1)
svm_save_model(gauss_name,model_2)

p1_label, p1_acc, p1_val = svm_predict(y1,x1,model_1)
p2_label, p2_acc, p2_val = svm_predict(y1,x1,model_2)

说明一下,svm_train的参数详见文章开头的参考链接。
options:
-s svm_type : set type of SVM (default 0)
0 – C-SVC
1 – nu-SVC
2 – one-class SVM
3 – epsilon-SVR
4 – nu-SVR
-t kernel_type : set type of kernel function (default 2)
0 – linear: u’v
1 – polynomial: (gamma
u’v + coef0)^degree
2 – radial basis function: exp(-gamma
|u-v|^2)
3 – sigmoid: tanh(gamma*u’v + coef0)
-d degree : set degree in kernel function (default 3)
-g gamma : set gamma in kernel function (default 1/num_features)
-r coef0 : set coef0 in kernel function (default 0)
-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)
-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)
-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)
-m cachesize : set cache memory size in MB (default 100)
-e epsilon : set tolerance of termination criterion (default 0.001)
-h shrinking: whether to use the shrinking heuristics, 0 or 1 (default 1)
-b probability_estimates: whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)
-wi weight: set the parameter C of class i to weight
C, for C-SVC (default 1)

5.实验结果

optimization finished, #iter = 4
nu = 1.000000
obj = -7.781854, rho = 0.342881
nSV = 8, nBSV = 8
Total nSV = 8
*
optimization finished, #iter = 4
nu = 1.000000
obj = -7.794470, rho = 0.047321
nSV = 8, nBSV = 8
Total nSV = 8
Accuracy = 77.7778% (7/9) (classification)
Accuracy = 77.7778% (7/9) (classification)

  • 10
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值