在matlab下測試libsvm工具箱

一、libsvm工具箱的簡介

SVM由Vapnik首先提出(Boser,Guyon and Vapnik,1992;Cortes and Vapnik,1995;Vapnik,1995,1998)。
SVM的主要思想是建立一個超平面作爲決策曲面,使得正例和反例之間的隔離邊緣被最大化。
SVM的優點:
1、通用性(能夠在各種函數集中構造函數)
2、魯棒性(不需要微調)
3、有效性(在解決實際問題中屬於最好的方法之一)
4、計算簡單(方法的實現只需要利用簡單的優化技術)
5、理論上完善(基於VC推廣理論的框架)
SVM工具箱:種類很多,公認的最好用的是libsvm by 林智仁[臺大] (http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html)

二、Libsvm-matlab工具箱的安裝

1、給matlab指定編譯器mex –setup %let you select or change the compiler configuration
這裏寫圖片描述

2、make將c寫的文件轉換成matlab相應的接口
make的結果
這裏寫圖片描述
目的:將libsvm-3.11\matlab 中 libsvmwrite.c 等 C++文件編譯成 libsvmread.mexw32 等matlab文件,這樣就可以在command window中被直接調用了。

3、注意事項
(1)、安裝時需要把libsvm-mat作爲當前的工作目錄,在後面使用時並將其加爲工作目錄(setpath…)。
這裏寫圖片描述
這裏寫圖片描述
(2)、電腦裏需要有相應的編譯器,可以用自帶的,最好裝一個VC。
4、svmtrain函數相關參數說明
%通過訓練集來訓練模型
svmtrain( … );

model =
svmtrain(train_label, train_matrix, [‘libsvm_options’]);

-train_label:
An m by 1 vector of training labels (type must be double).
-train_matrix:
An m by n matrix of m training instances with n features.
It can be dense or sparse (type must be double).
-libsvm_options:
A string of training options in the same format as that of LIBSVM.

-libsvm_options:
A string of training options in the same format as that of LIBSVM.
有關libsvm的參數選項:
Options:可用的選項即表示的涵義如下
  -s svm類型:SVM設置類型(默認0)
  0 – C-SVC
  1 –v-SVC
  2 –一類SVM
  3 – e -SVR
  4 – v-SVR
  -t 核函數類型:核函數設置類型(默認2)
  0 – 線性:u’v
  1 – 多項式:(r*u’v + coef0)^degree
  2 – RBF函數:exp(-r|u-v|^2)
  3 –sigmoid:tanh(r*u’v + coef0)
  -d degree:核函數中的degree設置(針對多項式核函數)(默認3)
  -g r(gama):核函數中的gamma函數設置(針對多項式/rbf/sigmoid核函數)(默認1/ k)
  -r coef0:核函數中的coef0設置(針對多項式/sigmoid核函數)((默認0)
  -c cost:設置C-SVC,e -SVR和v-SVR的參數(損失函數)(默認1)
  -n nu:設置v-SVC,一類SVM和v- SVR的參數(默認0.5)
  -p p:設置e -SVR 中損失函數p的值(默認0.1)
  -m cachesize:設置cache內存大小,以MB爲單位(默認40)
  -e eps:設置允許的終止判據(默認0.001)
  -h shrinking:是否使用啓發式,0或1(默認1)
  -wi weight:設置第幾類的參數C爲weight?C(C-SVC中的C)(默認1)
  -v n: n-fold交互檢驗模式,n爲fold的個數,必須大於等於2
其中-g選項中的k是指輸入數據中的屬性數。option -v 隨機地將數據剖分爲n部分並計算交互檢驗準確度和均方根誤差。以上這些參數設置可以按照SVM的類型和核函數所支持的參數進行任意組合,如果設置的參數在函數或SVM類型中沒有也不會產生影響,程序不會接受該參數;如果應有的參數設置不正確,參數將採用默認值。
The ‘svmtrain’ function returns a model which can be used for future prediction.
5、svmpredict函數相關參數說明
%對測試集進行預測
svmpredict( … );

[predicted_label, accuracy, decision_values/prob_estimates]
= svmpredict(test_label, test_matrix, model, [‘libsvm_options’]);

-test_label:
An m by 1 vector of prediction labels. If labels of test
data are unknown, simply use any random values. (type must be double)
-testmatrix:
An m by n matrix of m testing instances with n features.
It can be dense or sparse. (type must be double)
-model:
The output of svmtrain.
-libsvm_options:
A string of testing options in the same format as that of LIBSVM.
decision_values/prob_estimates:迴歸相關問題中才會涉及到

三、試驗結果

[heart_scale_label,heart_scale_inst]=libsvmread(‘heart_scale’);
model = svmtrain(heart_scale_label,heart_scale_inst, ‘-c 1 -g 0.07’);
[predict_label, accuracy, dec_values] =svmpredict(heart_scale_label, heart_scale_inst, model); % test the trainingdata
如果出現一行:Accuracy = 86.6667% (234/270) (classification)。就說明成功了。就可以在matlab中使用svm了。

作者:Jacky_Ponder,轉載或分享請註明出處。QQ:2814152689

發佈了31 篇原創文章 · 獲贊 18 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章