SHOGUN
v1.1.0
Main Page
Related Pages
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
src
shogun
evaluation
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
11
#include <
shogun/evaluation/PRCEvaluation.h
>
12
#include <
shogun/mathematics/Math.h
>
13
14
using namespace
shogun;
15
16
CPRCEvaluation::~CPRCEvaluation
()
17
{
18
SG_FREE
(
m_PRC_graph
);
19
}
20
21
float64_t
CPRCEvaluation::evaluate
(
CLabels
* predicted,
CLabels
* ground_truth)
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);
50
SG_FREE
(
m_PRC_graph
);
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
81
m_auPRC
=
CMath::area_under_curve
(
m_PRC_graph
,length,
true
);
82
83
// set PRC length and computed indicator
84
m_PRC_length
= length;
85
m_computed
=
true
;
86
87
return
m_auPRC
;
88
}
89
90
SGMatrix<float64_t>
CPRCEvaluation::get_PRC
()
91
{
92
if
(!
m_computed
)
93
SG_ERROR
(
"Uninitialized, please call evaluate first"
);
94
95
ASSERT
(
m_PRC_graph
);
96
97
return
SGMatrix<float64_t>
(
m_PRC_graph
,2,
m_PRC_length
);
98
}
99
100
SGVector<float64_t>
CPRCEvaluation::get_thresholds
()
101
{
102
if
(!
m_computed
)
103
SG_ERROR
(
"Uninitialized, please call evaluate first"
);
104
105
ASSERT
(
m_thresholds
);
106
107
return
SGVector<float64_t>
(
m_thresholds
,
m_PRC_length
);
108
}
109
110
float64_t
CPRCEvaluation::get_auPRC
()
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