SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PRCEvaluation.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  // number of true positive examples
28  float64_t tp = 0.0;
29  int32_t i;
30 
31  // total number of positive labels in predicted
32  int32_t pos_count=0;
33 
34  // initialize number of labels and labels
35  SGVector<float64_t> orig_labels = predicted->get_labels();
36  int32_t length = orig_labels.vlen;
37  float64_t* labels = CMath::clone_vector(orig_labels.vector, length);
38  orig_labels.free_vector();
39 
40  // get indexes for sort
41  int32_t* idxs = SG_MALLOC(int32_t, length);
42  for(i=0; i<length; i++)
43  idxs[i] = i;
44 
45  // sort indexes by labels ascending
46  CMath::qsort_backward_index(labels,idxs,length);
47 
48  // clean and initialize graph and auPRC
49  SG_FREE(labels);
51  m_PRC_graph = SG_MALLOC(float64_t, length*2);
52  m_thresholds = SG_MALLOC(float64_t, length);
53  m_auPRC = 0.0;
54 
55  // get total numbers of positive and negative labels
56  for (i=0; i<length; i++)
57  {
58  if (ground_truth->get_label(i) > 0)
59  pos_count++;
60  }
61 
62  // assure number of positive examples is >0
63  ASSERT(pos_count>0);
64 
65  // create PRC curve
66  for (i=0; i<length; i++)
67  {
68  // update number of true positive examples
69  if (ground_truth->get_label(idxs[i]) > 0)
70  tp += 1.0;
71 
72  // precision (x)
73  m_PRC_graph[2*i] = tp/float64_t(i+1);
74  // recall (y)
75  m_PRC_graph[2*i+1] = tp/float64_t(pos_count);
76 
77  m_thresholds[i]= predicted->get_label(idxs[i]);
78  }
79 
80  // calc auRPC using area under curve
82 
83  // set PRC length and computed indicator
84  m_PRC_length = length;
85  m_computed = true;
86 
87  return m_auPRC;
88 }
89 
91 {
92  if (!m_computed)
93  SG_ERROR("Uninitialized, please call evaluate first");
94 
96 
98 }
99 
101 {
102  if (!m_computed)
103  SG_ERROR("Uninitialized, please call evaluate first");
104 
106 
108 }
109 
111 {
112  if (!m_computed)
113  SG_ERROR("Uninitialized, please call evaluate first");
114 
115  return m_auPRC;
116 }
117 
118 

SHOGUN Machine Learning Toolbox - Documentation