Load a LibSVM model file in Matlab -


i have couple of libsvm model files created running svm train on command line. i'm trying load them matlab.
i've tried running libsvm2mat program linked in previous answer. when try run svmpredict in matlab terminal gives me error message:

"error: can't read model: number of return field not correct." 

any suggestions?

edit: furthermore, libsvm2mat gave me warning messages proba ignored! , probb ignored! though both of them defined in file.

try following mex-function, based on this:

libsvmloadmodel.c

#include "svm.h" #include "mex.h" #include "svm_model_matlab.h"  static void fake_answer(mxarray *plhs[]) {     plhs[0] = mxcreatedoublematrix(0, 0, mxreal); }  void mexfunction(int nlhs, mxarray *plhs[], int nrhs, const mxarray *prhs[]) {     struct svm_model *model;     char *filename;     const char *error_msg;     int nr_feat;      // check input     if(nrhs != 2) {         mexprintf("usage: model = libsvmloadmodel('filename', num_of_feature);\n");         fake_answer(plhs);         return;     }     if(!mxischar(prhs[0]) || mxgetm(prhs[0])!=1) {         mexprintf("filename should given string\n");         fake_answer(plhs);         return;     }     if(mxgetnumberofelements(prhs[1])!=1) {         mexprintf("number of features should given scalar\n");         fake_answer(plhs);         return;     }      // filename , number of features     filename = mxarraytostring(prhs[0]);     nr_feat = (int) *(mxgetpr(prhs[1]));      // load model file     model = svm_load_model(filename);     if (model == null) {         mexprintf("error occured while reading file.\n");         fake_answer(plhs);         mxfree(filename);         return;     }      // convert matlab struct c struct     error_msg = model_to_matlab_structure(plhs, nr_feat, model);     if(error_msg) {         mexprintf("error: can't convert libsvm model matrix structure: %s\n", error_msg);         fake_answer(plhs);     }      // destroy model     svm_free_and_destroy_model(&model);     mxfree(filename);      return; } 

example usage in matlab:

%# load data, train model, save file [y,x] = libsvmread('./heart_scale'); model = libsvmtrain(y, x, '-c 1 -g 0.07 -b 1'); libsvmsavemodel(model, 'm.model');  %# load model file, , use predict labels m = libsvmloadmodel('m.model',size(x,2)); [yy, acc, prob_est] = libsvmpredict(y, x, m, '-b 1'); 

note sv_indices in model not persisted:

>> model model =      parameters: [5x1 double]       nr_class: 2        totalsv: 130            rho: 0.42641          label: [2x1 double]     sv_indices: [130x1 double]          proba: -1.7801          probb: -0.056797            nsv: [2x1 double]        sv_coef: [130x1 double]            svs: [130x13 double] >> m m =      parameters: [5x1 double]       nr_class: 2        totalsv: 130            rho: 0.42641          label: [2x1 double]     sv_indices: []          proba: -1.7801          probb: -0.056797            nsv: [2x1 double]        sv_coef: [130x1 double]            svs: [130x13 double] 

the saved model looks like:

m.model

svm_type c_svc kernel_type rbf gamma 0.07 nr_class 2 total_sv 130 rho 0.426412 label 1 -1 proba -1.7801 probb -0.0567966 nr_sv 63 67 sv 1 1:0.166667 2:1 3:-0.333333 4:-0.433962 5:-0.383562 6:-1 7:-1 8:0.0687023 9:-1 10:-0.903226 11:-1 12:-1 13:1  0.6646947579781318 1:0.125 2:1 3:0.333333 4:-0.320755 5:-0.406393 6:1 7:1 8:0.0839695 9:1 10:-0.806452 12:-0.333333 13:0.5  

Comments