SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Labels.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  * Subset support written (W) 2011 Heiko Strathmann
10  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
13 #include <shogun/features/Labels.h>
14 #include <shogun/lib/common.h>
15 #include <shogun/io/File.h>
16 #include <shogun/io/SGIO.h>
18 #include <shogun/base/Parameter.h>
19 #include <shogun/lib/Set.h>
20 
21 using namespace shogun;
22 
24 : CSGObject()
25 {
26  init();
27 }
28 
29 CLabels::CLabels(int32_t num_lab)
30 : CSGObject()
31 {
32  init();
33  labels=SGVector<float64_t>(num_lab);
34 }
35 
37 : CSGObject()
38 {
39  init();
40 
41  set_labels(src);
43 }
44 
46 {
48  index_t subset_size=get_num_labels();
49  for (int32_t i=0; i<subset_size; i++)
51 }
52 
54 : CSGObject()
55 {
56  init();
57  load(loader);
58 }
59 
61 {
63  delete m_subset;
64  m_subset=NULL;
65 
66  m_num_classes=0;
67 }
68 
69 void CLabels::init()
70 {
71  m_parameters->add(&labels, "labels", "The labels.");
72  m_parameters->add((CSGObject**)&m_subset, "subset", "Subset object");
73 
75  m_num_classes=0;
76  m_subset=NULL;
77 }
78 
80 {
81  if (m_subset)
82  SG_ERROR("A subset is set, cannot set labels\n");
83 
85  labels=v;
86  labels.do_free=false;
87 }
88 
90 {
92  bool found_plus_one=false;
93  bool found_minus_one=false;
94 
95  int32_t subset_size=get_num_labels();
96  for (int32_t i=0; i<subset_size; i++)
97  {
98  int32_t real_i=subset_idx_conversion(i);
99  if (labels.vector[real_i]==+1.0)
100  found_plus_one=true;
101  else if (labels.vector[real_i]==-1.0)
102  found_minus_one=true;
103  else
104  {
105  SG_ERROR("Not a two class labeling label[%d]=%f (only +1/-1 "
106  "allowed)\n", i, labels.vector[real_i]);
107  }
108  }
109 
110  if (!found_plus_one)
111  SG_ERROR("Not a two class labeling - no positively labeled examples found\n");
112  if (!found_minus_one)
113  SG_ERROR("Not a two class labeling - no negatively labeled examples found\n");
114 
115  return true;
116 }
117 
119 {
120  CSet<float64_t>* classes=new CSet<float64_t>();
121  for (int32_t i=0; i<get_num_labels(); i++)
122  classes->add(get_label(i));
123 
124  int32_t result=classes->get_num_elements();
125  SG_UNREF(classes);
126  return result;
127 }
128 
130 {
131  CSet<float64_t>* classes=new CSet<float64_t>();
132 
133  for (int32_t i=0; i<get_num_labels(); i++)
134  classes->add(get_label(i));
135 
136  SGVector<float64_t> result(classes->get_num_elements());
137  memcpy(result.vector, classes->get_array(),
138  sizeof(float64_t)*classes->get_num_elements());
139 
140  SG_UNREF(classes);
141  return result;
142 }
143 
145 {
146  if (m_subset)
147  SG_ERROR("get_labels() is not possible on subset");
148 
149  return labels;
150 }
151 
153 {
154  SGVector<int32_t> intlab(get_num_labels(), true);
155 
156  for (int32_t i=0; i<get_num_labels(); i++)
157  intlab.vector[i]= get_int_label(i);
158 
159  return intlab;
160 }
161 
163 {
164  if (m_subset)
165  SG_ERROR("set_int_labels() is not possible on subset");
166 
169 
170  for (int32_t i=0; i<lab.vlen; i++)
171  set_int_label(i, labels.vector[i]);
172 }
173 
174 void CLabels::load(CFile* loader)
175 {
176  remove_subset();
177 
180 
181  ASSERT(loader);
182  loader->get_vector(labels.vector, labels.vlen);
185 }
186 
187 void CLabels::save(CFile* writer)
188 {
189  if (m_subset)
190  SG_ERROR("save() is not possible on subset");
191 
193  ASSERT(writer);
195  writer->set_vector(labels.vector, labels.vlen);
197 }
198 
199 bool CLabels::set_label(int32_t idx, float64_t label)
200 {
201  int32_t real_num=subset_idx_conversion(idx);
202  if (labels.vector && real_num<get_num_labels())
203  {
204  labels.vector[real_num]=label;
205  return true;
206  }
207  else
208  return false;
209 }
210 
211 bool CLabels::set_int_label(int32_t idx, int32_t label)
212 {
213  int32_t real_num=subset_idx_conversion(idx);
214  if (labels.vector && real_num<get_num_labels())
215  {
216  labels.vector[real_num]= (float64_t) label;
217  return true;
218  }
219  else
220  return false;
221 }
222 
224 {
225  int32_t real_num=subset_idx_conversion(idx);
227  return labels.vector[real_num];
228 }
229 
230 int32_t CLabels::get_int_label(int32_t idx)
231 {
232  int32_t real_num=subset_idx_conversion(idx);
234  if (labels.vector[real_num] != float64_t((int32_t(labels.vector[real_num]))))
235  SG_ERROR("label[%d]=%g is not an integer\n", labels.vector[real_num]);
236 
237  return int32_t(labels.vector[real_num]);
238 }
239 
241 {
242  return m_subset ? m_subset->get_size() : labels.vlen;
243 }
244 
246 {
247  SG_UNREF(m_subset);
248  m_subset=subset;
249  SG_REF(subset);
250 }
251 
253 {
254  set_subset(NULL);
255 }
256 
258 {
259  return m_subset ? m_subset->subset_idx_conversion(idx) : idx;
260 }
bool set_int_label(int32_t idx, int32_t label)
Definition: Labels.cpp:211
class for adding subset support to a class. Provides an interface for getting/setting subset_matrices...
Definition: Subset.h:24
#define SG_RESET_LOCALE
Definition: SGIO.h:67
bool set_label(int32_t idx, float64_t label)
Definition: Labels.cpp:199
SGVector< float64_t > get_labels()
Definition: Labels.cpp:144
SGVector< float64_t > get_classes()
Definition: Labels.cpp:129
int32_t get_num_labels()
Definition: Labels.cpp:240
#define SG_ERROR(...)
Definition: SGIO.h:75
Parameter * m_parameters
Definition: SGObject.h:297
void set_to_one()
Definition: Labels.cpp:45
T * get_array()
Definition: Set.h:129
#define SG_REF(x)
Definition: SGObject.h:44
#define SG_SET_LOCALE_C
Definition: SGIO.h:66
const index_t get_size() const
Definition: Subset.h:45
void set_int_labels(SGVector< int32_t > labels)
Definition: Labels.cpp:162
int32_t get_int_label(int32_t idx)
Definition: Labels.cpp:230
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
void set_labels(SGVector< float64_t > v)
Definition: Labels.cpp:79
virtual void set_subset(CSubset *subset)
Definition: Labels.cpp:245
virtual void free_vector()
Definition: DataType.h:212
#define ASSERT(x)
Definition: SGIO.h:102
SGVector< int32_t > get_int_labels()
Definition: Labels.cpp:152
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
Template Set class.
Definition: Set.h:26
virtual void get_vector(bool *&vector, int32_t &len)
Definition: File.cpp:64
virtual void destroy_vector()
Definition: DataType.h:223
double float64_t
Definition: common.h:56
SGVector< float64_t > labels
Definition: Labels.h:212
float64_t get_label(int32_t idx)
Definition: Labels.cpp:223
virtual void remove_subset()
Definition: Labels.cpp:252
A File access base class.
Definition: File.h:33
virtual void load(CFile *loader)
Definition: Labels.cpp:174
int32_t m_num_classes
Definition: Labels.h:215
bool is_two_class_labeling()
Definition: Labels.cpp:89
int32_t get_num_classes()
Definition: Labels.cpp:118
#define SG_UNREF(x)
Definition: SGObject.h:45
index_t subset_idx_conversion(index_t idx) const
Definition: Labels.cpp:257
void add(T e)
Definition: Set.h:45
index_t subset_idx_conversion(index_t idx) const
Definition: Subset.h:55
int32_t get_num_elements() const
Definition: Set.h:86
int32_t index_t
Definition: DataType.h:25
virtual void set_vector(const bool *vector, int32_t len)
Definition: File.cpp:78
virtual ~CLabels()
Definition: Labels.cpp:60
virtual void save(CFile *writer)
Definition: Labels.cpp:187
index_t vlen
Definition: DataType.h:248

SHOGUN Machine Learning Toolbox - Documentation