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

SHOGUN Machine Learning Toolbox - Documentation