Wednesday, November 30, 2011

HowTo - Using LIBSVM


Data format [1]


training_data / testing_data

+1 1:6.240913 2:0.117454 3:-0.957491 4:1.653823 5:0.929491
   6:-1.036259 7:-1.025772 8:0.372377 9:0.273566 10:-0.493052
   11:-0.583311 12:-0.218060 13:-0.376675 14:-0.589258 15:-0.346050
   16:0.125185 17:0.106718 18:-0.285916 19:-0.459112 20:-0.170765 

-1 1:10.284770 2:1.785967 3:-2.979066 4:-2.174549 5:-1.434280 

   6:-1.306482 7:-0.548775 8:0.777055 9:0.275654 10:-1.284672 
   11:-1.663850 12:-0.432742 13:0.273012 14:-0.137017 15:-0.533320 
   16:-0.255564 17:-0.193427 18:-0.584661 19:-0.668189 20:-0.118506

-1 1:8.184908 2:0.312909 3:-2.742637 4:-0.217420 5:0.472176 

   6:-1.277293 7:-2.298117 8:-0.996349 9:-0.141399 10:-0.481425 
   11:-0.589631 12:0.220194 13:0.382807 14:-0.349594 15:-0.694144 
   16:-0.155397 17:0.011160 18:-0.555094 19:-0.893452 20:-0.488398

+1 1:8.695444 2:0.383987 3:-1.195099 4:2.320266 5:1.958957 

   6:-0.578233 7:-1.353031 8:-0.364041 9:-0.357977 10:-0.794986 
   11:-0.867561 12:-0.755012 13:-1.061063 14:-1.202318 15:-0.848766 
   16:-0.270570 17:-0.199191 18:-0.510582 19:-0.540136 20:-0.134563
...


--- each line is an instance

+1 / -1 --- label 
1:6.240913   --- the value of Feature 1 is 6.240913 .

For prediction (if you don't know the labels of instances), I just put 0 where +1/-1 is put. It doesn't matter what you put here. 
You only need to check the predict result in the predict file (see the "Predict" session below).

Scale

Get scaling file range, scale training_data into training_data.scaled

$ ~/libsvm-3.11$ svm-scale -l -1 -u 1 -s range training_data > training_data.scaled

Train

Get parameters for C-SVM classification (-c ? -v ?)

$ ~/libsvm-3.11$ cd tools
$ ~/libsvm-3.11/tools$ python grid.py ../training_data.scaled
Cross validation (-v)

$ ~/libsvm-3.11/tools$ cd ..
$ ~/libsvm-3.11$ svm-train -c 2 -g 8 -v 4 training_data.scaled

Get classifying model

$ ~/libsvm-3.11$ svm-train -c 2 -g 8 training_data.scaled

After this, there will be a .model file in the current file folder.

Test

Scale testing_data

$ ~/libsvm-3.11$ svm-scale -r range testing_data > testing_data.scaledPredict

$ ~/libsvm-3.11$ svm-predict testing_data.scaled training_data.scaled.model predict

Predicted labels will be in the predict file in the current file folder.

predict

-1
-1
1
-1
.
.
.

Retrieve w and b from model [2]

w = model.SVs' * model.sv_coef; % the first number of each line is the coefficient
b = -model.rho;

if model.Label(1) == -1
 w = -w;
 b = -b;
end



Reference
[1] http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf

[2] http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f804

No comments:

Post a Comment