1.SVM訓練
使用前請按照python
改變核函數需要在svm-train.exe執行命令行加
"-s svm_type : set type of SVM (default 0)\n"
" 0 -- C-SVC (multi-class classification)\n"
" 1 -- nu-SVC (multi-class classification)\n"
" 2 -- one-class SVM\n"
" 3 -- epsilon-SVR (regression)\n"
" 4 -- nu-SVR (regression)\n"
"-t kernel_type : set type of kernel function (default 2)\n"
" 0 -- linear: u'*v\n"
" 1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
" 2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
" 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
" 4 -- precomputed kernel (kernel values in training_set_file)\n"
"-d degree : set degree in kernel function (default 3)\n"
"-g gamma : set gamma in kernel function (default 1/num_features)\n"
"-r coef0 : set coef0 in kernel function (default 0)\n"
"-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
"-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
"-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
"-m cachesize : set cache memory size in MB (default 100)\n"
"-e epsilon : set tolerance of termination criterion (default 0.001)\n"
"-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
"-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
"-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
"-v n: n-fold cross validation mode\n"
"-q : quiet mode (no outputs)\n"
第一步:打開easy.py,修改# example forwindows下的幾個路徑,同時修改grid.py路徑。
修改cmd = '{0} {1} -svmtrain "{2}" -gnuplot "{3}" "{4}"'.format("D:\Anaconda2\python.exe",grid_py, svmtrain_exe, gnuplot_exe, scaled_file)
添加Python路徑
第二步:運行cmd,進入控制檯環境,定位到放置easy.py的目錄d:/libsvm/program/tools。
第三步:輸入命令:
d:/libsvm/python26/python easy.py heart_scale heart_test
heart_scale 是訓練樣本文件
heart_test 是測試樣本文件
訓練完成後得到模型文件
-
訓練文件製作
提取舌面分析算法,刪除SVM預測部分svm.predict。將算法生產的特徵向量輸出到文件
如下所示
1:0.002 2:0.033:0.448
在特徵向量前增加分類標示
1 1:0 2:0
1 1:3 2:4
1 1:5 2:9
1 1:12 2:1
1 1:8 2:7
0 1:9 2:8
0 1:6 2:12
0 1:10 2:8
0 1:8 2:5
0 1:14 2:8
增加住夠多的特徵向量即可用於訓練。注意各分類的樣本數量應該一致。
-
模型文件使用
SVM訓練結束後會生成range文件和model文件
Range文件如下:
x
0.000000000000000 1.000000000000000
1 0.000000000000000 14.00000000000000
2 0.000000000000000 12.00000000000000
model文件內容如下:
svm_type c_svc
kernel_type rbf
gamma 0.5
nr_class 2
total_sv 9
rho -0.5061570424019811
label 1 0
nr_sv 4 5
SV
2.7686973549711875 1:0.214285714285714272:0.3333333333333333
5.0 1:0.35714285714285715 2:0.75
5.0 1:0.8571428571428571 2:0.08333333333333333
5.0 1:0.5714285714285714 2:0.5833333333333334
-5.0 1:0.6428571428571429 2:0.6666666666666666
-2.4351637665059895 1:0.42857142857142855 2:1.0
-5.0 1:0.7142857142857143 2:0.6666666666666666
-5.0 1:0.5714285714285714 2:0.4166666666666667
-0.3335335884651968 1:1.0 2:0.6666666666666666
將兩個文件合併成
x
0.000000000000000 1.000000000000000
1 0.000000000000000 14.00000000000000
2 0.000000000000000 12.00000000000000
svm_type c_svc
kernel_type rbf
gamma 0.5
nr_class 2
total_sv 9
rho -0.5061570424019811
label 1 0
nr_sv 4 5
SV
2.7686973549711875 1:0.214285714285714272:0.3333333333333333
5.0 1:0.35714285714285715 2:0.75
5.0 1:0.8571428571428571 2:0.08333333333333333
5.0 1:0.5714285714285714 2:0.5833333333333334
-5.0 1:0.6428571428571429 2:0.6666666666666666
-2.4351637665059895 1:0.42857142857142855 2:1.0
-5.0 1:0.7142857142857143 2:0.6666666666666666
-5.0 1:0.5714285714285714 2:0.4166666666666667
-0.3335335884651968 1:1.0 2:0.6666666666666666
即可成爲用於分析的模型文件
文件內容說明
svm_type c_svc % 訓練所採用的 svm 類型,此處爲 C- SVC
kernel_type rbf % 訓練採用的核函數類型,此處爲 RBF 核
gamma 0.047619 % 與操作參數設置中的 g 含義相同
nr_class 2 % 分類時的類別數,此處爲兩分類問題
total_sv 604 % 總共的支持向量個數
rho -0.337784 % 決策函數中的常數項 b
label 0 1 % 類別標籤
nr_sv 314 290 % 各類別標籤對應的支持向量個數
SV % 以下爲支持向量
1 1:-0.963808 2:0.906788 ... 19:-0.197706 20:-0.928853 21:-1
1 1:-0.885128 2:0.768219 ... 19:-0.452573 20:-0.980591 21:-1
... ... ...
1 1:-0.847359 2:0.485921 ... 19:-0.541457 20:-0.989077 21:-1
% 對於分類問題,上面的支持向量的各列含義與訓練數據集相同;對於迴歸問題,略有不同,與訓練數據中的標籤 label (即 y 值)所對應的位置在模型文件的支持向量中現在存放的是 Lagrange 係數 a 值,即爲下面決策函數公式中的 a 值:
* *
1
() ( )( ( ) ( )) ( ) ( , )
( , )
k
i i i i i i
i i sv
i i
i sv
fx a a x x b a a k x x b
ak x x b
=
= - F F + = - +
= +
g