SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ContingencyTableEvaluation.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Sergey Lisitsyn
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
13 
14 using namespace shogun;
15 
17 {
18  ASSERT(predicted->get_label_type()==LT_BINARY);
19  ASSERT(ground_truth->get_label_type()==LT_BINARY);
20 
21  /* commented out: what if a machine only returns +1 in apply() ??
22  * Heiko Strathamn */
23 // predicted->ensure_valid();
24 
25  ground_truth->ensure_valid();
26  compute_scores((CBinaryLabels*)predicted,(CBinaryLabels*)ground_truth);
27  switch (m_type)
28  {
29  case ACCURACY:
30  return get_accuracy();
31  case ERROR_RATE:
32  return get_error_rate();
33  case BAL:
34  return get_BAL();
35  case WRACC:
36  return get_WRACC();
37  case F1:
38  return get_F1();
39  case CROSS_CORRELATION:
40  return get_cross_correlation();
41  case RECALL:
42  return get_recall();
43  case PRECISION:
44  return get_precision();
45  case SPECIFICITY:
46  return get_specificity();
47  }
48 
50  return 42;
51 }
52 
54 {
55  switch (m_type)
56  {
57  case ACCURACY:
58  return ED_MAXIMIZE;
59  case ERROR_RATE:
60  return ED_MINIMIZE;
61  case BAL:
62  return ED_MINIMIZE;
63  case WRACC:
64  return ED_MAXIMIZE;
65  case F1:
66  return ED_MAXIMIZE;
67  case CROSS_CORRELATION:
68  return ED_MAXIMIZE;
69  case RECALL:
70  return ED_MAXIMIZE;
71  case PRECISION:
72  return ED_MAXIMIZE;
73  case SPECIFICITY:
74  return ED_MAXIMIZE;
75  default:
77  }
78 
79  return ED_MINIMIZE;
80 }
81 
83 {
84  ASSERT(ground_truth->get_label_type() == LT_BINARY);
85  ASSERT(predicted->get_label_type() == LT_BINARY);
86 
87  if (predicted->get_num_labels()!=ground_truth->get_num_labels())
88  {
89  SG_ERROR("%s::compute_scores(): Number of predicted labels (%d) is not "
90  "equal to number of ground truth labels (%d)!\n", get_name(),
91  predicted->get_num_labels(), ground_truth->get_num_labels());
92  }
93  m_TP = 0.0;
94  m_FP = 0.0;
95  m_TN = 0.0;
96  m_FN = 0.0;
97  m_N = predicted->get_num_labels();
98 
99  for (int i=0; i<predicted->get_num_labels(); i++)
100  {
101  if (ground_truth->get_label(i)==1)
102  {
103  if (predicted->get_label(i)==1)
104  m_TP += 1.0;
105  else
106  m_FN += 1.0;
107  }
108  else
109  {
110  if (predicted->get_label(i)==1)
111  m_FP += 1.0;
112  else
113  m_TN += 1.0;
114  }
115  }
116  m_computed = true;
117 }

SHOGUN Machine Learning Toolbox - Documentation