SVM工具箱快速入手簡易教程(by faruto)
一. matlab 內建的函數(matlab協助檔案裡的例子)[只有較新版本的matlab中有這兩個SVM的函數]
=====
svmtrain svmclassify
=====簡要文法規則====
svmtrain
Train support vector machine classifier
Syntax
SVMStruct = svmtrain(Training, Group)
SVMStruct = svmtrain(..., 'Kernel_Function', Kernel_FunctionValue, ...)
SVMStruct = svmtrain(..., 'RBF_Sigma', RBFSigmaValue, ...)
SVMStruct = svmtrain(..., 'Polyorder', PolyorderValue, ...)
SVMStruct = svmtrain(..., 'Mlp_Params', Mlp_ParamsValue, ...)
SVMStruct = svmtrain(..., 'Method', MethodValue, ...)
SVMStruct = svmtrain(..., 'QuadProg_Opts', QuadProg_OptsValue, ...)
SVMStruct = svmtrain(..., 'SMO_Opts', SMO_OptsValue, ...)
SVMStruct = svmtrain(..., 'BoxConstraint', BoxConstraintValue, ...)
SVMStruct = svmtrain(..., 'Autoscale', AutoscaleValue, ...)
SVMStruct = svmtrain(..., 'Showplot', ShowplotValue, ...)
---------------------
svmclassify
Classify data using support vector machine
Syntax
Group = svmclassify(SVMStruct, Sample)
Group = svmclassify(SVMStruct, Sample, 'Showplot', ShowplotValue)
============================執行個體研究====================
load fisheriris
%載入matlab內建的資料[有關資料的資訊可以自己到UCI尋找,這是UCI的經典資料之一],得到的資料如下圖:
tu1
1.jpg (7.94 KB)
2009-5-12 19:50
其中meas是150*4的矩陣代表著有150個樣本每個樣本有4個屬性描述,species代表著這150個樣本的分類.
data = [meas(:,1), meas(:,2)];
%在這裡只取meas的第一列和第二列,即只選取前兩個屬性.
groups = ismember(species,'setosa');
%由於species分類中是有三個分類:setosa,versicolor,virginica,為了使問題簡單,我們將其變為二分類問題:Setosa and non-Setosa.
[train, test] = crossvalind('holdOut',groups);
cp = classperf(groups);
%隨機播放訓練集合測試集[有關crossvalind的使用請自己help一下]
其中cp作用是後來用來評價分類器的.
svmStruct = svmtrain(data(train,:),groups(train),'showplot',true);
%使用svmtrain進行訓練,得到訓練後的結構svmStruct,在預測時使用.
訓練結果如圖:
tu2
2.jpg (26.86 KB)
2009-5-12 19:50
classes = svmclassify(svmStruct,data(test,:),'showplot',true);
%對於未知的測試集進行分類預測,結果如圖:
tu3
3.jpg (37.34 KB)
2009-5-12 19:50
classperf(cp,classes,test);
cp.CorrectRate
ans =
0.9867
%分類器效果測評,就是看測試集分類的準確率的高低.
二.台灣林智仁的libsvm工具箱
該工具箱下載[libsvm-mat-2.86-1]: libsvm-mat-2.86-1.rar(73.75 KB)
libsvm-mat-2.86-1.rar(73.75 KB)
下載次數: 373
2009-5-12 20:02
安裝方法也很簡單,解壓檔案,把當前工作目錄調整到libsvm所在的檔案夾下,再在set path裡將libsvm所在的檔案夾加到裡面.然後
在命令列裡輸入
mex -setup %選擇一下編譯器
make
這樣就可以了.
建議大家使用libsvm工具箱,這個更好用一些.可以進行分類[多類別],預測....
=========
svmtrain
svmpredict
================
簡要文法:
Usage
=====
matlab> model = svmtrain(training_label_vector, training_instance_matrix [,'libsvm_options']);
-training_label_vector:
An m by 1 vector oftraining labels (type must be double).
-training_instance_matrix:
An m by n matrix of mtraining instances with n features.
It can be dense or sparse(type must be double).
-libsvm_options:
A string of trainingoptions in the same format as that of LIBSVM.
matlab> [predicted_label, accuracy, decision_values/prob_estimates] =svmpredict(testing_label_vector, testing_instance_matrix, model [,'libsvm_options']);
-testing_label_vector:
An m by 1 vector ofprediction labels. If labels of test
data are unknown, simplyuse any random values. (type must be double)
-testing_instance_matrix:
An m by n matrix of mtesting 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 optionsin the same format as that of LIBSVM.
Returned Model Structure
========================
執行個體研究:
load heart_scale.mat
%工具箱裡內建的資料
如圖:
tu4
4.jpg(9.36 KB)
2009-5-12 20:08
其中 heart_scale_inst是樣本,heart_scale_label是樣本標籤
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);
%分類預測,這裡把訓練集當作測試集,驗證效果如下:
>> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label,heart_scale_inst, model); % test the training data
Accuracy = 86.6667% (234/270) (classification)
==============
這回把SVM這點入門的東西都說完了,大家可以參照著上手了,有關SVM的原理我下面有個簡易的PPT,是以前做項目時我做的[當時我負責有關SVM這一塊代碼實現講解什麼的],感興趣的你可以看看,都是上手較快的東西,想要深入學習SVM,你的學習統計學習理論什麼的....挺多的呢..
SVM.ppt(391 KB)
SVM.ppt(391 KB)
下載次數: 429
2009-5-12 20:18
-----------有關SVM和libsvm的非常好的資料,想要詳細研究SVM看這個------
libsvm_guide.pdf(194.53 KB)
libsvm_guide.pdf(194.53 KB)
下載次數:186
2009-8-19 14:58
libsvm_library.pdf(316.82 KB)
libsvm_library.pdf(316.82 KB)
下載次數: 137
2009-8-19 14:58
OptimizationSupportVectorMachinesandMachine