Exemple #1
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;

	// fix random seed to have same results for each run
	// (for cross validation and probability estimation)
	srand(1);

	if(nlhs > 1)
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}

	// Transform the input Matrix to libsvm format
	if(nrhs > 1 && nrhs < 4)
	{
		int err;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
			mexPrintf("Error: label vector and instance matrix must be double\n");
			fake_answer(nlhs, plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			svm_destroy_param(&param);
			fake_answer(nlhs, plhs);
			return;
		}

		if(mxIsSparse(prhs[1]))
		{
			if(param.kernel_type == PRECOMPUTED)
			{
				// precomputed kernel requires dense matrix, so we make one
				mxArray *rhs[1], *lhs[1];

				rhs[0] = mxDuplicateArray(prhs[1]);
				if(mexCallMATLAB(1, lhs, 1, rhs, "full"))
				{
					mexPrintf("Error: cannot generate a full training instance matrix\n");
					svm_destroy_param(&param);
					fake_answer(nlhs, plhs);
					return;
				}
				err = read_problem_dense(prhs[0], lhs[0]);
				mxDestroyArray(lhs[0]);
				mxDestroyArray(rhs[0]);
			}
			else
				err = read_problem_sparse(prhs[0], prhs[1]);
		}
		else
			err = read_problem_dense(prhs[0], prhs[1]);

		// svmtrain's original code
		error_msg = svm_check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			svm_destroy_param(&param);
			free(prob.y);
			free(prob.x);
			free(x_space);
			fake_answer(nlhs, plhs);
			return;
		}

		if(cross_validation)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			int nr_feat = (int)mxGetN(prhs[1]);
			const char *error_msg;
			model = svm_train(&prob, &param);
			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);
			svm_free_and_destroy_model(&model);
		}
		svm_destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}
}
Exemple #2
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction(int nlhs, mxArray *plhs[],
		 int nrhs, const mxArray *prhs[]){
  const char *error_msg=NULL;

  // transform the input matrix to libspline format
  if(nrhs > 0 && nrhs < 5){
    int err=0;
    
    if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
      mexPrintf("Error: label vector and instance matrix must be double\n");
      fake_answer(plhs);
      return;
    }
    
    if(parse_command_line(nrhs, prhs, NULL)){
      exit_with_help();
      fake_answer(plhs);
      return;
    }
		
    if(!mxIsSparse(prhs[1]))
      err = read_problem_dense(prhs[0], prhs[1]);
    else{
      mexPrintf("Error: test_instance_matrix must be dense\n");
      fake_answer(plhs);
      return;
    }
    
    if(err || error_msg){
      if (error_msg != NULL)
	mexPrintf("Error: %s\n", error_msg);
      fake_answer(plhs);
      delete [] x;
      return;
    }
    
		
    //initialize an empty model
    model = new additiveModel();
    
    error_msg = matlab_matrix_to_model(model,prhs[2]);
    
    if(error_msg){
      mexPrintf("Error: can't read model: %s\n", error_msg);
      delete [] x;
      delete model;
      fake_answer(plhs);
      return;
    }

    //compute predictions
    if(do_predict(plhs) < 0){
      delete [] x;
      delete [] model;
      fake_answer(plhs);
      return;
    }
    delete [] x;
    delete model;
  }else{
    exit_with_help();
    fake_answer(plhs);
    return;
  }
}