ContingencyTableEvaluation.cpp

Go to the documentation of this file.
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         case CUSTOM:
00048             return get_custom_score();
00049     }
00050 
00051     SG_NOTIMPLEMENTED;
00052     return 42;
00053 }
00054 
00055 inline EEvaluationDirection CContingencyTableEvaluation::get_evaluation_direction()
00056 {
00057     switch (m_type)
00058     {
00059     case ACCURACY:
00060         return ED_MAXIMIZE;
00061     case ERROR_RATE:
00062         return ED_MINIMIZE;
00063     case BAL:
00064         return ED_MINIMIZE;
00065     case WRACC:
00066         return ED_MAXIMIZE;
00067     case F1:
00068         return ED_MAXIMIZE;
00069     case CROSS_CORRELATION:
00070         return ED_MAXIMIZE;
00071     case RECALL:
00072         return ED_MAXIMIZE;
00073     case PRECISION:
00074         return ED_MAXIMIZE;
00075     case SPECIFICITY:
00076         return ED_MAXIMIZE;
00077     case CUSTOM:
00078         return get_custom_direction();
00079     default:
00080         SG_NOTIMPLEMENTED;
00081     }
00082 
00083     return ED_MINIMIZE;
00084 }
00085 
00086 void CContingencyTableEvaluation::compute_scores(CBinaryLabels* predicted, CBinaryLabels* ground_truth)
00087 {
00088     ASSERT(ground_truth->get_label_type() == LT_BINARY);
00089     ASSERT(predicted->get_label_type() == LT_BINARY);
00090 
00091     if (predicted->get_num_labels()!=ground_truth->get_num_labels())
00092     {
00093         SG_ERROR("%s::compute_scores(): Number of predicted labels (%d) is not "
00094                 "equal to number of ground truth labels (%d)!\n", get_name(),
00095                 predicted->get_num_labels(), ground_truth->get_num_labels());
00096     }
00097     m_TP = 0.0;
00098     m_FP = 0.0;
00099     m_TN = 0.0;
00100     m_FN = 0.0;
00101     m_N = predicted->get_num_labels();
00102 
00103     for (int i=0; i<predicted->get_num_labels(); i++)
00104     {
00105         if (ground_truth->get_label(i)==1)
00106         {
00107             if (predicted->get_label(i)==1)
00108                 m_TP += 1.0;
00109             else
00110                 m_FN += 1.0;
00111         }
00112         else
00113         {
00114             if (predicted->get_label(i)==1)
00115                 m_FP += 1.0;
00116             else
00117                 m_TN += 1.0;
00118         }
00119     }
00120     m_computed = true;
00121 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation