forked from shogun-toolbox/shogun
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DenseLabels.h
294 lines (252 loc) · 6.16 KB
/
DenseLabels.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Sergey Lisitsyn, Soeren Sonnenburg, Fernando Iglesias,
* Christopher Goldsworthy, Heiko Strathmann, Yuyu Zhang,
* Chiyuan Zhang
*/
#ifndef _DENSE_LABELS__H__
#define _DENSE_LABELS__H__
#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/io/File.h>
#include <shogun/labels/Labels.h>
#include <shogun/features/SubsetStack.h>
namespace shogun
{
class CFile;
class CBinaryLabels;
/** @brief Dense integer or floating point labels
*
* DenseLabels here are always real-valued and thus applicable to
* classification
* (cf. CClassifier) and regression (cf. CRegression) problems.
*
* This class implements the shared functions for storing, and accessing
* label
* (vectors).
*/
class CDenseLabels : public CLabels
{
public:
/** default constructor */
CDenseLabels();
/** constructor
*
* @param num_labels number of labels
*/
CDenseLabels(int32_t num_labels);
/** copy constructor
*
* @param orig The dense labels to copy
*/
CDenseLabels(const CDenseLabels& orig);
/** constructor
*
* @param loader File object via which to load data
*/
CDenseLabels(CFile* loader);
/** destructor */
virtual ~CDenseLabels() override;
virtual bool is_valid() const override;
/** Make sure the label is valid, otherwise raise SG_ERROR.
*
* possible with subset
*
* @param context optional message to convey the context
*/
void ensure_valid(const char* context = NULL) override;
/** load labels from file
*
* any subset is removed before
*
* @param loader File object via which to load data
*/
virtual void load(CFile* loader);
/** save labels to file
*
* not possible with subset
*
* @param writer File object via which to save data
*/
virtual void save(CFile* writer);
/** set label
*
* possible with subset
*
* @param idx index of label to set
* @param label value of label
* @return if setting was successful
*/
bool set_label(int32_t idx, float64_t label);
/** set INT label
*
* possible with subset
*
* @param idx index of label to set
* @param label INT value of label
* @return if setting was successful
*/
bool set_int_label(int32_t idx, int32_t label);
/** Getter for labels
*
* @return labels, a copy if a subset is set
*/
virtual SGVector<float64_t> get_labels() const;
/** get label
*
* possible with subset
*
* @param idx index of label to get
* @return value of label
*/
virtual float64_t get_label(int32_t idx) const;
/** get label
*
* possible with subset
*
* @param idx index of label to get
* @return value of label
*/
template<typename ST>
ST get_label_t(int32_t idx)
{
REQUIRE(idx<get_num_labels(), "The provided index (%d) is out of bounds (the last label has index (%d)). "
"Please ensure that you're using a valid index number.", idx, get_num_labels())
REQUIRE(m_labels.vector, "You're attempting to get a label when there are in fact none! "
"Please ensure that you initialized the labels correctly.")
int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
return m_labels.vector[real_num];
}
/** get INT label
*
* possible with subset
*
* @param idx index of label to get
* @return INT value of label
*/
int32_t get_int_label(int32_t idx);
/** Gets a copy of the labels.
*
* possible with subset
*
* @return labels
*/
SGVector<float64_t> get_labels_copy() const;
/** Getter for labels
*
* @return labels, or a copy of them if a subset is
* set or if ST is not of type float64_t
*/
template<typename ST>
SGVector<ST> get_labels_t()
{
if (m_subset_stack->has_subsets())
return get_labels_copy_t<ST>();
SGVector<ST> labels_copy(m_labels.vlen);
for(index_t i = 0; i < m_labels.vlen; ++i)
labels_copy[i] = (ST) m_labels[i];
return labels_copy;
}
/** Get copy of labels.
*
* possible with subset
*
* @return labels
*/
template<typename ST>
SGVector<ST> get_labels_copy_t()
{
if (!m_subset_stack->has_subsets())
{
SGVector<ST> labels_copy(m_labels.vlen);
for(index_t i = 0; i < m_labels.vlen; ++i)
labels_copy[i] = (ST) m_labels[i];
return labels_copy;
}
index_t num_labels = get_num_labels();
SGVector<ST> result(num_labels);
/* copy element wise because of possible subset */
for (index_t i=0; i<num_labels; i++)
result[i] = get_label_t<ST>(i);
return result;
}
/** set labels
*
* not possible with subset
*
* @param v labels
*/
void set_labels(SGVector<float64_t> v);
/**
* set all labels to +1
*
* possible with subset
* */
void set_to_one();
/**
* set all labels to zero
*
* possible with subset
* */
void zero();
/**
* set all labels to a const value
*
* possible with subset
*
* @param c const to set labels to
* */
void set_to_const(float64_t c);
/** get INT label vector
*
* possible with subset
*
* @return INT labels
*/
SGVector<int32_t> get_int_labels();
/** set INT labels
*
* not possible on subset
*
* @param labels INT labels
*/
void set_int_labels(SGVector<int32_t> labels);
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
/** set INT64 labels
*
* not possible on subset
*
* @param labels INT labels
*/
void set_int_labels(SGVector<int64_t> labels);
#endif
/** get number of labels, depending on whether a subset is set
*
* @return number of labels
*/
int32_t get_num_labels() const override;
virtual const char* get_name() const override = 0;
public:
/** label designates classify reject */
static const int32_t REJECTION_LABEL = -2;
private:
void init();
protected:
/** the label vector */
SGVector<float64_t> m_labels;
};
/**
* Specialization of get_labels for float64_t
* @return labels, or a copy of them if a subset
* is set
*/
template <>
inline SGVector<float64_t> CDenseLabels::get_labels_t<float64_t>()
{
if (m_subset_stack->has_subsets())
return get_labels_copy_t<float64_t>();
return m_labels;
}
}
#endif