SHOGUN  v3.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  case CUSTOM:
48  return get_custom_score();
49  }
50 
52  return 42;
53 }
54 
56 {
57  switch (m_type)
58  {
59  case ACCURACY:
60  return ED_MAXIMIZE;
61  case ERROR_RATE:
62  return ED_MINIMIZE;
63  case BAL:
64  return ED_MINIMIZE;
65  case WRACC:
66  return ED_MAXIMIZE;
67  case F1:
68  return ED_MAXIMIZE;
69  case CROSS_CORRELATION:
70  return ED_MAXIMIZE;
71  case RECALL:
72  return ED_MAXIMIZE;
73  case PRECISION:
74  return ED_MAXIMIZE;
75  case SPECIFICITY:
76  return ED_MAXIMIZE;
77  case CUSTOM:
78  return get_custom_direction();
79  default:
81  }
82 
83  return ED_MINIMIZE;
84 }
85 
87 {
88  ASSERT(ground_truth->get_label_type() == LT_BINARY)
89  ASSERT(predicted->get_label_type() == LT_BINARY)
90 
91  if (predicted->get_num_labels()!=ground_truth->get_num_labels())
92  {
93  SG_ERROR("%s::compute_scores(): Number of predicted labels (%d) is not "
94  "equal to number of ground truth labels (%d)!\n", get_name(),
95  predicted->get_num_labels(), ground_truth->get_num_labels());
96  }
97  m_TP = 0.0;
98  m_FP = 0.0;
99  m_TN = 0.0;
100  m_FN = 0.0;
101  m_N = predicted->get_num_labels();
102 
103  for (int i=0; i<predicted->get_num_labels(); i++)
104  {
105  if (ground_truth->get_label(i)==1)
106  {
107  if (predicted->get_label(i)==1)
108  m_TP += 1.0;
109  else
110  m_FN += 1.0;
111  }
112  else
113  {
114  if (predicted->get_label(i)==1)
115  m_FP += 1.0;
116  else
117  m_TN += 1.0;
118  }
119  }
120  m_computed = true;
121 }

SHOGUN Machine Learning Toolbox - Documentation