参考资料:
LibSVM: https://www.csie.ntu.edu.tw/~cjlin/libsvm/
github: https://github.com/cjlin1/libsvm
尝试直接用pip install LibSVM失败…
参考大神的链接可行: https://blog.csdn.net/he99774/article/details/80388612
参考大神的链接可行: 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
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: (gammau’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 weightC, for C-SVC (default 1)
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)