SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DenseLabels.h
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 #ifndef _DENSE_LABELS__H__
14 #define _DENSE_LABELS__H__
15 
16 #include <shogun/lib/config.h>
17 
18 #include <shogun/lib/common.h>
19 #include <shogun/io/File.h>
20 #include <shogun/labels/Labels.h>
22 
23 namespace shogun
24 {
25  class CFile;
26 
35 class CDenseLabels : public CLabels
36 {
37  public:
39  CDenseLabels();
40 
45  CDenseLabels(int32_t num_labels);
46 
51  CDenseLabels(CFile* loader);
52 
54  virtual ~CDenseLabels();
55 
62  virtual void ensure_valid(const char* context=NULL);
63 
70  virtual void load(CFile* loader);
71 
78  virtual void save(CFile* writer);
79 
88  bool set_label(int32_t idx, float64_t label);
89 
98  bool set_int_label(int32_t idx, int32_t label);
99 
105 
113  float64_t get_label(int32_t idx);
114 
122  template<typename ST>
123  ST get_label_t(int32_t idx)
124  {
125  REQUIRE(idx<get_num_labels(), "The provided index (%d) is out of bounds (the last label has index (%d)). "
126  "Please ensure that you're using a valid index number.", idx, get_num_labels())
127  REQUIRE(m_labels.vector, "You're attempting to get a label when there are in fact none! "
128  "Please ensure that you initialized the labels correctly.")
129  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
130  return m_labels.vector[real_num];
131  }
132 
140  int32_t get_int_label(int32_t idx);
141 
149 
155  template<typename ST>
157  {
159  return get_labels_copy_t<ST>();
160 
161  SGVector<ST> labels_copy(m_labels.vlen);
162  for(index_t i = 0; i < m_labels.vlen; ++i)
163  labels_copy[i] = (ST) m_labels[i];
164 
165  return labels_copy;
166  }
167 
174  template<typename ST>
176  {
177  if (!m_subset_stack->has_subsets())
178  {
179  SGVector<ST> labels_copy(m_labels.vlen);
180  for(index_t i = 0; i < m_labels.vlen; ++i)
181  labels_copy[i] = (ST) m_labels[i];
182 
183  return labels_copy;
184  }
185 
186  index_t num_labels = get_num_labels();
187  SGVector<ST> result(num_labels);
188 
189  /* copy element wise because of possible subset */
190  for (index_t i=0; i<num_labels; i++)
191  result[i] = get_label_t<ST>(i);
192 
193  return result;
194  }
195 
203 
209  void set_to_one();
210 
216  void zero();
217 
225  void set_to_const(float64_t c);
226 
234 
241  void set_int_labels(SGVector<int32_t> labels);
242 
243 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
244 
250  void set_int_labels(SGVector<int64_t> labels);
251 #endif
252 
257  virtual int32_t get_num_labels() const;
258 
263  virtual ELabelType get_label_type() const=0;
264 
265  public:
267  static const int32_t REJECTION_LABEL = -2;
268 
269  private:
270  void init();
271 
272  protected:
275 };
276 
282  template<> inline
283  SGVector<float64_t> CDenseLabels::get_labels_t<float64_t>()
284  {
285  if (m_subset_stack->has_subsets())
286  return get_labels_copy_t<float64_t>();
287 
288  return m_labels;
289  }
290 }
291 #endif
SGVector< float64_t > m_labels
Definition: DenseLabels.h:274
void set_int_labels(SGVector< int32_t > labels)
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
virtual ELabelType get_label_type() const =0
#define REQUIRE(x,...)
Definition: SGIO.h:206
CSubsetStack * m_subset_stack
Definition: Labels.h:140
float64_t get_label(int32_t idx)
SGVector< float64_t > get_labels_copy()
Definition: DenseLabels.cpp:90
bool set_label(int32_t idx, float64_t label)
SGVector< float64_t > get_labels()
Definition: DenseLabels.cpp:82
index_t vlen
Definition: SGVector.h:494
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:50
static const int32_t REJECTION_LABEL
Definition: DenseLabels.h:267
A File access base class.
Definition: File.h:34
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
void set_to_const(float64_t c)
Definition: DenseLabels.cpp:63
SGVector< int32_t > get_int_labels()
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void ensure_valid(const char *context=NULL)
bool set_int_label(int32_t idx, int32_t label)
virtual bool has_subsets() const
Definition: SubsetStack.h:89
Dense integer or floating point labels.
Definition: DenseLabels.h:35
ST get_label_t(int32_t idx)
Definition: DenseLabels.h:123
SGVector< ST > get_labels_copy_t()
Definition: DenseLabels.h:175
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:74
virtual void load(CFile *loader)
ELabelType
Definition: LabelTypes.h:15
virtual void save(CFile *writer)
SGVector< ST > get_labels_t()
Definition: DenseLabels.h:156

SHOGUN Machine Learning Toolbox - Documentation