簡體   English   中英

在Matlab中加載LibSVM模型文件

[英]Load a LibSVM model file in Matlab

我有一些通過在命令行上運行svm train創建的libsvm模型文件。 我正在嘗試將它們加載到Matlab中。
我已經嘗試在前一個答案中運行鏈接到的libsvm2mat程序。 當我嘗試在Matlab終端中運行svmpredict ,它給出了錯誤消息:

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

有什么建議么?

編輯 :此外, libsvm2mat給了我probA ignored!的警告消息probA ignored! 並且probB ignored! 即使它們都在文件中定義。

嘗試以下MEX功能,基於

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 be given as string\n");
        fake_answer(plhs);
        return;
    }
    if(mxGetNumberOfElements(prhs[1])!=1) {
        mexPrintf("number of features should be given as scalar\n");
        fake_answer(plhs);
        return;
    }

    // get filename and number of features
    filename = mxArrayToString(prhs[0]);
    nr_feat = (int) *(mxGetPr(prhs[1]));

    // load model from file
    model = svm_load_model(filename);
    if (model == NULL) {
        mexPrintf("Error occured while reading from file.\n");
        fake_answer(plhs);
        mxFree(filename);
        return;
    }

    // convert MATLAB struct to C struct
    error_msg = model_to_matlab_structure(plhs, nr_feat, model);
    if(error_msg) {
        mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
        fake_answer(plhs);
    }

    // destroy model
    svm_free_and_destroy_model(&model);
    mxFree(filename);

    return;
}

MATLAB中的示例用法:

%# load some data, train a model, save it to file
[y,X] = libsvmread('./heart_scale');
model = libsvmtrain(y, X, '-c 1 -g 0.07 -b 1');
libsvmsavemodel(model, 'm.model');

%# load model from file, and use it to predict labels
m = libsvmloadmodel('m.model',size(X,2));
[yy, acc, prob_est] = libsvmpredict(y, X, m, '-b 1');

請注意,模型中的sv_indices不會sv_indices

>> 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]

保存的模型如下所示:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM