今日の一言
2010/08/03(Tue) 元気70倍!! ホワイトクリームパイマン!! [長年日記] 3:00現在晴 27℃
_ [Matlab][Linux][Debian][Ubuntu][FC][研究関係][雑記]Matlabでlibsvmを使う
まあ、Cのプログラムをmexでコンパイルして動かしているので、本当は「Support Vector Machine Toolbox」とか(http://www.support-vector-machines.org/SVM_soft.html)を使ったほうがいいかもしれない。
インストール
ここ(http://www.csie.ntu.edu.tw/~cjlin/libsvm)の「Interfaces to LIBSVM」から、
「A simple MATLAB interface」
をDLする。
解凍し、ReadMeを熟読。
で、それにしたがって、まずMakefileの
MATLABDIR ?= /usr/local/matlab
を、
MATLABDIR ?= /usr/local/MATLAB/R2009a/
のように、自分が実際にMatlabをインストールしたディレクトリのパスに修正する。
そして、ディレクトリ内でMatlabを開いて、
>> make
と打つ。
この際、
svm_model_matlab.c: In function ‘model_to_matlab_structure’: svm_model_matlab.c:37: error: expected expression before ‘/’ token svm_model_matlab.c:47: error: expected expression before ‘/’ token svm_model_matlab.c:53: error: expected expression before ‘/’ token svm_model_matlab.c:59: error: expected expression before ‘/’ token svm_model_matlab.c:67: error: expected expression before ‘/’ token svm_model_matlab.c:79: error: expected expression before ‘/’ token svm_model_matlab.c:91: error: expected expression before ‘/’ token svm_model_matlab.c:103: error: expected expression before ‘/’ token svm_model_matlab.c:115: error: expected expression before ‘/’ token svm_model_matlab.c:123: error: expected expression before ‘/’ token svm_model_matlab.c: In function ‘matlab_matrix_to_model’: svm_model_matlab.c:222: error: expected expression before ‘/’ token svm_model_matlab.c:240: error: expected expression before ‘/’ token svm_model_matlab.c:248: error: expected expression before ‘/’ token svm_model_matlab.c:258: error: expected expression before ‘/’ token svm_model_matlab.c:268: error: expected expression before ‘/’ token svm_model_matlab.c:278: error: expected expression before ‘/’ token svm_model_matlab.c:288: error: expected expression before ‘/’ token svm_model_matlab.c:298: error: expected expression before ‘/’ token mex: compile of ' "svm_model_matlab.c"' failed.
みたいなエラーが出る場合には、「make」ではなく、「!make」とすることでコンパイルできる場合がある。
サンプルでテスト
>> load heart_scale.mat >> model = svmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 2'); >> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model);
「Accuracy = 99.2593% (268/270) (classification)」とか言うのが表示されれば、取り合えずOKのはず。
参考1
実際の使い方はこちら(http://www.alivelearn.net/?p=912)を参考にさせていただきました。(ソースがあるので分かりやすい)
参考2
こちら(http://www.okuma.nuee.nagoya-u.ac.jp/~sakaguti/wiki/index.php?LibSVM)にモデルの指定についての詳しい説明があったので引用させていただく。
svm-trainの後ろにオプションをつけることで初期設定を変更できる.
libsvmには,svmのタイプが複数用意されている.
-s svm_type : SVMタイプの指定 0 -- C-SVC (デフォルトタイプ) 1 -- nu-SVC 2 -- one-class SVM 3 -- epsilon-SVR 4 -- nu-SVR
また,カーネル関数のタイプも指定できる.
-t kernel_type : カーネル関数の指定 0 -- 線形 1 -- 多項式 2 -- RBF (デフォルトタイプ) 3 -- シグモイド
カーネル関数に関連して
-d degree : カーネル関数のdegreeの指定 (デフォルト 3) -g gamma : カーネル関数のgammaの指定 (デフォルト 1/k, kは入力ベクトルの次元) -r coef0 : カーネル関数のcoef0の指定 (デフォルト 0) -c cost : コストパラメータの指定 (C-SVC, epsilon-SVR, and nu-SVRで使用、デフォルト 1) -n nu : nuパラメータの指定 (nu-SVC, one-class SVM, and nu-SVRで使用、デフォルト 0.5) -m cachesize : 使用キャッシュメモリサイズの指定(単位MB, デフォルト 40) -v n: n-foldのクロスバリデーションを実行
がある.
詳細について
こちらをどうぞー
_ [Matlab][Windows][Linux][Debian][Ubuntu][FC][研究関係][雑記]Matlab上の変数をファイルに保存
普通にasciiで保存してもいいが、MAT形式の方が、型や精度を保ったまま保存できるので便利。
特に、libsvmを使って学習させたmodelなどを保存する場合には、構造体のまま保存した方が扱いやすいと思われ。
すべての変数を一括で保存
>> save
とすれば、「matlab.mat」と言う名前ですべての変数を保存してくれる。
読み出す場合は、
>> load
とすれば、「matlab.mat」からすべての変数を読み込んでくれる。
なお、特定の変数のみを読み込みたい場合は、
>> load('matlab.mat','model');
とすれば、matlab.matの中から、modelという変数のみを探し出して読み込んでくれる。
目的の変数のみを保存
こんな感じでファイル名と変数名を指定して保存する。
>> save('model.mat','model')
で、ファイルから読み出す場合はこうする。
>> load('model.mat');
詳しい使い方
ここを見れ!(http://www.mathworks.co.jp/access/helpdesk_ja_JP/help/techdoc/matlab_oop/bres1y6.html)