SHOGUN
v2.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
DenseLabels.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) 1999-2009 Soeren Sonnenburg
8
* Written (W) 1999-2008 Gunnar Raetsch
9
* Written (W) 2011 Heiko Strathmann
10
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11
*/
12
13
#include <
shogun/labels/Labels.h
>
14
#include <
shogun/labels/DenseLabels.h
>
15
#include <
shogun/lib/common.h
>
16
#include <
shogun/io/File.h
>
17
#include <
shogun/io/SGIO.h
>
18
#include <
shogun/mathematics/Math.h
>
19
#include <
shogun/base/Parameter.h
>
20
21
using namespace
shogun;
22
23
CDenseLabels::CDenseLabels
()
24
:
CLabels
()
25
{
26
init();
27
}
28
29
CDenseLabels::CDenseLabels
(int32_t num_lab)
30
:
CLabels
()
31
{
32
init();
33
m_labels
=
SGVector<float64_t>
(num_lab);
34
}
35
36
CDenseLabels::CDenseLabels
(
CFile
* loader)
37
:
CLabels
()
38
{
39
init();
40
load
(loader);
41
}
42
43
CDenseLabels::~CDenseLabels
()
44
{
45
}
46
47
void
CDenseLabels::init()
48
{
49
SG_ADD
(&
m_labels
,
"labels"
,
"The labels."
,
MS_NOT_AVAILABLE
);
50
}
51
52
void
CDenseLabels::set_to_one
()
53
{
54
set_to_const
(1.0);
55
}
56
57
void
CDenseLabels::zero
()
58
{
59
set_to_const
(0.0);
60
}
61
62
void
CDenseLabels::set_to_const
(
float64_t
c)
63
{
64
ASSERT
(
m_labels
.
vector
);
65
index_t
subset_size=
get_num_labels
();
66
for
(int32_t i=0; i<subset_size; i++)
67
m_labels
.
vector
[
m_subset_stack
->
subset_idx_conversion
(i)]=c;
68
}
69
70
void
CDenseLabels::set_labels
(
SGVector<float64_t>
v)
71
{
72
if
(
m_subset_stack
->
has_subsets
())
73
SG_ERROR
(
"A subset is set, cannot set labels\n"
);
74
75
m_labels
= v;
76
}
77
78
SGVector<float64_t>
CDenseLabels::get_labels
()
79
{
80
if
(
m_subset_stack
->
has_subsets
())
81
SG_ERROR
(
"get_labels() is not possible on subset"
);
82
83
return
m_labels
;
84
}
85
86
SGVector<float64_t>
CDenseLabels::get_labels_copy
()
87
{
88
if
(!
m_subset_stack
->
has_subsets
())
89
return
m_labels
.
clone
();
90
91
index_t
num_labels =
get_num_labels
();
92
SGVector<float64_t>
result(num_labels);
93
94
/* copy element wise because of possible subset */
95
for
(
index_t
i=0; i<num_labels; i++)
96
result[i] =
get_label
(i);
97
98
return
result;
99
}
100
101
SGVector<int32_t>
CDenseLabels::get_int_labels
()
102
{
103
SGVector<int32_t>
intlab(
get_num_labels
());
104
105
for
(int32_t i=0; i<
get_num_labels
(); i++)
106
intlab.
vector
[i] =
get_int_label
(i);
107
108
return
intlab;
109
}
110
111
void
CDenseLabels::set_int_labels
(
SGVector<int32_t>
lab)
112
{
113
if
(
m_subset_stack
->
has_subsets
())
114
SG_ERROR
(
"set_int_labels() is not possible on subset"
);
115
116
m_labels
=
SGVector<float64_t>
(lab.
vlen
);
117
118
for
(int32_t i=0; i<lab.
vlen
; i++)
119
set_int_label
(i, lab.
vector
[i]);
120
}
121
122
void
CDenseLabels::ensure_valid
(
const
char
* context)
123
{
124
if
(
m_labels
.
vector
== NULL)
125
SG_ERROR
(
"%s%sempty content (NULL) for labels\n"
, context?context:
""
, context?
": "
:
""
);
126
}
127
128
void
CDenseLabels::load
(
CFile
* loader)
129
{
130
remove_subset
();
131
132
SG_SET_LOCALE_C
;
133
m_labels
=
SGVector<float64_t>
();
134
ASSERT
(loader);
135
loader->
get_vector
(
m_labels
.
vector
,
m_labels
.
vlen
);
136
SG_RESET_LOCALE
;
137
}
138
139
void
CDenseLabels::save
(
CFile
* writer)
140
{
141
if
(
m_subset_stack
->
has_subsets
())
142
SG_ERROR
(
"save() is not possible on subset"
);
143
144
SG_SET_LOCALE_C
;
145
ASSERT
(writer);
146
ASSERT
(
m_labels
.
vector
&&
m_labels
.
vlen
>0);
147
writer->
set_vector
(
m_labels
.
vector
,
m_labels
.
vlen
);
148
SG_RESET_LOCALE
;
149
}
150
151
bool
CDenseLabels::set_label
(int32_t idx,
float64_t
label)
152
{
153
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
154
if
(
m_labels
.
vector
&& real_num<
get_num_labels
())
155
{
156
m_labels
.
vector
[real_num]=label;
157
return
true
;
158
}
159
else
160
return
false
;
161
}
162
163
bool
CDenseLabels::set_int_label
(int32_t idx, int32_t label)
164
{
165
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
166
if
(
m_labels
.
vector
&& real_num<
get_num_labels
())
167
{
168
m_labels
.
vector
[real_num] = (
float64_t
)label;
169
return
true
;
170
}
171
else
172
return
false
;
173
}
174
175
float64_t
CDenseLabels::get_label
(int32_t idx)
176
{
177
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
178
ASSERT
(
m_labels
.
vector
&& idx<
get_num_labels
());
179
return
m_labels
.
vector
[real_num];
180
}
181
182
int32_t
CDenseLabels::get_int_label
(int32_t idx)
183
{
184
int32_t real_num=
m_subset_stack
->
subset_idx_conversion
(idx);
185
ASSERT
(
m_labels
.
vector
&& idx<
get_num_labels
());
186
if
(
m_labels
.
vector
[real_num] !=
float64_t
((int32_t(
m_labels
.
vector
[real_num]))))
187
SG_ERROR
(
"label[%d]=%g is not an integer\n"
,
m_labels
.
vector
[real_num]);
188
189
return
int32_t(
m_labels
.
vector
[real_num]);
190
}
191
192
int32_t
CDenseLabels::get_num_labels
()
193
{
194
return
m_subset_stack
->
has_subsets
()
195
?
m_subset_stack
->
get_size
() :
m_labels
.
vlen
;
196
}
SHOGUN
Machine Learning Toolbox - Documentation