SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 {
30 }
31 
32 void CMulticlassLabels::init()
33 {
35 }
36 
38  SGVector<float64_t> confidences)
39 {
41  "%s::set_multiclass_confidences(): Length of confidences should "
42  "match size of the matrix", get_name());
43 
44  for (index_t j=0; j<confidences.size(); j++)
45  m_multiclass_confidences(j,i) = confidences[j];
46 }
47 
49 {
51  for (index_t j=0; j<confs.size(); j++)
52  confs[j] = m_multiclass_confidences(j,i);
53 
54  return confs;
55 }
56 
58 {
59  int32_t n_labels = m_labels.size();
60  REQUIRE(n_labels!=0,"%s::allocate_confidences_for(): There should be "
61  "labels to store confidences", get_name());
62 
63  m_multiclass_confidences = SGMatrix<float64_t>(n_classes,n_labels);
64 }
65 
66 void CMulticlassLabels::ensure_valid(const char* context)
67 {
69 
70  int32_t subset_size=get_num_labels();
71  for (int32_t i=0; i<subset_size; i++)
72  {
73  int32_t real_i = m_subset_stack->subset_idx_conversion(i);
74  int32_t label = int32_t(m_labels[real_i]);
75 
76  if (label<0 || float64_t(label)!=m_labels[real_i])
77  {
78  SG_ERROR("%s%sMulticlass Labels must be in range 0...<nr_classes-1> and integers!\n",
79  context?context:"", context?": ":"");
80  }
81  }
82 }
83 
85 {
86  return LT_MULTICLASS;
87 }
88 
90 {
91  SGVector<float64_t> binary_labels(get_num_labels());
92 
93  bool use_confidences = false;
95  {
96  use_confidences = true;
97  }
98  if (use_confidences)
99  {
100  for (int32_t k=0; k<binary_labels.vlen; k++)
101  {
102  int32_t label = get_int_label(k);
103  float64_t confidence = m_multiclass_confidences(label,k);
104  binary_labels[k] = label == i ? confidence : -confidence;
105  }
106  }
107  else
108  {
109  for (int32_t k=0; k<binary_labels.vlen; k++)
110  {
111  int32_t label = get_int_label(k);
112  binary_labels[k] = label == i ? +1.0 : -1.0;
113  }
114  }
115  return new CBinaryLabels(binary_labels);
116 }
117 
119 {
120  /* extract all labels (copy because of possible subset) */
121  SGVector<float64_t> unique_labels=get_labels_copy();
122  unique_labels.vlen=SGVector<float64_t>::unique(unique_labels.vector, unique_labels.vlen);
123 
124  SGVector<float64_t> result(unique_labels.vlen);
125  memcpy(result.vector, unique_labels.vector,
126  sizeof(float64_t)*unique_labels.vlen);
127 
128  return result;
129 }
130 
131 
133 {
135  return unique.vlen;
136 }
137 
139 {
140  CLabels* shallow_copy_labels=NULL;
141  SGVector<float64_t> shallow_copy_vector(m_labels);
142  shallow_copy_labels=new CMulticlassLabels(m_labels.size());
143  SG_REF(shallow_copy_labels);
144  ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector);
146  shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
147 
148  return shallow_copy_labels;
149 }
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 CLabels * shallow_subset_copy()
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:62
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:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
SGMatrix< float64_t > m_multiclass_confidences
index_t num_cols
Definition: SGMatrix.h:376
CSubsetStack * m_subset_stack
Definition: Labels.h:140
SGVector< float64_t > get_labels_copy()
Definition: DenseLabels.cpp:90
#define SG_REF(x)
Definition: SGObject.h:54
index_t num_rows
Definition: SGMatrix.h:374
CSubset * get_last_subset() const
Definition: SubsetStack.h:98
int32_t size() const
Definition: SGVector.h:113
index_t vlen
Definition: SGVector.h:494
SGVector< float64_t > get_multiclass_confidences(int32_t i)
int32_t get_int_label(int32_t idx)
double float64_t
Definition: common.h:50
A File access base class.
Definition: File.h:34
virtual const char * get_name() const
virtual void add_subset(SGVector< index_t > subset)
Definition: Labels.cpp:39
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
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)
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:785
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:74
ELabelType
Definition: LabelTypes.h:15

SHOGUN Machine Learning Toolbox - Documentation