SHOGUN  6.1.3
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/base/Parameter.h>
14 #include <shogun/io/File.h>
15 #include <shogun/io/SGIO.h>
17 #include <shogun/labels/Labels.h>
18 #include <shogun/lib/common.h>
21 
22 using namespace shogun;
23 
25 : CLabels()
26 {
27  init();
28 }
29 
31 : CLabels()
32 {
33  init();
34  m_labels = SGVector<float64_t>(num_lab);
37  linalg::zero(m_current_values);
38 }
39 
41 : CLabels()
42 {
43  init();
44  load(loader);
45 }
46 
48 {
49 }
50 
51 void CDenseLabels::init()
52 {
53  SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
54 }
55 
57 {
58  set_to_const(1.0);
59 }
60 
62 {
63  set_to_const(0.0);
64 }
65 
67 {
69  index_t subset_size=get_num_labels();
70  for (int32_t i=0; i<subset_size; i++)
71  {
74  }
75 }
76 
78 {
80  SG_ERROR("A subset is set, cannot set labels\n")
81 
82  m_labels = v;
83 }
84 
86 {
88  return get_labels_copy();
89 
90  return m_labels;
91 }
92 
94 {
96  return m_labels.clone();
97 
98  index_t num_labels = get_num_labels();
99  SGVector<float64_t> result(num_labels);
100 
101  /* copy element wise because of possible subset */
102  for (index_t i=0; i<num_labels; i++)
103  result[i] = get_label(i);
104 
105  return result;
106 }
107 
109 {
111 
112  for (int32_t i=0; i<get_num_labels(); i++)
113  intlab.vector[i] = get_int_label(i);
114 
115  return intlab;
116 }
117 
119 {
121  SG_ERROR("set_int_labels() is not possible on subset")
122 
124 
125  for (int32_t i=0; i<lab.vlen; i++)
126  set_int_label(i, lab.vector[i]);
127 }
128 
129 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
131 {
133  SG_ERROR("set_int_labels() is not possible on subset")
134 
136 
137  for (int32_t i=0; i<lab.vlen; i++)
138  set_int_label(i, lab.vector[i]);
139 }
140 #endif
141 
142 void CDenseLabels::ensure_valid(const char* context)
143 {
144  if (m_labels.vector == NULL)
145  SG_ERROR("%s%sempty content (NULL) for labels\n", context?context:"", context?": ":"")
146 }
147 
149 {
150  remove_subset();
152  m_labels.load(loader);
153 }
154 
156 {
158  SG_ERROR("save() is not possible on subset")
159 
160  m_labels.save(writer);
161 }
162 
163 bool CDenseLabels::set_label(int32_t idx, float64_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]=label;
169  return true;
170  }
171  else
172  return false;
173 }
174 
175 bool CDenseLabels::set_int_label(int32_t idx, int32_t label)
176 {
177  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
178  if (m_labels.vector && real_num<get_num_labels())
179  {
180  m_labels.vector[real_num] = (float64_t)label;
181  return true;
182  }
183  else
184  return false;
185 }
186 
188 {
189  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
191  return m_labels.vector[real_num];
192 }
193 
194 int32_t CDenseLabels::get_int_label(int32_t idx)
195 {
196  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
198  if (m_labels.vector[real_num] != float64_t((int32_t(m_labels.vector[real_num]))))
199  SG_ERROR("label[%d]=%g is not an integer\n", m_labels.vector[real_num])
200 
201  return int32_t(m_labels.vector[real_num]);
202 }
203 
205 {
206  return m_subset_stack->has_subsets()
208 }
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:72
index_t get_size() const
Definition: SubsetStack.h:80
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:128
void load(CFile *loader)
Definition: SGVector.cpp:903
CSubsetStack * m_subset_stack
Definition: Labels.h:145
float64_t get_label(int32_t idx)
SGVector< float64_t > get_labels_copy()
Definition: DenseLabels.cpp:93
bool set_label(int32_t idx, float64_t label)
SGVector< float64_t > get_labels()
Definition: DenseLabels.cpp:85
#define ASSERT(x)
Definition: SGIO.h:176
void zero(Container< T > &a)
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:60
void save(CFile *saver)
Definition: SGVector.cpp:924
virtual void remove_subset()
Definition: Labels.cpp:51
A File access base class.
Definition: File.h:34
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
SGVector< T > clone() const
Definition: SGVector.cpp:262
void set_to_const(float64_t c)
Definition: DenseLabels.cpp:66
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
SGVector< float64_t > m_current_values
Definition: Labels.h:148
#define SG_ADD(...)
Definition: SGObject.h:93
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:77
virtual void load(CFile *loader)
virtual void save(CFile *writer)
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation