SimpleCall::LIBSVM
load csv file, call libsvm for mechainze learning, export predict csv file
libsvm : https://www.csie.ntu.edu.tw/~cjlin/libsvm/
default sep is comma, ','
example in xt/ directory, use iris data
use SimpleCall::LIBSVM; my $s = SimpleCall::LIBSVM->new(); #train my %data_opt = ( type => 'Species', data => [ qw/SepalLength SepalWidth PetalLength PetalWidth/ ], ); my $train = 'iris_train.csv'; my $train_opt = '-h 0'; my ($train_data, $train_type) = $s->conv_file_to_libsvm($train, %data_opt); my $train_model = $s->train_libsvm($train_data, "$train.libsvm.model", train_opt => $train_opt); # test my $test = 'iris_test.csv'; my $predict_opt = ''; my ($test_data, $test_type) = $s->conv_file_to_libsvm($test, libsvm_type => $train_type, %data_opt, ); my $test_out = $s->predict_libsvm($test_data, $train_model, "$test.libsvm.out", predict_opt=>$predict_opt); my $test_predict_f = $s->conv_libsvm_to_file($test, libsvm_type=> $test_type, libsvm_out => $test_out, predict_file => "$test.predict.csv", );
perl chart_libsvm.pl [predict_csv_file] [predict_type_column_index] [cnt_column_index] example chart is xt/iris_test.csv.predict.csv.0.x.png
init
my $s = SimpleCall::LIBSVM->new( sep => ',' );
conv src file into libsvm format : $train -> $train_data , $train_type : map type into integer
my ($train_data, $train_type) = $s->conv_file_to_libsvm($train, type => 'xxx', data => [ 'yyy', 'zzz' ], # libsvm_type => 'kkkk.libsvm.type' , #have exists type file );
call svm-train to build model file
my $train_model = $s->train_libsvm($train_data, "$train.libsvm.model", train_opt => $train_opt);
call svm-predict to predict $test_data use $train_model
my $test_out = $s->predict_libsvm($test_data, $train_model, "$test.libsvm.out", predict_opt=>$predict_opt);
merge libsvm predict out file, rewrite $test, add predict column ahead
my $test_predict_f = $s->conv_libsvm_to_file($test, libsvm_type=> $test_type, libsvm_out => $test_out, predict_file => "$test.predict.csv", );
To install SimpleCall::LIBSVM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SimpleCall::LIBSVM
CPAN shell
perl -MCPAN -e shell install SimpleCall::LIBSVM
For more information on module installation, please visit the detailed CPAN module installation guide.