SHOGUN
v1.1.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Sergey Lisitsyn 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 00011 #ifndef CONTINGENCYTABLEEVALUATION_H_ 00012 #define CONTINGENCYTABLEEVALUATION_H_ 00013 00014 #include <shogun/evaluation/BinaryClassEvaluation.h> 00015 #include <shogun/features/Labels.h> 00016 #include <shogun/mathematics/Math.h> 00017 #include <shogun/io/SGIO.h> 00018 00019 namespace shogun 00020 { 00021 00022 class CLabels; 00023 00025 enum EContingencyTableMeasureType 00026 { 00027 ACCURACY = 0, 00028 ERROR_RATE = 10, 00029 BAL = 20, 00030 WRACC = 30, 00031 F1 = 40, 00032 CROSS_CORRELATION = 50, 00033 RECALL = 60, 00034 PRECISION = 70, 00035 SPECIFICITY = 80 00036 }; 00037 00069 class CContingencyTableEvaluation: public CBinaryClassEvaluation 00070 { 00071 00072 public: 00073 00075 CContingencyTableEvaluation() : 00076 CBinaryClassEvaluation(), m_type(ACCURACY), m_computed(false) {}; 00077 00081 CContingencyTableEvaluation(EContingencyTableMeasureType type) : 00082 CBinaryClassEvaluation(), m_type(type), m_computed(false) {}; 00083 00085 virtual ~CContingencyTableEvaluation() {}; 00086 00092 virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); 00093 00094 EEvaluationDirection get_evaluation_direction(); 00095 00097 virtual inline const char* get_name() const 00098 { 00099 return "ContingencyTableEvaluation"; 00100 } 00101 00105 inline float64_t get_accuracy() const 00106 { 00107 if (!m_computed) 00108 SG_ERROR("Uninitialized, please call evaluate first"); 00109 00110 return (m_TP+m_TN)/m_N; 00111 }; 00112 00116 inline float64_t get_error_rate() const 00117 { 00118 if (!m_computed) 00119 SG_ERROR("Uninitialized, please call evaluate first"); 00120 00121 return (m_FP + m_FN)/m_N; 00122 }; 00123 00127 inline float64_t get_BAL() const 00128 { 00129 if (!m_computed) 00130 SG_ERROR("Uninitialized, please call evaluate first"); 00131 00132 return 0.5*(m_FN/(m_FN + m_TP) + m_FP/(m_FP + m_TN)); 00133 }; 00134 00138 inline float64_t get_WRACC() const 00139 { 00140 if (!m_computed) 00141 SG_ERROR("Uninitialized, please call evaluate first"); 00142 00143 return m_TP/(m_FN + m_TP) - m_FP/(m_FP + m_TN); 00144 }; 00145 00149 inline float64_t get_F1() const 00150 { 00151 if (!m_computed) 00152 SG_ERROR("Uninitialized, please call evaluate first"); 00153 00154 return (2*m_TP)/(2*m_TP + m_FP + m_FN); 00155 }; 00156 00160 inline float64_t get_cross_correlation() const 00161 { 00162 if (!m_computed) 00163 SG_ERROR("Uninitialized, please call evaluate first"); 00164 00165 return (m_TP*m_TN-m_FP*m_FN)/CMath::sqrt((m_TP+m_FP)*(m_TP+m_FN)*(m_TN+m_FP)*(m_TN+m_FN)); 00166 }; 00167 00171 inline float64_t get_recall() const 00172 { 00173 if (!m_computed) 00174 SG_ERROR("Uninitialized, please call evaluate first"); 00175 00176 return m_TP/(m_TP+m_FN); 00177 }; 00178 00182 inline float64_t get_precision() const 00183 { 00184 if (!m_computed) 00185 SG_ERROR("Uninitialized, please call evaluate first"); 00186 00187 return m_TP/(m_TP+m_FP); 00188 }; 00189 00193 inline float64_t get_specificity() const 00194 { 00195 if (!m_computed) 00196 SG_ERROR("Uninitialized, please call evaluate first"); 00197 00198 return m_TN/(m_TN+m_FP); 00199 }; 00200 00201 protected: 00202 00204 void compute_scores(CLabels* predicted, CLabels* ground_truth); 00205 00207 EContingencyTableMeasureType m_type; 00208 00210 bool m_computed; 00211 00213 int32_t m_N; 00214 00216 float64_t m_TP; 00217 00219 float64_t m_FP; 00220 00222 float64_t m_TN; 00223 00225 float64_t m_FN; 00226 }; 00227 00237 class CAccuracyMeasure: public CContingencyTableEvaluation 00238 { 00239 public: 00240 /* constructor */ 00241 CAccuracyMeasure() : CContingencyTableEvaluation(ACCURACY) {}; 00242 /* virtual destructor */ 00243 virtual ~CAccuracyMeasure() {}; 00244 /* name */ 00245 virtual inline const char* get_name() const { return "AccuracyMeasure"; }; 00246 }; 00247 00257 class CErrorRateMeasure: public CContingencyTableEvaluation 00258 { 00259 public: 00260 /* constructor */ 00261 CErrorRateMeasure() : CContingencyTableEvaluation(ERROR_RATE) {}; 00262 /* virtual destructor */ 00263 virtual ~CErrorRateMeasure() {}; 00264 /* name */ 00265 virtual inline const char* get_name() const { return "ErrorRateMeasure"; }; 00266 }; 00267 00277 class CBALMeasure: public CContingencyTableEvaluation 00278 { 00279 public: 00280 /* constructor */ 00281 CBALMeasure() : CContingencyTableEvaluation(BAL) {}; 00282 /* virtual destructor */ 00283 virtual ~CBALMeasure() {}; 00284 /* name */ 00285 virtual inline const char* get_name() const { return "BALMeasure"; }; 00286 }; 00287 00297 class CWRACCMeasure: public CContingencyTableEvaluation 00298 { 00299 public: 00300 /* constructor */ 00301 CWRACCMeasure() : CContingencyTableEvaluation(WRACC) {}; 00302 /* virtual destructor */ 00303 virtual ~CWRACCMeasure() {}; 00304 /* name */ 00305 virtual inline const char* get_name() const { return "WRACCMeasure"; }; 00306 }; 00307 00317 class CF1Measure: public CContingencyTableEvaluation 00318 { 00319 public: 00320 /* constructor */ 00321 CF1Measure() : CContingencyTableEvaluation(F1) {}; 00322 /* virtual destructor */ 00323 virtual ~CF1Measure() {}; 00324 /* name */ 00325 virtual inline const char* get_name() const { return "F1Measure"; }; 00326 }; 00327 00337 class CCrossCorrelationMeasure: public CContingencyTableEvaluation 00338 { 00339 public: 00340 /* constructor */ 00341 CCrossCorrelationMeasure() : CContingencyTableEvaluation(CROSS_CORRELATION) {}; 00342 /* virtual destructor */ 00343 virtual ~CCrossCorrelationMeasure() {}; 00344 /* name */ 00345 virtual inline const char* get_name() const { return "CrossCorrelationMeasure"; }; 00346 }; 00347 00357 class CRecallMeasure: public CContingencyTableEvaluation 00358 { 00359 public: 00360 /* constructor */ 00361 CRecallMeasure() : CContingencyTableEvaluation(RECALL) {}; 00362 /* virtual destructor */ 00363 virtual ~CRecallMeasure() {}; 00364 /* name */ 00365 virtual inline const char* get_name() const { return "RecallMeasure"; }; 00366 }; 00367 00377 class CPrecisionMeasure: public CContingencyTableEvaluation 00378 { 00379 public: 00380 /* constructor */ 00381 CPrecisionMeasure() : CContingencyTableEvaluation(PRECISION) {}; 00382 /* virtual destructor */ 00383 virtual ~CPrecisionMeasure() {}; 00384 /* name */ 00385 virtual inline const char* get_name() const { return "PrecisionMeasure"; }; 00386 }; 00387 00397 class CSpecificityMeasure: public CContingencyTableEvaluation 00398 { 00399 public: 00400 /* constructor */ 00401 CSpecificityMeasure() : CContingencyTableEvaluation(SPECIFICITY) {}; 00402 /* virtual destructor */ 00403 virtual ~CSpecificityMeasure() {}; 00404 /* name */ 00405 virtual inline const char* get_name() const { return "SpecificityMeasure"; }; 00406 }; 00407 00408 } 00409 00410 00411 #endif /* CONTINGENCYTABLEEVALUATION_H_ */