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 }
SGVector< float64_t > get_labels()
Definition: Labels.cpp:144
SGVector< float64_t > get_thresholds()
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
int32_t get_num_labels()
Definition: Labels.cpp:240
#define SG_ERROR(...)
Definition: SGIO.h:75
SGMatrix< float64_t > get_ROC()
static const float64_t ALMOST_NEG_INFTY
almost neg (log) infinity
Definition: Math.h:1598
virtual void free_vector()
Definition: DataType.h:212
#define ASSERT(x)
Definition: SGIO.h:102
double float64_t
Definition: common.h:56
float64_t get_label(int32_t idx)
Definition: Labels.cpp:223
static T * clone_vector(const T *vec, int32_t len)
Definition: Math.h:607
static float64_t area_under_curve(float64_t *xy, int32_t len, bool reversed)
Definition: Math.h:419
#define SG_FREE(ptr)
Definition: memory.h:39
bool is_two_class_labeling()
Definition: Labels.cpp:89
virtual float64_t evaluate(CLabels *predicted, CLabels *ground_truth)
float64_t * m_thresholds
Definition: ROCEvaluation.h:77
#define SG_MALLOC(type, len)
Definition: memory.h:36
static void qsort_backward_index(T1 *output, T2 *index, int32_t size)
Definition: Math.h:1778
index_t vlen
Definition: DataType.h:248

SHOGUN Machine Learning Toolbox - Documentation