SHOGUN
v2.0.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 #include <shogun/evaluation/ContingencyTableEvaluation.h> 00012 #include <shogun/labels/BinaryLabels.h> 00013 00014 using namespace shogun; 00015 00016 float64_t CContingencyTableEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) 00017 { 00018 ASSERT(predicted->get_label_type()==LT_BINARY); 00019 ASSERT(ground_truth->get_label_type()==LT_BINARY); 00020 00021 /* commented out: what if a machine only returns +1 in apply() ?? 00022 * Heiko Strathamn */ 00023 // predicted->ensure_valid(); 00024 00025 ground_truth->ensure_valid(); 00026 compute_scores((CBinaryLabels*)predicted,(CBinaryLabels*)ground_truth); 00027 switch (m_type) 00028 { 00029 case ACCURACY: 00030 return get_accuracy(); 00031 case ERROR_RATE: 00032 return get_error_rate(); 00033 case BAL: 00034 return get_BAL(); 00035 case WRACC: 00036 return get_WRACC(); 00037 case F1: 00038 return get_F1(); 00039 case CROSS_CORRELATION: 00040 return get_cross_correlation(); 00041 case RECALL: 00042 return get_recall(); 00043 case PRECISION: 00044 return get_precision(); 00045 case SPECIFICITY: 00046 return get_specificity(); 00047 } 00048 00049 SG_NOTIMPLEMENTED; 00050 return 42; 00051 } 00052 00053 inline EEvaluationDirection CContingencyTableEvaluation::get_evaluation_direction() 00054 { 00055 switch (m_type) 00056 { 00057 case ACCURACY: 00058 return ED_MAXIMIZE; 00059 case ERROR_RATE: 00060 return ED_MINIMIZE; 00061 case BAL: 00062 return ED_MINIMIZE; 00063 case WRACC: 00064 return ED_MAXIMIZE; 00065 case F1: 00066 return ED_MAXIMIZE; 00067 case CROSS_CORRELATION: 00068 return ED_MAXIMIZE; 00069 case RECALL: 00070 return ED_MAXIMIZE; 00071 case PRECISION: 00072 return ED_MAXIMIZE; 00073 case SPECIFICITY: 00074 return ED_MAXIMIZE; 00075 default: 00076 SG_NOTIMPLEMENTED; 00077 } 00078 00079 return ED_MINIMIZE; 00080 } 00081 00082 void CContingencyTableEvaluation::compute_scores(CBinaryLabels* predicted, CBinaryLabels* ground_truth) 00083 { 00084 ASSERT(ground_truth->get_label_type() == LT_BINARY); 00085 ASSERT(predicted->get_label_type() == LT_BINARY); 00086 00087 if (predicted->get_num_labels()!=ground_truth->get_num_labels()) 00088 { 00089 SG_ERROR("%s::compute_scores(): Number of predicted labels (%d) is not " 00090 "equal to number of ground truth labels (%d)!\n", get_name(), 00091 predicted->get_num_labels(), ground_truth->get_num_labels()); 00092 } 00093 m_TP = 0.0; 00094 m_FP = 0.0; 00095 m_TN = 0.0; 00096 m_FN = 0.0; 00097 m_N = predicted->get_num_labels(); 00098 00099 for (int i=0; i<predicted->get_num_labels(); i++) 00100 { 00101 if (ground_truth->get_label(i)==1) 00102 { 00103 if (predicted->get_label(i)==1) 00104 m_TP += 1.0; 00105 else 00106 m_FN += 1.0; 00107 } 00108 else 00109 { 00110 if (predicted->get_label(i)==1) 00111 m_FP += 1.0; 00112 else 00113 m_TN += 1.0; 00114 } 00115 } 00116 m_computed = true; 00117 }