SHOGUN  v3.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROCEvaluation.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 }
19 
21 {
22  return evaluate_roc(predicted,ground_truth);
23 }
24 
26 {
27  ASSERT(predicted && ground_truth)
28  ASSERT(predicted->get_num_labels()==ground_truth->get_num_labels())
29  ASSERT(predicted->get_label_type()==LT_BINARY)
30  ASSERT(ground_truth->get_label_type()==LT_BINARY)
31  ground_truth->ensure_valid();
32 
33  // assume threshold as negative infinity
35  // false positive rate
36  float64_t fp = 0.0;
37  // true positive rate
38  float64_t tp=0.0;
39 
40  int32_t i;
41  // total number of positive labels in predicted
42  int32_t pos_count=0;
43  int32_t neg_count=0;
44 
45  // initialize number of labels and labels
46  SGVector<float64_t> orig_labels(predicted->get_num_labels());
47  int32_t length = orig_labels.vlen;
48  for (i=0; i<length; i++)
49  orig_labels[i] = predicted->get_value(i);
50  float64_t* labels = SGVector<float64_t>::clone_vector(orig_labels.vector, length);
51 
52  // get sorted indexes
53  SGVector<int32_t> idxs(length);
54  for(i=0; i<length; i++)
55  idxs[i] = i;
56 
57  CMath::qsort_backward_index(labels,idxs.vector,idxs.vlen);
58 
59  // number of different predicted labels
60  int32_t diff_count=1;
61 
62  // get number of different labels
63  for (i=0; i<length-1; i++)
64  {
65  if (labels[i] != labels[i+1])
66  diff_count++;
67  }
68 
69  SG_FREE(labels);
70 
71  // initialize graph and auROC
72  m_ROC_graph = SGMatrix<float64_t>(2,diff_count+1);
74  m_auROC = 0.0;
75 
76  // get total numbers of positive and negative labels
77  for(i=0; i<length; i++)
78  {
79  if (ground_truth->get_value(i) >= 0)
80  pos_count++;
81  else
82  neg_count++;
83  }
84 
85  // assure both number of positive and negative examples is >0
86  REQUIRE(pos_count>0, "%s::evaluate_roc(): Number of positive labels is "
87  "zero, ROC fails!\n", get_name());
88  REQUIRE(neg_count>0, "%s::evaluate_roc(): Number of negative labels is "
89  "zero, ROC fails!\n", get_name());
90 
91  int32_t j = 0;
92  float64_t label;
93 
94  // create ROC curve and calculate auROC
95  for(i=0; i<length; i++)
96  {
97  label = predicted->get_value(idxs[i]);
98 
99  if (label != threshold)
100  {
101  threshold = label;
102  m_ROC_graph[2*j] = fp/neg_count;
103  m_ROC_graph[2*j+1] = tp/pos_count;
104  j++;
105  }
106 
107  m_thresholds[i]=threshold;
108 
109  if (ground_truth->get_value(idxs[i]) > 0)
110  tp+=1.0;
111  else
112  fp+=1.0;
113  }
114 
115  // add (1,1) to ROC curve
116  m_ROC_graph[2*diff_count] = 1.0;
117  m_ROC_graph[2*diff_count+1] = 1.0;
118 
119  // calc auROC using area under curve
120  m_auROC = CMath::area_under_curve(m_ROC_graph.matrix,diff_count+1,false);
121 
122  m_computed = true;
123 
124  return m_auROC;
125 }
126 
128 {
129  if (!m_computed)
130  SG_ERROR("Uninitialized, please call evaluate first")
131 
132  return m_ROC_graph;
133 }
134 
136 {
137  if (!m_computed)
138  SG_ERROR("Uninitialized, please call evaluate first")
139 
140  return m_thresholds;
141 }
142 
144 {
145  if (!m_computed)
146  SG_ERROR("Uninitialized, please call evaluate first")
147 
148  return m_auROC;
149 }

SHOGUN Machine Learning Toolbox - Documentation