double mfe_zscore(const char *seq, double mfe, int *type, int avoid_shuffle, char* warning_string) { double E, stdv, avg; char *struc; if (mfe>0) { struc = space(strlen(seq)+1); E = fold(seq, struc); free(struc); } else { E=mfe; } avg = 0.0; stdv = 0.0; predict_values(seq, &avg, &stdv, type, avoid_shuffle, warning_string); /* Just as backup strategy if something goes totally wrong, we evaluate the sequence once again by shuffling */ if (avg > -1 || stdv < 0.1) { if (*type == 2) *type = 3; if (*type == 0) *type = 1; predict_values(seq, &avg, &stdv, type, avoid_shuffle, warning_string); } /*printf("%f,%f\n",avg,stdv);*/ return ((E-avg)/stdv); }
double LVlinear_predict_values(lvError *lvErr, const LVlinear_model *model_in, const LVArray_Hdl<LVlinear_node> x_in, LVArray_Hdl<double> dec_values_out){ try{ // Input validation: Uninitialized model if (model_in == nullptr || model_in->w == nullptr || (*model_in->w)->dimSize == 0) throw LVException(__FILE__, __LINE__, "Uninitialized model passed to liblinear_predict_values."); // Input validation: Empty feature vector if (x_in == nullptr || (*x_in)->dimSize == 0) throw LVException(__FILE__, __LINE__, "Empty feature vector passed to liblinear_predict_values."); // Input validation: Final index -1? if ((*x_in)->elt[(*x_in)->dimSize - 1].index != -1) throw LVException(__FILE__, __LINE__, "The index of the last element of the feature vector needs to be -1 (liblinear_predict_values)."); // Convert LVsvm_model to svm_model auto mdl = std::make_unique<model>(); LVConvertModel(*model_in, *mdl); int nr_class = model_in->nr_class; int solver = (model_in->param).solver_type; // Set up output array int nr_dec = 0; if (nr_class <= 2){ if (solver == MCSVM_CS) nr_dec = 2; else nr_dec = 1; } else { nr_dec = nr_class; } LVResizeNumericArrayHandle(dec_values_out, nr_dec); (*dec_values_out)->dimSize = nr_dec; double predicted_label = predict_values(mdl.get(), reinterpret_cast<feature_node*>((*x_in)->elt), (*dec_values_out)->elt); return predicted_label; } catch (LVException &ex) { ex.returnError(lvErr); (*dec_values_out)->dimSize = 0; return std::nan(""); } catch (std::exception &ex) { LVException::returnStdException(lvErr, __FILE__, __LINE__, ex); (*dec_values_out)->dimSize = 0; return std::nan(""); } catch (...) { LVException ex(__FILE__, __LINE__, "Unknown exception has occurred"); ex.returnError(lvErr); (*dec_values_out)->dimSize = 0; return std::nan(""); } }
int copy_predict_values (char *predict, struct model *model_, npy_intp *predict_dims, char *dec_values, int nr_class) { npy_intp i; struct feature_node **predict_nodes; predict_nodes = dense_to_sparse((double *) predict, predict_dims, model_->bias); if (predict_nodes == NULL) return -1; for(i=0; i<predict_dims[0]; ++i) { predict_values(model_, predict_nodes[i], ((double *) dec_values) + i*nr_class); free(predict_nodes[i]); } free(predict_nodes); return 0; }
double LVlinear_predict_values(lvError *lvErr, const LVlinear_model *model_in, const LVArray_Hdl<LVlinear_node> x_in, LVArray_Hdl<double> dec_values_out){ try{ // Convert LVsvm_model to svm_model std::unique_ptr<model> model(new model); LVConvertModel(model_in, model.get()); int nr_class = model_in->nr_class; int solver = (model_in->param).solver_type; // Set up output array int nr_dec = 0; if (nr_class <= 2){ if (solver == MCSVM_CS) nr_dec = 2; else nr_dec = 1; } else { nr_dec = nr_class; } LVResizeNumericArrayHandle(dec_values_out, nr_dec); (*dec_values_out)->dimSize = nr_dec; double predicted_label = predict_values(model.get(), reinterpret_cast<feature_node*>((*x_in)->elt), (*dec_values_out)->elt); return predicted_label; } catch (LVException &ex) { ex.returnError(lvErr); (*dec_values_out)->dimSize = 0; return std::nan(""); } catch (std::exception &ex) { LVException::returnStdException(lvErr, __FILE__, __LINE__, ex); (*dec_values_out)->dimSize = 0; return std::nan(""); } catch (...) { LVException ex(__FILE__, __LINE__, "Unknown exception has occurred"); ex.returnError(lvErr); (*dec_values_out)->dimSize = 0; return std::nan(""); } }
double SVMLinear::predictModel(vector<double> features) { if(modelLinearSVM==NULL) { fprintf(stdout,"Error, Train Model First \n"); return 0.0; } int nr_class=get_nr_class(modelLinearSVM); int bias=modelLinearSVM->bias; int sparsity=0.0; for (int i=0; i<features.size(); i++) if(features[i]!=0.0) sparsity++; feature_node *x=Malloc(struct feature_node,sparsity+bias+1); //bias and -1 index int cnt=0; for (int i=0; i<features.size(); i++) { if(features[i]!=0.0) { x[cnt].index=i+1; x[cnt].value=features[i]; cnt++; } } if(bias) { x[cnt].index=modelLinearSVM->nr_feature+1, x[cnt].value=1; cnt++; } x[cnt].index=-1; double val=0; predict_values(modelLinearSVM,x,&val); return val; }
int csr_copy_predict_values(npy_intp n_features, npy_intp *data_size, char *data, npy_intp *index_size, char *index, npy_intp *indptr_shape, char *intptr, struct model *model_, char *dec_values, int nr_class) { struct feature_node **predict_nodes; npy_intp i; predict_nodes = csr_to_sparse((double *) data, index_size, (int *) index, indptr_shape, (int *) intptr, model_->bias, n_features); if (predict_nodes == NULL) return -1; for (i = 0; i < indptr_shape[0] - 1; ++i) { predict_values(model_, predict_nodes[i], ((double *) dec_values) + i*nr_class); free(predict_nodes[i]); } free(predict_nodes); return 0; }
void ImageViewer::classify(QImage &img) { struct feature_node* x = Malloc(struct feature_node, NUM_FEATURES+1); x[NUM_FEATURES].index = -1; // -1 marks the end of list std::vector<double> vPredictValues; double prob_estimates[1]; for (size_t i = 0; i < reader.getInstancesNumber(); i++) { for (int j = 0; j < NUM_FEATURES; j++) { x[j].index = 1+j; // 1-based feature number x[j].value = reader.instancesFeatures[i][j]; } int predict_label = predict(modelPedestrian, x); if (predict_label == 1) { predict_values(modelPedestrian, x, prob_estimates); } else { prob_estimates[0] = 0; } vPredictValues.push_back(prob_estimates[0]); } suppression(img, vPredictValues); reader.instancesFeatures.clear(); free(x); }
void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct feature_node *x; mxArray *pplhs[1]; // instance sparse matrix in row format int correct = 0; int total = 0; double error = 0; double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; int nr_class=get_nr_class(model_); int nr_w; double *prob_estimates=NULL; if(nr_class==2 && model_->param.solver_type!=MCSVM_CS) nr_w=1; else nr_w=nr_class; // prhs[1] = testing instance matrix feature_number = get_nr_feature(model_); testing_instance_number = (int) mxGetM(prhs[1]); if(col_format_flag) { feature_number = (int) mxGetM(prhs[1]); testing_instance_number = (int) mxGetN(prhs[1]); } label_vector_row_num = (int) mxGetM(prhs[0]); label_vector_col_num = (int) mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(col_format_flag) pplhs[0] = (mxArray *)prhs[1]; else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } prob_estimates = Malloc(double, nr_class); plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability_flag) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_w, mxREAL); ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = Malloc(struct feature_node, feature_number+2); for(instance_index=0;instance_index<testing_instance_number;instance_index++) { int i; double target_label, predict_label; target_label = ptr_label[instance_index]; // prhs[1] and prhs[1]^T are sparse read_sparse_instance(pplhs[0], instance_index, x, feature_number, model_->bias); if(predict_probability_flag) { predict_label = predict_probability(model_, x, prob_estimates); ptr_predict_label[instance_index] = predict_label; for(i=0;i<nr_class;i++) ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i]; } else { double *dec_values = Malloc(double, nr_class); predict_label = predict_values(model_, x, dec_values); ptr_predict_label[instance_index] = predict_label; for(i=0;i<nr_w;i++) ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i]; free(dec_values); } if(predict_label == target_label) ++correct; error += (predict_label-target_label)*(predict_label-target_label); sump += predict_label; sumt += target_label; sumpp += predict_label*predict_label; sumtt += target_label*target_label; sumpt += predict_label*target_label; ++total; } if(model_->param.solver_type==L2R_L2LOSS_SVR || model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || model_->param.solver_type==L2R_L2LOSS_SVR_DUAL) { mexPrintf("Mean squared error = %g (regression)\n",error/total); mexPrintf("Squared correlation coefficient = %g (regression)\n", ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)) ); } //else //mexPrintf("Accuracy = %g%% (%d/%d)\n", (double) correct/total*100,correct,total); // return accuracy, mean squared error, squared correlation coefficient plhs[1] = mxCreateDoubleMatrix(3, 1, mxREAL); ptr = mxGetPr(plhs[1]); ptr[0] = (double)correct/total*100; ptr[1] = error/total; ptr[2] = ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)); free(x); if(prob_estimates != NULL) free(prob_estimates); }
void do_predict(mxArray *plhs[], const mxArray *prhs[], struct model *model_, const int predict_probability_flag) { int label_vector_row_num, label_vector_col_num; int feature_number, testing_instance_number; int instance_index; double *ptr_instance, *ptr_label, *ptr_predict_label; double *ptr_prob_estimates, *ptr_dec_values, *ptr; struct feature_node *x; mxArray *pplhs[1]; // instance sparse matrix in row format int correct = 0; int total = 0; int nr_class=get_nr_class(model_); int nr_classifier; double *prob_estimates=NULL; if(nr_class==2) nr_classifier=1; else nr_classifier=nr_class; // prhs[1] = testing instance matrix feature_number = mxGetN(prhs[1]); testing_instance_number = mxGetM(prhs[1]); if(col_format_flag) { feature_number = mxGetM(prhs[1]); testing_instance_number = mxGetN(prhs[1]); } label_vector_row_num = mxGetM(prhs[0]); label_vector_col_num = mxGetN(prhs[0]); if(label_vector_row_num!=testing_instance_number) { mexPrintf("Length of label vector does not match # of instances.\n"); fake_answer(plhs); return; } if(label_vector_col_num!=1) { mexPrintf("label (1st argument) should be a vector (# of column is 1).\n"); fake_answer(plhs); return; } ptr_instance = mxGetPr(prhs[1]); ptr_label = mxGetPr(prhs[0]); // transpose instance matrix if(mxIsSparse(prhs[1])) { if(col_format_flag) { pplhs[0] = (mxArray *)prhs[1]; } else { mxArray *pprhs[1]; pprhs[0] = mxDuplicateArray(prhs[1]); if(mexCallMATLAB(1, pplhs, 1, pprhs, "transpose")) { mexPrintf("Error: cannot transpose testing instance matrix\n"); fake_answer(plhs); return; } } } else mexPrintf("Testing_instance_matrix must be sparse\n"); prob_estimates = Malloc(double, nr_class); plhs[0] = mxCreateDoubleMatrix(testing_instance_number, 1, mxREAL); if(predict_probability_flag) plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_class, mxREAL); else plhs[2] = mxCreateDoubleMatrix(testing_instance_number, nr_classifier, mxREAL); ptr_predict_label = mxGetPr(plhs[0]); ptr_prob_estimates = mxGetPr(plhs[2]); ptr_dec_values = mxGetPr(plhs[2]); x = Malloc(struct feature_node, feature_number+2); for(instance_index=0;instance_index<testing_instance_number;instance_index++) { int i; double target,v; target = ptr_label[instance_index]; // prhs[1] and prhs[1]^T are sparse read_sparse_instance(pplhs[0], instance_index, x, feature_number, model_->bias); if(predict_probability_flag) { v = predict_probability(model_, x, prob_estimates); ptr_predict_label[instance_index] = v; for(i=0;i<nr_class;i++) ptr_prob_estimates[instance_index + i * testing_instance_number] = prob_estimates[i]; } else { double *dec_values = Malloc(double, nr_class); v = predict(model_, x); ptr_predict_label[instance_index] = v; predict_values(model_, x, dec_values); for(i=0;i<nr_classifier;i++) ptr_dec_values[instance_index + i * testing_instance_number] = dec_values[i]; } if(v == target) ++correct; ++total; } mexPrintf("Accuracy = %g%% (%d/%d)\n", (double)correct/total*100,correct,total); // return accuracy, mean squared error, squared correlation coefficient plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL); ptr = mxGetPr(plhs[1]); ptr[0] = (double)correct/total*100; free(x); if(prob_estimates != NULL) free(prob_estimates); }
double binary_class_cross_validation(const problem *prob, const parameter *param, int nr_fold) { dvec_t dec_values; ivec_t ty; int *labels; if (nr_fold > 1) { int i; int *fold_start = Malloc(int,nr_fold+1); int l = prob->l; int *perm = Malloc(int,l); for(i=0;i<l;i++) perm[i]=i; for(i=0;i<l;i++) { int j = i+rand()%(l-i); std::swap(perm[i],perm[j]); } for(i=0;i<=nr_fold;i++) fold_start[i]=i*l/nr_fold; for(i=0;i<nr_fold;i++) { int begin = fold_start[i]; int end = fold_start[i+1]; int j,k; struct problem subprob; subprob.l = l-(end-begin); subprob.x = Malloc(struct feature_node*,subprob.l); subprob.y = Malloc(int,subprob.l); k=0; for(j=0;j<begin;j++) { subprob.x[k] = prob->x[perm[j]]; subprob.y[k] = prob->y[perm[j]]; ++k; } for(j=end;j<l;j++) { subprob.x[k] = prob->x[perm[j]]; subprob.y[k] = prob->y[perm[j]]; ++k; } struct model *submodel = train(&subprob,param); //int svm_type = get_svm_type(submodel); //if(svm_type == NU_SVR || svm_type == EPSILON_SVR){ // fprintf(stderr, "wrong svm type"); // exit(1); //} labels = Malloc(int, get_nr_class(submodel)); get_labels(submodel, labels); if(get_nr_class(submodel) > 2) { fprintf(stderr,"Error: the number of class is not equal to 2\n"); exit(-1); } dec_values.resize(end); ty.resize(end); for(j=begin;j<end;j++) { predict_values(submodel,prob->x[perm[j]], &dec_values[j]); ty[j] = (prob->y[perm[j]] > 0)? 1: -1; } if(labels[0] <= 0) { for(j=begin;j<end;j++) dec_values[j] *= -1; } free_and_destroy_model(&submodel); free(subprob.x); free(subprob.y); free(labels); } free(perm); free(fold_start); }
void do_predict(FILE *input, FILE *output, FILE *output2) { int correct = 0; int total = 0; double error = 0; double sump = 0, sumt = 0, sumpp = 0, sumtt = 0, sumpt = 0; int nr_class=get_nr_class(model_); double *dec_values = (double *) malloc(nr_class*sizeof(double)); double prob = 0; double *prob_estimates=NULL; int j, n; int nr_feature=get_nr_feature(model_); if(model_->bias>=0) n=nr_feature+1; else n=nr_feature; if(flag_predict_probability) { int *labels; if(!check_probability_model(model_)) { fprintf(stderr, "probability output is only supported for logistic regression\n"); exit(1); } labels=(int *) malloc(nr_class*sizeof(int)); get_labels(model_,labels); prob_estimates = (double *) malloc(nr_class*sizeof(double)); fprintf(output,"labels"); for(j=0;j<nr_class;j++) fprintf(output," %d",labels[j]); fprintf(output,"\n"); free(labels); } max_line_len = 1024; line = (char *)malloc(max_line_len*sizeof(char)); while(readline(input) != NULL) { int i = 0; double target_label, predict_label; char *idx, *val, *label, *endptr; int inst_max_index = 0; // strtol gives 0 if wrong format label = strtok(line," \t\n"); if(label == NULL) // empty line exit_input_error(total+1); target_label = strtod(label,&endptr); if(endptr == label || *endptr != '\0') exit_input_error(total+1); while(1) { if(i>=max_nr_attr-2) // need one more for index = -1 { max_nr_attr *= 2; x = (struct feature_node *) realloc(x,max_nr_attr*sizeof(struct feature_node)); } idx = strtok(NULL,":"); val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; x[i].index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) exit_input_error(total+1); else inst_max_index = x[i].index; errno = 0; x[i].value = strtod(val,&endptr); if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) exit_input_error(total+1); // feature indices larger than those in training are not used if(x[i].index <= nr_feature) ++i; } if(model_->bias>=0) { x[i].index = n; x[i].value = model_->bias; i++; } x[i].index = -1; if(flag_predict_probability) { int j; predict_label = predict_probability(model_,x,prob_estimates); fprintf(output,"%g",predict_label); for(j=0;j<model_->nr_class;j++) fprintf(output," %g",prob_estimates[j]); fprintf(output,"\n"); } else { predict_label=predict_values(model_, x, dec_values); fprintf(output,"%g\n",predict_label); //transfer the score into probability prob = sigmoid_predict(dec_values[0], model_->probA, model_->probB); fprintf(output2,"%4g\n",prob); } if(predict_label == target_label) ++correct; error += (predict_label-target_label)*(predict_label-target_label); sump += predict_label; sumt += target_label; sumpp += predict_label*predict_label; sumtt += target_label*target_label; sumpt += predict_label*target_label; ++total; } if(model_->param.solver_type==L2R_L2LOSS_SVR || model_->param.solver_type==L2R_L1LOSS_SVR_DUAL || model_->param.solver_type==L2R_L2LOSS_SVR_DUAL) { info("Mean squared error = %g (regression)\n",error/total); info("Squared correlation coefficient = %g (regression)\n", ((total*sumpt-sump*sumt)*(total*sumpt-sump*sumt))/ ((total*sumpp-sump*sump)*(total*sumtt-sumt*sumt)) ); } else info("Accuracy = %g%% (%d/%d)\n",(double) correct/total*100,correct,total); if(flag_predict_probability) free(prob_estimates); free(dec_values); }
void binary_class_predict(FILE *input, FILE *output){ int total = 0; int *labels; int max_nr_attr = 64; struct feature_node *x = Malloc(struct feature_node, max_nr_attr); dvec_t dec_values; ivec_t true_labels; int n; if(model_->bias >= 1) n = get_nr_feature(model_) + 1; else n = get_nr_feature(model_); labels = Malloc(int, get_nr_class(model_)); get_labels(model_, labels); max_line_len = 1024; line = (char *)malloc(max_line_len*sizeof(char)); while(readline(input) != NULL) { int i = 0; double target_label, predict_label; char *idx, *val, *label, *endptr; int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0 label = strtok(line," \t"); target_label = strtod(label,&endptr); if(endptr == label) exit_input_error(total+1); while(1) { if(i>=max_nr_attr - 2) // need one more for index = -1 { max_nr_attr *= 2; x = (struct feature_node *) realloc(x,max_nr_attr*sizeof(struct feature_node)); } idx = strtok(NULL,":"); val = strtok(NULL," \t"); if(val == NULL) break; errno = 0; x[i].index = (int) strtol(idx,&endptr,10); if(endptr == idx || errno != 0 || *endptr != '\0' || x[i].index <= inst_max_index) exit_input_error(total+1); else inst_max_index = x[i].index; errno = 0; x[i].value = strtod(val,&endptr); if(endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr))) exit_input_error(total+1); ++i; } if(model_->bias >= 0){ x[i].index = n; x[i].value = model_->bias; ++i; } x[i].index = -1; predict_label = predict(model_,x); fprintf(output,"%g\n",predict_label); double dec_value; predict_values(model_, x, &dec_value); true_labels.push_back((target_label > 0)? 1: -1); if(labels[0] <= 0) dec_value *= -1; dec_values.push_back(dec_value); } validation_function(dec_values, true_labels); free(labels); free(x); }