SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
labels
BinaryLabels.cpp
Go to the documentation of this file.
1
#include <
shogun/labels/DenseLabels.h
>
2
#include <
shogun/labels/BinaryLabels.h
>
3
#include <
shogun/mathematics/Statistics.h
>
4
5
using namespace
shogun;
6
7
CBinaryLabels::CBinaryLabels
() :
CDenseLabels
()
8
{
9
}
10
11
CBinaryLabels::CBinaryLabels
(int32_t num_labels) :
CDenseLabels
(num_labels)
12
{
13
}
14
15
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
16
CBinaryLabels::CBinaryLabels
(
SGVector<int32_t>
src) :
CDenseLabels
()
17
{
18
SGVector<float64_t>
values(src.
vlen
);
19
for
(int32_t i=0; i<values.
vlen
; i++)
20
values[i] = src[i];
21
set_int_labels
(src);
22
set_values
(values);
23
}
24
25
CBinaryLabels::CBinaryLabels
(
SGVector<int64_t>
src) :
CDenseLabels
()
26
{
27
SGVector<float64_t>
values(src.
vlen
);
28
for
(int32_t i=0; i<values.
vlen
; i++)
29
values[i] = src[i];
30
set_int_labels
(src);
31
set_values
(values);
32
}
33
#endif
34
35
CBinaryLabels::CBinaryLabels
(
SGVector<float64_t>
src,
float64_t
threshold) :
CDenseLabels
()
36
{
37
SGVector<float64_t>
labels(src.
vlen
);
38
for
(int32_t i=0; i<labels.
vlen
; i++)
39
labels[i] = src[i]+threshold>=0 ? +1.0 : -1.0;
40
set_labels
(labels);
41
set_values
(src);
42
}
43
44
CBinaryLabels::CBinaryLabels
(
CFile
* loader) :
CDenseLabels
(loader)
45
{
46
}
47
48
void
CBinaryLabels::ensure_valid
(
const
char
* context)
49
{
50
CDenseLabels::ensure_valid
(context);
51
bool
found_plus_one=
false
;
52
bool
found_minus_one=
false
;
53
54
int32_t subset_size=
get_num_labels
();
55
for
(int32_t i=0; i<subset_size; i++)
56
{
57
int32_t real_i=
m_subset_stack
->
subset_idx_conversion
(i);
58
if
(
m_labels
[real_i]==+1.0)
59
found_plus_one=
true
;
60
else
if
(
m_labels
[real_i]==-1.0)
61
found_minus_one=
true
;
62
else
63
{
64
SG_ERROR
(
65
"%s%s%s::ensure_valid(): Not a two class labeling label[%d]=%f (only +1/-1 "
66
"allowed)\n"
, context ? context :
""
,
67
context ?
": "
:
""
,
get_name
(), i,
m_labels
[real_i]);
68
}
69
}
70
71
if
(!found_plus_one)
72
{
73
SG_ERROR
(
74
"%s%s%s::ensure_valid(): Not a two class labeling - no positively labeled examples found\n"
,
75
context ? context :
""
, context ?
": "
:
""
,
get_name
());
76
}
77
78
if
(!found_minus_one)
79
{
80
SG_ERROR
(
81
"%s%s%s::ensure_valid): Not a two class labeling - no negatively labeled examples found\n"
,
82
context ? context :
""
, context ?
": "
:
""
,
get_name
());
83
}
84
}
85
86
ELabelType
CBinaryLabels::get_label_type
()
const
87
{
88
return
LT_BINARY
;
89
}
90
91
void
CBinaryLabels::scores_to_probabilities
(
float64_t
a,
float64_t
b)
92
{
93
SG_DEBUG
(
"entering CBinaryLabels::scores_to_probabilities()\n"
)
94
95
REQUIRE
(
m_current_values
.
vector
,
"%s::scores_to_probabilities() requires "
96
"values vector!\n"
,
get_name
());
97
98
if
(a==0 && b==0)
99
{
100
CStatistics::SigmoidParamters
params=
101
CStatistics::fit_sigmoid
(
m_current_values
);
102
a=params.
a
;
103
b=params.
b
;
104
}
105
106
SG_DEBUG
(
"using sigmoid: a=%f, b=%f\n"
, a, b)
107
108
/* now the sigmoid is fitted, convert all values to probabilities */
109
for
(
index_t
i=0; i<
m_current_values
.
vlen
; ++i)
110
{
111
float64_t
fApB=
m_current_values
[i]*a+b;
112
m_current_values
[i]=fApB>=0 ?
CMath::exp
(-fApB)/(1.0+
CMath::exp
(-fApB)) :
113
1.0/(1+
CMath::exp
(fApB));
114
}
115
116
SG_DEBUG
(
"leaving CBinaryLabels::scores_to_probabilities()\n"
)
117
}
SHOGUN
Machine Learning Toolbox - Documentation