前言

到寒假終於有時間來整理上學期ML中的一些內容了。這篇關於LIBSVM的一些簡單使用,是之前作業中所用到的部分。


LIBSVM介紹

LIBSVM is an integrated software for support vector classification, (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR) and distribution estimation (one-class SVM). It supports multi-class classification.

MORE INFO


Python下安裝

環境:

1
2
3
LIBLINEAR Version: 2.43
Python Version: Anaconda with pyhton 3.8
IDE: Pycharm

安裝命令:

1
> pip install -U libsvm-official


Data格式

data的形式與LIBLINEAR中相同,形如: Label feature_index_1:feature_value_1 feature_index_2:feature_value_2 ... feature_index_N:feature_value_N 例如(套件自帶範例heart_scale):

1
2
3
4
5
6
7
8
9
10
+1 1:0.708333 2:1 3:1 4:-0.320755 5:-0.105023 6:-1 7:1 8:-0.419847 9:-1 10:-0.225806 12:1 13:-1 
-1 1:0.583333 2:-1 3:0.333333 4:-0.603774 5:1 6:-1 7:1 8:0.358779 9:-1 10:-0.483871 12:-1 13:1
+1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1
-1 1:0.458333 2:1 3:1 4:-0.358491 5:-0.374429 6:-1 7:-1 8:-0.480916 9:1 10:-0.935484 12:-0.333333 13:1
-1 1:0.875 2:-1 3:-0.333333 4:-0.509434 5:-0.347032 6:-1 7:1 8:-0.236641 9:1 10:-0.935484 11:-1 12:-0.333333 13:-1
-1 1:0.5 2:1 3:1 4:-0.509434 5:-0.767123 6:-1 7:-1 8:0.0534351 9:-1 10:-0.870968 11:-1 12:-1 13:1
+1 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5
+1 1:0.25 2:1 3:1 4:-0.698113 5:-0.484018 6:-1 7:1 8:0.0839695 9:1 10:-0.612903 12:-0.333333 13:1
+1 1:0.291667 2:1 3:1 4:-0.132075 5:-0.237443 6:-1 7:1 8:0.51145 9:-1 10:-0.612903 12:0.333333 13:1
+1 1:0.416667 2:-1 3:1 4:0.0566038 5:0.283105 6:-1 7:1 8:0.267176 9:-1 10:0.290323 12:1 13:1


常用函數

  • svm_read_problem

  • svm_train

  • svm_predict


簡單例子

比較其他參數相同下,不同的影響:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
y_tr_15, x_tr_15 = svm_read_problem('./satimage_train', return_scipy=1)
y_te_15, x_te_15 = svm_read_problem('./satimage_test', return_scipy=1)
for i in range(4435):
if y_tr_15[i] != 1:
y_tr_15[i] = -1

for j in range(len(y_te_15)):
if y_te_15[j] != 1:
y_te_15[j] = -1


model15_1 = svm_train(y_tr_15, x_tr_15, '-s 0 -c 0.1 -t 2 -g 0.1 -q')
svm_predict(y_te_15, x_te_15, model15_1)

model15_2 = svm_train(y_tr_15, x_tr_15, '-s 0 -c 0.1 -t 2 -g 1 -q')
svm_predict(y_te_15, x_te_15, model15_2)

model15_3 = svm_train(y_tr_15, x_tr_15, '-s 0 -c 0.1 -t 2 -g 10 -q')
svm_predict(y_te_15, x_te_15, model15_3)

model15_4 = svm_train(y_tr_15, x_tr_15, '-s 0 -c 0.1 -t 2 -g 100 -q')
svm_predict(y_te_15, x_te_15, model15_4)

model15_5 = svm_train(y_tr_15, x_tr_15, '-s 0 -c 0.1 -t 2 -g 1000 -q')
svm_predict(y_te_15, x_te_15, model15_5)


參考