📄 statlib.c
字号:
/** * @file * Test statistics. * Calculates statistics and print plot data. * * @author Mikael Ylikoski * @date 2001-2002 */#include <stdio.h>#include <string.h>#include "statlib.h"#include "utility.h"/** * Box statistics. */typedef struct { int notm; /**< Number Of Trained Messages */ int nopm; /**< Number Of classified messages */ int *stats; /**< Classification statistics */ int tp; /**< a = messages correctly assigned to it */ int fp; /**< b = messages incorrectly assigned to it */ int fn; /**< c = messages incorrectly rejected from it */ int tn; /**< d = messages correctly rejected from it */} box_stat;/** * Classifier statistics. */typedef struct { int nocm; /**< Number Of Correctly classified Messages */ int nolm; /**< Number Of correctly Limited Messages */ int nofm; /**< Number Of Falsely classified Messages */} cls_stat;static enum plotting_mode plot_mode; /**< Plotting mode */static cls_stat *cls_stats; /**< Classifier statistics */static int noc; /**< Number Of Classifiers *///static box_stat *box_stats; /**< Box statistics */static int nob; /**< Number Of Boxes */static int plot_c; /**< Plot classifier */static int notm; /**< Number Of Trained Messages */static int *n_list; /**< N-average list */static int mean_n; /**< Number of steps in average */static int non; /**< Number Of N-average messages */static int nocn; /**< Number Of Correctly classified N-average messages */static int rank_limit; /**< Threshold for accepted rank *//** * Update N-average statistics. * * @param b 1 for correct classification, 0 otherwise * @return Zero if ok, non-zero otherwise. */static intupdate_n_stat (int b) { int i; if (plot_mode != N_AVERAGE) return 0; if (notm <= mean_n) non = notm; else non = mean_n; if (b) b = 1; i = (notm - 1) % mean_n; if (n_list[i] != b) { n_list[i] = b; if (b) nocn++; else nocn--; } return 0;}/** * Update statistics for new test message. * * @param td test data * @return Zero if ok, or nonzero otherwise. */intstatlib_update_stats (test_data *td) { int i, k, ok; if (td->bno == -1 || td->mno == -1) return -1; if (td->nor > noc) { //cls_stats = my_realloc (cls_stats, td->nor * sizeof(cls_stat)); fprintf (stderr, "Error: Wrong number of classifiers.\n"); return -1; } notm++; ok = 0; for (k = 0; k < td->nor && k < noc; k++) { switch (td->res[k].cm) { case RANK: if (td->res[k].len < 1) break; for (i = 0; i < rank_limit && i < td->res[k].len; i++) if (td->res[k].u.rlist[i] == td->bno) { cls_stats[k].nolm++; if (k == plot_c) ok = 1; break; } if (td->res[k].u.rlist[0] == td->bno) cls_stats[k].nocm++; else cls_stats[k].nofm++; break; case SCORE: return -1; } } update_n_stat (ok); return 0;}/** * Print header. */voidstatlib_print_plot_header (void) { printf ("# Process this data with gnuplot to create a plot.\n" "# gnuplot> plot \"datafile\" notitle with lines\n");}/** * Print current data for plot. */voidstatlib_print_plot_data (void) { printf ("%d ", notm); switch (plot_mode) { case TOTAL: printf ("%.3f\n", cls_stats[plot_c].nolm / (double)notm); break; case N_AVERAGE: printf ("%.3f\n", nocn / (double)non); break; }}/** * Print statistics. */voidstatlib_print_results (void) { int i, j; for (i = 0; i < noc; i++) { printf ("## Classifier %d", i); if (i == plot_c) printf (" (plotted)"); printf ("\n"); j = cls_stats[i].nocm + cls_stats[i].nofm; printf ("## Correct: %d Top-%d-Correct: %d Covered: %d " "Total: %d\n", cls_stats[i].nocm, rank_limit, cls_stats[i].nolm, j, notm); if (j) printf ("## Accuracy: %.3f Top-%d-Accuracy: %.3f " "Coverage: %.3f\n", cls_stats[i].nocm / (double)j, rank_limit, cls_stats[i].nolm / (double)j, j / (double)notm); else printf ("## Accuracy: 0 Top-%d-Accuracy: 0 " "Coverage: 0\n", rank_limit); printf ("## Total-Accuracy: %.3f Total-Top-%d-Accuracy: %.3f\n", cls_stats[i].nocm / (double)notm, rank_limit, cls_stats[i].nolm / (double)notm); }}/** * Initialization. * * @return Zero if ok, or nonzero otherwise. */intstatlib_initialize (enum plotting_mode in_plm, int in_rklm, int in_mean, int in_plotc, int in_noc, int in_nob) { int i; /* Configuration */ plot_mode = in_plm; rank_limit = in_rklm; mean_n = in_mean; plot_c = in_plotc; noc = in_noc; nob = in_nob; /* Allocate statistics memory */ cls_stats = my_malloc (noc * sizeof(cls_stat)); for (i = 0; i < noc; i++) { cls_stats[i].nocm = 0; cls_stats[i].nolm = 0; cls_stats[i].nofm = 0; } if (plot_mode == N_AVERAGE) n_list = my_calloc (mean_n, sizeof(int)); non = 0; nocn = 0; notm = 0; return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -