/
BinaryLabels.cpp
167 lines (140 loc) · 3.91 KB
/
BinaryLabels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Heiko Strathmann, Saurabh Mahindre, Sergey Lisitsyn,
* Soeren Sonnenburg, Fernando Iglesias, Evgeniy Andreev,
* Chiyuan Zhang, Olivier NGuyen, Thoralf Klein
*/
#include <shogun/base/range.h>
#include <shogun/labels/BinaryLabels.h>
#include <shogun/labels/DenseLabels.h>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/Statistics.h>
#include <utility>
using namespace shogun;
BinaryLabels::BinaryLabels() : DenseLabels()
{
}
BinaryLabels::BinaryLabels(int32_t num_labels) : DenseLabels(num_labels)
{
}
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
BinaryLabels::BinaryLabels(SGVector<int32_t> src) : DenseLabels()
{
SGVector<float64_t> values(src.vlen);
for (int32_t i = 0; i < values.vlen; i++)
{
values[i] = src[i];
}
set_int_labels(src);
set_values(values);
}
BinaryLabels::BinaryLabels(SGVector<int64_t> src) : DenseLabels()
{
SGVector<float64_t> values(src.vlen);
for (int32_t i = 0; i < values.vlen; i++)
{
values[i] = src[i];
}
set_int_labels(src);
set_values(values);
}
#endif
BinaryLabels::BinaryLabels(SGVector<float64_t> src, float64_t threshold) : DenseLabels()
{
SGVector<float64_t> labels(src.vlen);
for (int32_t i = 0; i < labels.vlen; i++)
{
labels[i] = src[i] >= threshold ? +1.0 : -1.0;
}
set_labels(labels);
set_values(src);
}
BinaryLabels::BinaryLabels(std::shared_ptr<File > loader) : DenseLabels(std::move(loader))
{
}
bool BinaryLabels::is_valid() const
{
if (!DenseLabels::is_valid())
return false;
int32_t subset_size = get_num_labels();
for (int32_t i = 0; i < subset_size; i++)
{
int32_t real_i = m_subset_stack->subset_idx_conversion(i);
if (m_labels[real_i] != +1.0 && m_labels[real_i] != -1.0)
return false;
}
return true;
}
void BinaryLabels::ensure_valid(const char* context)
{
require(is_valid(), "Binary Labels must be -1 or +1!");
}
ELabelType BinaryLabels::get_label_type() const
{
return LT_BINARY;
}
void BinaryLabels::scores_to_probabilities(float64_t a, float64_t b)
{
SG_TRACE("entering BinaryLabels::scores_to_probabilities()");
require(m_current_values.vector, "{}::scores_to_probabilities() requires "
"values vector!", get_name());
if (a == 0 && b == 0)
{
Statistics::SigmoidParamters params =
Statistics::fit_sigmoid(m_current_values);
a = params.a;
b = params.b;
}
SG_DEBUG("using sigmoid: a={}, b={}", a, b)
/* now the sigmoid is fitted, convert all values to probabilities */
for (index_t i = 0; i < m_current_values.vlen; ++i)
{
float64_t fApB = m_current_values[i] * a + b;
m_current_values[i] = fApB >= 0
? std::exp(-fApB) / (1.0 + std::exp(-fApB))
: 1.0 / (1 + std::exp(fApB));
}
SG_TRACE("leaving BinaryLabels::scores_to_probabilities()");
}
std::shared_ptr<Labels> BinaryLabels::shallow_subset_copy()
{
SGVector<float64_t> shallow_copy_vector(m_labels);
auto shallow_copy_labels=std::make_shared<BinaryLabels>(m_labels.size());
shallow_copy_labels->set_labels(shallow_copy_vector);
if (m_subset_stack->has_subsets())
shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
return shallow_copy_labels;
}
BinaryLabels::BinaryLabels(const DenseLabels& dense) : DenseLabels(dense)
{
ensure_valid();
}
std::shared_ptr<Labels> BinaryLabels::duplicate() const
{
return std::make_shared<BinaryLabels>(*this);
}
namespace shogun
{
std::shared_ptr<BinaryLabels> binary_labels(const std::shared_ptr<Labels>& orig)
{
require(orig, "No labels provided.");
try
{
switch (orig->get_label_type())
{
case LT_BINARY:
return std::static_pointer_cast<BinaryLabels>(orig);
default:
not_implemented(SOURCE_LOCATION);
}
}
catch (const ShogunException& e)
{
error(
"Cannot convert {} to binary labels: {}", orig->get_name(),
e.what());
}
return nullptr;
}
} // namespace shogun