SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
15 #include <shogun/lib/common.h>
16 #include <shogun/io/File.h>
17 #include <shogun/io/SGIO.h>
19 #include <shogun/base/Parameter.h>
20 
21 using namespace shogun;
22 
24 : CLabels()
25 {
26  init();
27 }
28 
30 : CLabels()
31 {
32  init();
33  m_labels = SGVector<float64_t>(num_lab);
34 }
35 
37 : CLabels()
38 {
39  init();
40  load(loader);
41 }
42 
44 {
45 }
46 
47 void CDenseLabels::init()
48 {
49  SG_ADD(&m_labels, "labels", "The labels.", MS_NOT_AVAILABLE);
50 }
51 
53 {
54  set_to_const(1.0);
55 }
56 
58 {
59  set_to_const(0.0);
60 }
61 
63 {
65  index_t subset_size=get_num_labels();
66  for (int32_t i=0; i<subset_size; i++)
68 }
69 
71 {
73  SG_ERROR("A subset is set, cannot set labels\n");
74 
75  m_labels = v;
76 }
77 
79 {
81  SG_ERROR("get_labels() is not possible on subset");
82 
83  return m_labels;
84 }
85 
87 {
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 
102 {
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 
112 {
114  SG_ERROR("set_int_labels() is not possible on subset");
115 
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 
129 {
130  remove_subset();
131 
134  ASSERT(loader);
137 }
138 
140 {
142  SG_ERROR("save() is not possible on subset");
143 
145  ASSERT(writer);
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 
176 {
177  int32_t real_num=m_subset_stack->subset_idx_conversion(idx);
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);
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 
193 {
194  return m_subset_stack->has_subsets()
196 }

SHOGUN Machine Learning Toolbox - Documentation