Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/evaluate.c
3206 views
/*!1\file evaluate.c2\brief Various routines to evaluate classification performance34\author George5\date 9/23/20086\version\verbatim $Id: evaluate.c 13328 2012-12-31 14:57:40Z karypis $ \endverbatim7*/89#include <GKlib.h>1011/**********************************************************************12* This function computes the max accuracy score of a ranked list,13* given +1/-1 class list14**********************************************************************/15float ComputeAccuracy(int n, gk_fkv_t *list)16{17int i, P, N, TP, FN = 0;18float bAccuracy = 0.0;19float acc;2021for (P=0, i=0;i<n;i++)22P += (list[i].val == 1? 1 : 0);23N = n - P;2425TP = FN = 0;2627for(i=0; i<n; i++){28if (list[i].val == 1)29TP++;30else31FN++;3233acc = (TP + N - FN) * 100.0/ (P + N) ;34if (acc > bAccuracy)35bAccuracy = acc;36}3738return bAccuracy;39}404142/*****************************************************************************43* This function computes the ROC score of a ranked list, given a +1/-1 class44* list.45******************************************************************************/46float ComputeROCn(int n, int maxN, gk_fkv_t *list)47{48int i, P, TP, FP, TPprev, FPprev, AUC;49float prev;5051FP = TP = FPprev = TPprev = AUC = 0;52prev = list[0].key -1;5354for (P=0, i=0; i<n; i++)55P += (list[i].val == 1 ? 1 : 0);5657for (i=0; i<n && FP < maxN; i++) {58if (list[i].key != prev) {59AUC += (TP+TPprev)*(FP-FPprev)/2;60prev = list[i].key;61FPprev = FP;62TPprev = TP;63}64if (list[i].val == 1)65TP++;66else {67FP++;68}69}70AUC += (TP+TPprev)*(FP-FPprev)/2;7172return (TP*FP > 0 ? (float)(1.0*AUC/(P*FP)) : 0.0);73}747576/*****************************************************************************77* This function computes the median rate of false positive for each positive78* instance.79******************************************************************************/80float ComputeMedianRFP(int n, gk_fkv_t *list)81{82int i, P, N, TP, FP;8384P = N = 0;85for (i=0; i<n; i++) {86if (list[i].val == 1)87P++;88else89N++;90}9192FP = TP = 0;93for (i=0; i<n && TP < (P+1)/2; i++) {94if (list[i].val == 1)95TP++;96else97FP++;98}99100return 1.0*FP/N;101}102103/*********************************************************104* Compute the mean105********************************************************/106float ComputeMean (int n, float *values)107{108int i;109float mean = 0.0;110111for(i=0; i < n; i++)112mean += values[i];113114return 1.0 * mean/ n;115}116117/********************************************************118* Compute the standard deviation119********************************************************/120float ComputeStdDev(int n, float *values)121{122int i;123float mean = ComputeMean(n, values);124float stdDev = 0;125126for(i=0;i<n;i++){127stdDev += (values[i] - mean)* (values[i] - mean);128}129130return sqrt(1.0 * stdDev/n);131}132133134