libsvm savemodel and loadmodel
来源:互联网 发布:将字符串转换为数组 编辑:程序博客网 时间:2024/06/14 18:11
savemodel 和 loadmodel的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;}savemode.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 status; // check input if(nrhs != 2) { mexPrintf("Usage: svm_savemodel(model, 'filename');\n"); fake_answer(plhs); return; } if(!mxIsStruct(prhs[0])) { mexPrintf("model file should be a struct array\n"); fake_answer(plhs); return; } if(!mxIsChar(prhs[1]) || mxGetM(prhs[1])!=1) { mexPrintf("filename should be given as char(s)\n"); fake_answer(plhs); return; } // convert MATLAB struct to C struct model = matlab_matrix_to_model(prhs[0], &error_msg); if(model == NULL) { mexPrintf("Error: can't read model: %s\n", error_msg); fake_answer(plhs); return; } // get filename filename = mxArrayToString(prhs[1]); // save model to file status = svm_save_model(filename,model); if (status != 0) { mexWarnMsgTxt("Error occured while writing to file."); } // destroy model svm_free_and_destroy_model(&model); mxFree(filename); // return status value (0: success, -1: failure) plhs[0] = mxCreateDoubleScalar(status); return;}之后在命令行下运行:
mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims loadmodel.c ../svm.cpp svm_model_matlab.cmex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims savemodel.c ../svm.cpp svm_model_matlab.c
会得到savemodel 和loadmodel 两个函数
下面给出一个使用的例子:
%# 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');savemodel(model, 'm.model');%# load model from file, and use it to predict labelsm = loadmodel('m.model',size(X,2));[yy, acc, prob_est] = libsvmpredict(y, X, m, '-b 1');
0 0
- libsvm savemodel and loadmodel
- LIBSVM
- libSVM
- libsvm
- LibSVM
- libsvm
- libsvm
- LIBSVM
- libsvm
- libsvm
- libsvm
- libsvm
- libSVM
- libsvm
- libsvm
- libsvm
- LibSVM
- LibSVM
- Android APP 缓存技术笔记1
- 数据结构习题1
- Android修炼之道—Bootstrap框架
- 归并排序
- verilog 入门教程
- libsvm savemodel and loadmodel
- 在使用libcurl库时发现的问题
- Xcode 的 svn 上传.a 文件解决方案
- Android dip,px,pt,sp 的区别
- 在Endnote中制作GB/T7714《文后参考文献著录规则》的输出格式
- Refbase
- Java基础回顾_Java内部类详细
- C语言(新建c文件、编译)
- SOCKET的send recv流程