Example #1
0
int main(int argc, char **argv)
{
	FILE *input, *output;
	int i;

	// parse options
	for(i=1;i<argc;i++)
	{
		if(argv[i][0] != '-') break;
		++i;
		switch(argv[i-1][1])
		{
//			case 'b':
//				flag_predict_probability = atoi(argv[i]);
//				break;
			case 'o':
				output_option = atoi(argv[i]);
				break;
			case 'q':
				info = &print_null;
				i--;
				break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}
	if(i>=argc)
		exit_with_help();

	input = fopen(argv[i],"r");
	if(input == NULL)
	{
		fprintf(stderr,"can't open input file %s\n",argv[i]);
		exit(1);
	}

	output = fopen(argv[i+2],"w");
	if(output == NULL)
	{
		fprintf(stderr,"can't open output file %s\n",argv[i+2]);
		exit(1);
	}

	if((model_=load_model(argv[i+1]))==0)
	{
		fprintf(stderr,"can't open model file %s\n",argv[i+1]);
		exit(1);
	}

	x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node));
	do_predict(input, output);
	free_and_destroy_model(&model_);
	free(line);
	free(x);
	fclose(input);
	fclose(output);
	return 0;
}
Example #2
0
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	int prob_estimate_flag = 0;
	struct model *model_;
	char cmd[CMD_LEN];
	col_format_flag = 0;

	if(nrhs > 5 || nrhs < 3)
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
	if(nrhs == 5)
	{
		mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1);
		if(strcmp(cmd, "col") == 0)
		{			
			col_format_flag = 1;
		}
	}

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

	if(mxIsStruct(prhs[2]))
	{
		const char *error_msg;

		// parse options
		if(nrhs>=4)
		{
			int i, argc = 1;
			char *argv[CMD_LEN/2];

			// put options in argv[]
			mxGetString(prhs[3], cmd,  mxGetN(prhs[3]) + 1);
			if((argv[argc] = strtok(cmd, " ")) != NULL)
				while((argv[++argc] = strtok(NULL, " ")) != NULL)
					;

			for(i=1;i<argc;i++)
			{
				if(argv[i][0] != '-') break;
				if(++i>=argc)
				{
					exit_with_help();
					fake_answer(plhs);
					return;
				}
				switch(argv[i-1][1])
				{
					case 'b':
						prob_estimate_flag = atoi(argv[i]);
						break;
					default:
						mexPrintf("unknown option\n");
						exit_with_help();
						fake_answer(plhs);
						return;
				}
			}
		}

		model_ = Malloc(struct model, 1);
		error_msg = matlab_matrix_to_model(model_, prhs[2]);
		if(error_msg)
		{
			mexPrintf("Error: can't read model: %s\n", error_msg);
			free_and_destroy_model(&model_);
			fake_answer(plhs);
			return;
		}

		if(prob_estimate_flag)
		{
			if(!check_probability_model(model_))
			{
				mexPrintf("probability output is only supported for logistic regression\n");
				prob_estimate_flag=0;
			}
		}

		if(mxIsSparse(prhs[1]))
			do_predict(plhs, prhs, model_, prob_estimate_flag);
		else
		{
			mexPrintf("Testing_instance_matrix must be sparse; "
				"use sparse(Testing_instance_matrix) first\n");
			fake_answer(plhs);
		}

		// destroy model_
		free_and_destroy_model(&model_);
	}
	else
	{
Example #3
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;
  }
}
int main(int argc, char **argv)
{
  const char *error_msg;
  parse_command_line(argc, argv); // also load data
  error_msg = check_parameter(prob,&param);
  
  if(error_msg)
    {
      fprintf(stderr,"Error: %s\n",error_msg);
      exit(1);
    }
	

  std::vector< std::pair<double, double> > test_errors(nb_runs);
  std::vector< std::pair<double, double> > train_errors(nb_runs);
  double trn_mean=0;
  double tst_mean=0;
  double mse_trn_mean=0;
  double mse_tst_mean=0;
  int *start = NULL;

  // perform runs
  for (int run=0; run<nb_runs; run++)
    {

      if ((trnsz>=prob->l) || (trnsz<=0))
	{
	  fprintf(stderr,"\nRun %d (from 0 to %d)\n", run, prob->l-1);

	  //train
	  model_=train(prob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(prob, model_);
	}
      else
	{
          // select all the splits before optimizing
          if(run == 0)
            {
              start = Malloc(int,nb_runs); 
              for (int run2=0; run2<nb_runs; run2++)
                start[run2] = (rand() % (prob->l-trnsz));
            }
	  // select examples
	  fprintf(stderr,"\nRun %d (from %d to %d)\n", run, start[run], start[run]+trnsz-1);
	  struct problem* subprob=extract_subprob(prob, start[run], trnsz);
	  
	  //train
	  model_=train(subprob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(subprob, model_);
	  free(subprob->y);
	  free(subprob->x);
	}

      tst_mean+=test_errors[run].first;
      printf("Test  classification ERROR = %g\n",test_errors[run].first);
      trn_mean+=train_errors[run].first;
      printf("Train classification ERROR = %g\n",train_errors[run].first);

      mse_tst_mean+=test_errors[run].second;
      printf("Test  normalized ACCURACY (ET requirement) = %g\n",test_errors[run].second);
      mse_trn_mean+=train_errors[run].second;
      printf("Train normalized ACCURACY (ET requirement) = %g\n",train_errors[run].second);

      //destroy model
      free_and_destroy_model(&model_);
      destroy_param(&param);
      
    }
Example #5
0
int main(int argc, char **argv)
{
	FILE *input, *output;
	int i;
	char model_file[1024];

	// parse options
	for(i=1;i<argc;i++)
	{
		if(argv[i][0] != '-') break;
		++i;
		switch(argv[i-1][1])
		{
			case 'b':
				flag_predict_probability = atoi(argv[i]);
				break;
			case 'q':
				info = &print_null;
				i--;
				break;
			default:
				fprintf(stderr,"unknown option: -%c\n", argv[i-1][1]);
				exit_with_help();
				break;
		}
	}
	if(i>=argc)
		exit_with_help();

	input = fopen(argv[i],"r");
	if(input == NULL)
	{
		fprintf(stderr,"can't open input file %s\n",argv[i]);
		exit(1);
	}

	output = fopen(argv[i+2],"w");
	if(output == NULL)
	{
		fprintf(stderr,"can't open output file %s\n",argv[i+2]);
		exit(1);
	}

	// if((model_=load_model(argv[i+1]))==0)
	// {
	// 	fprintf(stderr,"can't open model file %s\n",argv[i+1]);
	// 	exit(1);
	// }

	for ( int pid = 0; pid < sum_pro; pid++) {
		sprintf(model_file,"L%d%s%d%s",pid/(BLOCK * N) + 1, "_R", pid%(BLOCK * N) + 1,".model");
		printf("%s\n", model_file);
		if ((model_[pid] = load_model(model_file)) == 0) {
			fprintf(stderr,"can't open model file %s\n",argv[i+1]);
			exit(1);
		}
	}

	x = (struct feature_node *) malloc(max_nr_attr*sizeof(struct feature_node));
	do_predict(input, output);
	for ( int pid = 0; pid < sum_pro; pid++) {
		free_and_destroy_model(&model_[pid]);
	}
	free(line);
	free(x);
	fclose(input);
	fclose(output);
	return 0;
}