11 #if MX_API_VER < 0x07030000 17 #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) 28 "Usage: model = svmtrain(training_label_vector, training_instance_matrix, 'libsvm_options');\n" 30 "-s svm_type : set type of SVM (default 0)\n" 31 " 0 -- C-SVC (multi-class classification)\n" 32 " 1 -- nu-SVC (multi-class classification)\n" 33 " 2 -- one-class SVM\n" 34 " 3 -- epsilon-SVR (regression)\n" 35 " 4 -- nu-SVR (regression)\n" 36 "-t kernel_type : set type of kernel function (default 2)\n" 37 " 0 -- linear: u'*v\n" 38 " 1 -- polynomial: (gamma*u'*v + coef0)^degree\n" 39 " 2 -- radial basis function: exp(-gamma*|u-v|^2)\n" 40 " 3 -- sigmoid: tanh(gamma*u'*v + coef0)\n" 41 " 4 -- precomputed kernel (kernel values in training_instance_matrix)\n" 42 "-d degree : set degree in kernel function (default 3)\n" 43 "-g gamma : set gamma in kernel function (default 1/num_features)\n" 44 "-r coef0 : set coef0 in kernel function (default 0)\n" 45 "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n" 46 "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n" 47 "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n" 48 "-m cachesize : set cache memory size in MB (default 100)\n" 49 "-e epsilon : set tolerance of termination criterion (default 0.001)\n" 50 "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n" 51 "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n" 52 "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n" 53 "-v n : n-fold cross validation mode\n" 54 "-q : quiet mode (no outputs)\n" 70 int total_correct = 0;
71 double total_error = 0;
72 double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0;
80 for (i = 0; i <
prob.
l; i++)
84 total_error += (v - y) * (v - y);
91 mexPrintf(
"Cross Validation Mean squared error = %g\n", total_error /
prob.
l);
92 mexPrintf(
"Cross Validation Squared correlation coefficient = %g\n",
93 ((
prob.
l * sumvy - sumv * sumy) * (
prob.
l * sumvy - sumv * sumy)) /
94 ((
prob.
l * sumvv - sumv * sumv) * (
prob.
l * sumyy - sumy * sumy))
96 retval = total_error /
prob.
l;
100 for (i = 0; i <
prob.
l; i++)
101 if (target[i] ==
prob.
y[i])
103 mexPrintf(
"Cross Validation Accuracy = %g%%\n", 100.0 * total_correct /
prob.
l);
104 retval = 100.0 * total_correct /
prob.
l;
142 mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
143 if ((argv[argc] = strtok(cmd,
" ")) != NULL)
144 while ((argv[++argc] = strtok(NULL,
" ")) != NULL)
149 for (i = 1; i < argc; i++)
151 if (argv[i][0] !=
'-')
break;
153 if (i >= argc && argv[i - 1][1] !=
'q')
155 switch (argv[i - 1][1])
202 mexPrintf(
"n-fold cross validation: n must >= 2\n");
214 mexPrintf(
"Unknown option -%c\n", argv[i - 1][1]);
228 int elements,
max_index, sc, label_vector_row_num;
229 double *samples, *labels;
235 labels = mxGetPr(label_vec);
236 samples = mxGetPr(instance_mat);
237 sc = (int)mxGetN(instance_mat);
241 prob.
l = (int)mxGetM(instance_mat);
242 label_vector_row_num = (int)mxGetM(label_vec);
244 if (label_vector_row_num !=
prob.
l)
246 mexPrintf(
"Length of label vector does not match # of instances.\n");
251 elements =
prob.
l * (sc + 1);
254 for (i = 0; i <
prob.
l; i++)
256 for (k = 0; k < sc; k++)
257 if (samples[k *
prob.
l + i] != 0)
270 for (i = 0; i <
prob.
l; i++)
272 prob.
x[i] = &x_space[j];
273 prob.
y[i] = labels[i];
275 for (k = 0; k < sc; k++)
279 x_space[j].
index = k + 1;
284 x_space[j++].
index = -1;
291 for (i = 0; i <
prob.
l; i++)
295 mexPrintf(
"Wrong input format: sample_serial_number out of range\n");
305 int i, j, k, low, high;
307 int elements,
max_index, num_samples, label_vector_row_num;
308 double *samples, *labels;
309 mxArray *instance_mat_col;
317 mxArray *prhs[1], *plhs[1];
318 prhs[0] = mxDuplicateArray(instance_mat);
319 if (mexCallMATLAB(1, plhs, 1, prhs,
"transpose"))
321 mexPrintf(
"Error: cannot transpose training instance matrix\n");
324 instance_mat_col = plhs[0];
325 mxDestroyArray(prhs[0]);
329 labels = mxGetPr(label_vec);
330 samples = mxGetPr(instance_mat_col);
331 ir = mxGetIr(instance_mat_col);
332 jc = mxGetJc(instance_mat_col);
334 num_samples = (int)mxGetNzmax(instance_mat_col);
337 prob.
l = (int)mxGetN(instance_mat_col);
338 label_vector_row_num = (int)mxGetM(label_vec);
340 if (label_vector_row_num !=
prob.
l)
342 mexPrintf(
"Length of label vector does not match # of instances.\n");
346 elements = num_samples +
prob.
l;
347 max_index = (int)mxGetM(instance_mat_col);
354 for (i = 0; i <
prob.
l; i++)
356 prob.
x[i] = &x_space[j];
357 prob.
y[i] = labels[i];
358 low = (int)jc[i], high = (
int)jc[i + 1];
359 for (k = low; k < high; k++)
361 x_space[j].
index = (int)ir[k] + 1;
362 x_space[j].
value = samples[k];
365 x_space[j++].
index = -1;
376 plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
382 int nrhs,
const mxArray *prhs[])
384 const char *error_msg;
391 if (nrhs > 1 && nrhs < 4)
395 if (!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
397 mexPrintf(
"Error: label vector and instance matrix must be double\n");
410 if (mxIsSparse(prhs[1]))
415 mxArray *rhs[1], *lhs[1];
417 rhs[0] = mxDuplicateArray(prhs[1]);
418 if (mexCallMATLAB(1, lhs, 1, rhs,
"full"))
420 mexPrintf(
"Error: cannot generate a full training instance matrix\n");
426 mxDestroyArray(lhs[0]);
427 mxDestroyArray(rhs[0]);
438 if (err || error_msg)
440 if (error_msg != NULL)
441 mexPrintf(
"Error: %s\n", error_msg);
453 plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
454 ptr = mxGetPr(plhs[0]);
459 int nr_feat = (int)mxGetN(prhs[1]);
460 const char *error_msg;
464 mexPrintf(
"Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
const char * svm_check_parameter(const svm_problem *prob, const svm_parameter *param)
struct svm_parameter param
def svm_train(arg1, arg2=None, arg3=None)
void svm_cross_validation(const svm_problem *prob, const svm_parameter *param, int nr_fold, double *target)
int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
static void fake_answer(mxArray *plhs[])
void print_string_matlab(const char *s)
int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
void print_null(const char *s)
void svm_free_and_destroy_model(svm_model **model_ptr_ptr)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
double do_cross_validation()
void svm_destroy_param(svm_parameter *param)
const char * model_to_matlab_structure(mxArray *plhs[], int num_of_feature, struct svm_model *model)
struct svm_node * x_space
void svm_set_print_string_function(void(*print_func)(const char *))