Last active
February 5, 2018 16:20
-
-
Save ikegami-yukino/3341000 to your computer and use it in GitHub Desktop.
LIBLINEAR1.93のcross validationオプションでprecision/recallを出力する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- train.c 2012-10-29 01:46:32.000000000 +0900 | |
+++ train-new.c 2013-02-12 19:53:21.000000000 +0900 | |
@@ -135,10 +135,14 @@ | |
void do_cross_validation() | |
{ | |
int i; | |
- int total_correct = 0; | |
double total_error = 0; | |
double sumv = 0, sumy = 0, sumvv = 0, sumyy = 0, sumvy = 0; | |
double *target = Malloc(double, prob.l); | |
+ //** To caluculate precision/recall for each class **/ | |
+ int tp = 0; | |
+ int fp = 0; | |
+ int tn = 0; | |
+ int fn = 0; | |
cross_validation(&prob,¶m,nr_fold,target); | |
if(param.solver_type == L2R_L2LOSS_SVR || | |
@@ -164,10 +168,42 @@ | |
} | |
else | |
{ | |
- for(i=0;i<prob.l;i++) | |
- if(target[i] == prob.y[i]) | |
- ++total_correct; | |
- printf("Cross Validation Accuracy = %g%%\n",100.0*total_correct/prob.l); | |
+ for(i=0;i<prob.l; i++) { | |
+ if(prob.y[i] == 1) { // True label = +1 | |
+ if(target[i] == prob.y[i]) { | |
+ tp++; | |
+ } else { | |
+ fp++; | |
+ } | |
+ } else { // True label = -1 | |
+ if (target[i] == prob.y[i]) { | |
+ tn++; | |
+ } else { | |
+ fn++; | |
+ } | |
+ } | |
+ } | |
+ | |
+ printf("Cross Validation Accuracy = %g%%\n",100.0 * ((double)(tp + tn) / (double)(tp + fp + tn + fn)) ); | |
+ | |
+ // Precision and recall | |
+ double pos_prec = ((double)tp/(double)(tp + fp)); | |
+ double pos_rec = ((double)tp/(double)(tp + fn)); | |
+ double pos_f1 = (2 * pos_prec * pos_rec) / (pos_prec + pos_rec); | |
+ | |
+ double neg_prec = ((double)tn/(double)(tn + fn)); | |
+ double neg_rec = ((double)tn/(double)(tn + fp)); | |
+ double neg_f1 = (2 * neg_prec * neg_rec) / (neg_prec + neg_rec); | |
+ | |
+ printf("Positive (+1) class:\n"); | |
+ printf(" precision = %g\n", pos_prec ); | |
+ printf(" recall = %g\n", pos_rec ); | |
+ printf(" F1 value = %g\n\n", pos_f1 ); | |
+ | |
+ printf("Negative (-1) class:\n"); | |
+ printf(" precision = %g\n", neg_prec ); | |
+ printf(" recall = %g\n", neg_rec ); | |
+ printf(" F1 value = %g\n\n", neg_f1 ); | |
} | |
free(target); | |
@@ -397,3 +433,4 @@ | |
fclose(fp); | |
} | |
+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment