« 5分でgnuplotを使う | メイン | Cisco Packet Tracer »

LibSVMを使う

LibSVMは,教師付き分類器であるサポートベクターマシンの実装の一つ.
C++,Java,Python, R, MATLAB, Perl, Ruby, Weka, Common LISP, CLISP, Haskell, LabVIEW, C# .NET, CUDA の環境からライブラリとして呼び出すことができる.C++,Javaについてはオープンソースになっている.その使い方を一部メモ.

入手は,http://www.csie.ntu.edu.tw/~cjlin/libsvm/ から.

教師データの準備

+1 0:0.15605779 1:10.0 2:0.0 3:1.0
+1 0:0.17847104 1:10.0 2:0.0 3:1.0
+1 0:0.29922867 1:10.0 2:0.0 3:0.0
+1 0:0.2403232 1:10.0 2:0.0 3:0.0
-1 0:0.10234895 1:10.0 2:0.0 3:0.0
-1 0:0.28430352 1:10.0 2:0.0 3:1.0
-1 0:0.29922867 1:10.0 2:0.0 3:0.0
-1 0:0.05195945 1:10.0 2:0.0 3:0.0

のように,左端にクラス,その後スペースを挟んで,

0:(0番目のパラメータ) 1:(1番目のパラメータ)

として値を記入したテキストファイルを作成.
ここでは仮に,「input.txt」とする.


svm-trainクラスによるmodelの作成

javaを使う場合は,javaディレクトリの中で,


%javac *.java

として,あらかじめJavaファイルをコンパイルしておく.

次に,


%java svm_train input.txt

とすることで,input.txt.modelが作成される.

svm_trainクラスには,カーネルの指定などのオプションがある.以下,READMEから引用.


Usage: svm-train [options] training_set_file [model_file]
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)
4 -- precomputed kernel (kernel values in training_set_file)
-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)
-v n: n-fold cross validation mode
-q : quiet mode (no outputs)


svm_predictによる判定

+0 0:0.2299006 1:10.0 2:0.0 3:0.0

のように,

 クラス 0:(0番目のパラメータ) 1:(1番目のパラメータ)

という書式で,判定対象のパラメータを記入したテキストファイルを準備する.
ここでは,test.txtとする.

次に,

%java svm_predict test.txt input.txt.model output.txt

のように,第1引数に判定対象のパラメータファイル,第2引数にmodelファイル,第3引数に結果の出力先を指定する.

すると,出力先ファイルに,

1.0

のように,分類結果が示される.

トラックバック

このエントリーのトラックバックURL:
http://www.ddhost.jp/mt/mt-tb.cgi/520

About

2011年02月08日 17:00に投稿されたエントリーのページです。

ひとつ前の投稿は「5分でgnuplotを使う」です。

次の投稿は「Cisco Packet Tracer」です。

他にも多くのエントリーがあります。メインページアーカイブページも見てください。