C++ 将matlab中的libsvm模型结构保存到.model文件中,该文件可以在C+中读取+;
我有两个问题:C++ 将matlab中的libsvm模型结构保存到.model文件中,该文件可以在C+中读取+;,c++,matlab,libsvm,C++,Matlab,Libsvm,我有两个问题: 我按照邮局的指示去做 我尝试在Mac OSx El Capitan上构建svm_savemodel.c,使用: mex svm_savemodel.c发生了以下情况: 使用“带叮当声的Xcode”构建。为架构x86\u 64使用mex未定义符号时出错:“\u matlab\u matrix\u to\u model”,引用自:svm\u savemodel.o中的\u mexFunction“\u svm\u free\u and \u destroy\u model”,引用自:
int main(int argc, const char * argv[]) {
char input_file_name[1024];
char model_file_name[1024] = "MyModel" ;
const char *error_msg;
param.svm_type = EPSILON_SVR;
param.eps = 1e-3;
param.C = 1;
//param.kernel_type = RBF;
param.degree = 3;
//Problem definition-------------------------------------------------------------
prob.l = 8;
//x values matrix of xor values
double matrix[prob.l][2];
matrix[0][0] = 0.1;
matrix[0][1] = 0.2;
matrix[1][0] = 0.2;
matrix[1][1] = 0.3;
matrix[2][0] = 0.4;
matrix[2][1] = 0.5;
matrix[3][0] = 0.8;
matrix[3][1] = 0.9;
matrix[4][0] = 0.6;
matrix[4][1] = 0.7;
matrix[5][0] = 0.55;
matrix[5][1] = 0.65;
matrix[6][0] = 0.45;
matrix[6][1] = 0.55;
matrix[7][0] = 0.65;
matrix[7][1] = 0.75;
svm_node** x = Malloc(svm_node*,prob.l);
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row <prob.l; row++){
svm_node* x_space = Malloc(svm_node,3);
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = Malloc(double,prob.l);
prob.y[0] = 0.15;
prob.y[1] = 0.25;
prob.y[2] = 0.45;
prob.y[3] = 0.85;
prob.y[4] = 0.65;
prob.y[5] = 0.6;
prob.y[6] = 0.5;
prob.y[7] = 0.7;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//svm_model *model = svm_load_model(model_file_name);
//Test model----------------------------------------------------------------------
svm_node* testnode = Malloc(svm_node,3);
testnode[0].index = 0;
testnode[0].value = 0.6;
testnode[1].index = 1;
testnode[1].value = 0.7;
testnode[2].index = -1;
//This works correctly:
double retval = svm_predict(model,testnode);
svm_save_model(model_file_name, model);
printf("retval: %f\n",retval);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return 0;
结束
fclose(fid)
}我修改了处的回归代码,并查看了保存模型的格式
{
struct svm_parameter param;//由parse_命令行设置
struct svm_problem prob;//由read_problem设置
//结构svm_模型*模型;
结构svm_节点*x_空间
int main(int argc, const char * argv[]) {
char input_file_name[1024];
char model_file_name[1024] = "MyModel" ;
const char *error_msg;
param.svm_type = EPSILON_SVR;
param.eps = 1e-3;
param.C = 1;
//param.kernel_type = RBF;
param.degree = 3;
//Problem definition-------------------------------------------------------------
prob.l = 8;
//x values matrix of xor values
double matrix[prob.l][2];
matrix[0][0] = 0.1;
matrix[0][1] = 0.2;
matrix[1][0] = 0.2;
matrix[1][1] = 0.3;
matrix[2][0] = 0.4;
matrix[2][1] = 0.5;
matrix[3][0] = 0.8;
matrix[3][1] = 0.9;
matrix[4][0] = 0.6;
matrix[4][1] = 0.7;
matrix[5][0] = 0.55;
matrix[5][1] = 0.65;
matrix[6][0] = 0.45;
matrix[6][1] = 0.55;
matrix[7][0] = 0.65;
matrix[7][1] = 0.75;
svm_node** x = Malloc(svm_node*,prob.l);
//Trying to assign from matrix to svm_node training examples
for (int row = 0;row <prob.l; row++){
svm_node* x_space = Malloc(svm_node,3);
for (int col = 0;col < 2;col++){
x_space[col].index = col;
x_space[col].value = matrix[row][col];
}
x_space[2].index = -1; //Each row of properties should be terminated with a -1 according to the readme
x[row] = x_space;
}
prob.x = x;
//yvalues
prob.y = Malloc(double,prob.l);
prob.y[0] = 0.15;
prob.y[1] = 0.25;
prob.y[2] = 0.45;
prob.y[3] = 0.85;
prob.y[4] = 0.65;
prob.y[5] = 0.6;
prob.y[6] = 0.5;
prob.y[7] = 0.7;
//Train model---------------------------------------------------------------------
svm_model *model = svm_train(&prob,¶m);
//svm_model *model = svm_load_model(model_file_name);
//Test model----------------------------------------------------------------------
svm_node* testnode = Malloc(svm_node,3);
testnode[0].index = 0;
testnode[0].value = 0.6;
testnode[1].index = 1;
testnode[1].value = 0.7;
testnode[2].index = -1;
//This works correctly:
double retval = svm_predict(model,testnode);
svm_save_model(model_file_name, model);
printf("retval: %f\n",retval);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return 0;
结束
fclose(fid)
}