SHOGUN  6.1.3
MulticlassLabels.cpp
Go to the documentation of this file.
4 
5 using namespace shogun;
6 
8 {
9  init();
10 }
11 
12 CMulticlassLabels::CMulticlassLabels(int32_t num_labels) : CDenseLabels(num_labels)
13 {
14  init();
15 }
16 
18 {
19  init();
20  set_labels(src);
21 }
22 
24 {
25  init();
26 }
27 
29  : CDenseLabels(labels->get_num_labels())
30 {
31  init();
32 
33  for (index_t i = 0; i < labels->get_num_labels(); ++i)
34  m_labels[i] = (labels->get_label(i) == 1 ? 1 : 0);
35 }
36 
38 {
39 }
40 
41 void CMulticlassLabels::init()
42 {
44 }
45 
47  SGVector<float64_t> confidences)
48 {
50  "%s::set_multiclass_confidences(): Length of confidences should "
51  "match size of the matrix", get_name());
52 
53  m_multiclass_confidences.set_column(i, confidences);
54 }
55 
57 {
59  for (index_t j=0; j<confs.size(); j++)
60  confs[j] = m_multiclass_confidences(j,i);
61 
62  return confs;
63 }
64 
66 {
67  int32_t n_labels = m_labels.size();
68  REQUIRE(n_labels!=0,"%s::allocate_confidences_for(): There should be "
69  "labels to store confidences", get_name());
70 
71  m_multiclass_confidences = SGMatrix<float64_t>(n_classes,n_labels);
72 }
73 
74 void CMulticlassLabels::ensure_valid(const char* context)
75 {
77 
78  int32_t subset_size=get_num_labels();
79  for (int32_t i=0; i<subset_size; i++)
80  {
81  int32_t real_i = m_subset_stack->subset_idx_conversion(i);
82  int32_t label = int32_t(m_labels[real_i]);
83 
84  if (label<0 || float64_t(label)!=m_labels[real_i])
85  {
86  SG_ERROR("%s%sMulticlass Labels must be in range 0...<nr_classes-1> and integers!\n",
87  context?context:"", context?": ":"");
88  }
89  }
90 }
91 
93 {
94  return LT_MULTICLASS;
95 }
96 
98 {
99  SGVector<float64_t> binary_labels(get_num_labels());
100 
101  bool use_confidences = false;
103  {
104  use_confidences = true;
105  }
106  if (use_confidences)
107  {
108  for (int32_t k=0; k<binary_labels.vlen; k++)
109  {
110  int32_t label = get_int_label(k);
111  float64_t confidence = m_multiclass_confidences(label,k);
112  binary_labels[k] = label == i ? confidence : -confidence;
113  }
114  }
115  else
116  {
117  for (int32_t k=0; k<binary_labels.vlen; k++)
118  {
119  int32_t label = get_int_label(k);
120  binary_labels[k] = label == i ? +1.0 : -1.0;
121  }
122  }
123  return new CBinaryLabels(binary_labels);
124 }
125 
127 {
128  /* extract all labels (copy because of possible subset) */
129  SGVector<float64_t> unique_labels=get_labels_copy();
130  unique_labels.vlen=SGVector<float64_t>::unique(unique_labels.vector, unique_labels.vlen);
131 
132  SGVector<float64_t> result(unique_labels.vlen);
133  sg_memcpy(result.vector, unique_labels.vector,
134  sizeof(float64_t)*unique_labels.vlen);
135 
136  return result;
137 }
138 
139 
141 {
143  return unique.vlen;
144 }
145 
147 {
148  CLabels* shallow_copy_labels=NULL;
149  SGVector<float64_t> shallow_copy_vector(m_labels);
150  shallow_copy_labels=new CMulticlassLabels(m_labels.size());
151  SG_REF(shallow_copy_labels);
152  ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector);
154  shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
155 
156  return shallow_copy_labels;
157 }
158 
160 {
161  if (labels == NULL)
162  return NULL;
163 
164  if (labels->get_label_type() != LT_MULTICLASS)
165  {
166  SG_SERROR("The Labels passed cannot be casted to CMulticlassLabels!")
167  return NULL;
168  }
169 
170  CMulticlassLabels* casted = dynamic_cast<CMulticlassLabels*>(labels);
171  SG_REF(casted)
172  return casted;
173 }
void allocate_confidences_for(int32_t n_classes)
SGVector< index_t > get_subset_idx() const
Definition: Subset.h:48
SGVector< float64_t > m_labels
Definition: DenseLabels.h:274
virtual ELabelType get_label_type() const =0
virtual CLabels * shallow_subset_copy()
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:72
CBinaryLabels * get_binary_for_class(int32_t i)
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
multi-class labels 0,1,...
Definition: LabelTypes.h:20
virtual void ensure_valid(const char *context=NULL)
SGVector< float64_t > get_unique_labels()
#define SG_ERROR(...)
Definition: SGIO.h:128
#define REQUIRE(x,...)
Definition: SGIO.h:181
SGMatrix< float64_t > m_multiclass_confidences
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
#define SG_REF(x)
Definition: SGObject.h:52
Multiclass Labels for multi-class classification.
CSubset * get_last_subset() const
Definition: SubsetStack.h:98
int32_t size() const
Definition: SGVector.h:156
SGVector< float64_t > get_multiclass_confidences(int32_t i)
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:60
A File access base class.
Definition: File.h:34
index_t num_rows
Definition: SGMatrix.h:495
virtual const char * get_name() const
virtual void add_subset(SGVector< index_t > subset)
Definition: Labels.cpp:41
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
static CMulticlassLabels * obtain_from_generic(CLabels *labels)
index_t num_cols
Definition: SGMatrix.h:497
void set_multiclass_confidences(int32_t i, SGVector< float64_t > confidences)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual ELabelType get_label_type() const
virtual void ensure_valid(const char *context=NULL)
#define SG_SERROR(...)
Definition: SGIO.h:164
virtual bool has_subsets() const
Definition: SubsetStack.h:89
Binary Labels for binary classification.
Definition: BinaryLabels.h:37
Dense integer or floating point labels.
Definition: DenseLabels.h:35
static int32_t unique(T *output, int32_t size)
Definition: SGVector.cpp:841
void set_column(index_t col, const SGVector< T > vec)
Definition: SGMatrix.cpp:406
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:77
ELabelType
Definition: LabelTypes.h:15
index_t vlen
Definition: SGVector.h:571

SHOGUN Machine Learning Toolbox - Documentation