本站內容為本人所有,禁止未經同意恣意未具名轉載或修改原作內容轉發!

2011年5月23日 星期一

[筆記] LibSVM for matlab 安裝教學&使用方法

最近因課程需要,

所以必須學習這項東西,

自己看了一下~@@

沒想到這個東西這麼方便的說XD…

總之廢話不多說,

開始教學囉!

首先請準備好以下的軟體或library:

1.Visual studio 2010(Express or Pro都可)

2.Matlab 2010b(官網連結)

3.LibSVM Library(請至官網下載)

image

之後請遵循以下步驟進行操作:

Step1:安裝Visual studio 2010

Step2:安裝Matlab 2010b

從Step3開始就是重頭戲囉!!請大家注意囉!!!XD

Step3:開啟Matlab 2010b

Step4:輸入"mex –setup"指令,並根據以下截圖依序操作,以進行c++ compiler的導入

image

Step5:將LibSVM的壓縮包解開,並利用Matlab左側"目前目錄"移至LibSVM目錄底下,如下圖所示:

image

Step6:為了方便起見,請將libsvm根目錄底下的heart_scale(等會測試用)、svm.cpp、svm.def、svm.h、svm.obj、svm-predict.c、svm-scale.c與svm-train.c複製到其內之matlab目錄底下,如下圖所示:

image

Step7:輸入"Make",等待SVM complie完成

image

做完以上步驟我們的SVM tool環境建構就算是大功告成拉!!

接下來就來示範一下LibSVM內附的心臟病dataset範例啦!

Step8:輸入"[heart_scale_label, heart_scale_inst] = libsvmread('heart_scale');",將心臟病資料load進matlab

補充:heart_scale資料結構其實非常簡單,左邊第一個值就是所謂的desired output,右邊的x:都代表這個sample的features,而在下面的例子中,總共有13個feature,其Desired output為:+1:

+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

Step9:輸入"model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07');",開始進行資料訓練

(ps. c代表懲罰成本 g代表γ常數值的設定 剩下的參數請參考下列資料(資料來源:LibSVM官網):

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)



)

 



 



 



 



 



Step10:輸入"[predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model);",測試分類結果,跑出分類正確率



以下就是跑出來的結果:



image



是不是很簡單容易呢!!!XD



相信對一些須使用SVM進行相關研究的人一定有幫助的!!



Good luck!微笑

4 則留言:
Write 意見
  1. Naught... There ain't no such thing as a free lunch. HAVING sent you testimonial for my share whether you accept or not.

    回覆刪除
  2. B.T.W. I'd be grateful to you if you don't mind writing another tutorial of svmlight, really. Again, thanks in advance!

    回覆刪除
  3. 感謝大大分享,很有幫助!
    使用過程有一些狀況排除,和大家分享 @R2015a

    1. 在libsvm/matlab 中執行make 若輸入Make會被系統導引輸入mae無助於事

    2. 在執行 svmtrain可能會遇到
    Error using svmtrain (line 234)
    Y must be a vector or a character array.
    此時請在上方功能列中的 Environment/set path中點入
    選擇 add with subfolders... 將 libsvm資料夾加入儲存~

    回覆刪除
    回覆
    1. 請問你有使用成功嗎
      我在mex安裝遇到很多問題

      刪除