SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MulticlassLabels.cpp
Go to the documentation of this file.
5 
6 using namespace shogun;
7 
9 {
10  init();
11 }
12 
13 CMulticlassLabels::CMulticlassLabels(int32_t num_labels) : CDenseLabels(num_labels)
14 {
15  init();
16 }
17 
19 {
20  init();
21  set_labels(src);
22 }
23 
25 {
26  init();
27 }
28 
30 {
31 }
32 
33 void CMulticlassLabels::init()
34 {
35  /* for this to work, migration has to be fixed */
36 // SG_ADD(&m_multiclass_confidences, "multiclass_confidences", "Vectors of "
37 // "multiclass confidences", MS_NOT_AVAILABLE);
38 
39 // m_parameter_map->finalize_map();
40 
42 }
43 
45  SGVector<float64_t> confidences)
46 {
48  "%s::set_multiclass_confidences(): Length of confidences should "
49  "match size of the matrix", get_name());
50 
51  for (index_t j=0; j<confidences.size(); j++)
52  m_multiclass_confidences(j,i) = confidences[j];
53 }
54 
56 {
58  for (index_t j=0; j<confs.size(); j++)
59  confs[j] = m_multiclass_confidences(j,i);
60 
61  return confs;
62 }
63 
65 {
66  int32_t n_labels = m_labels.size();
67  REQUIRE(n_labels!=0,"%s::allocate_confidences_for(): There should be "
68  "labels to store confidences", get_name());
69 
70  m_multiclass_confidences = SGMatrix<float64_t>(n_classes,n_labels);
71 }
72 
73 void CMulticlassLabels::ensure_valid(const char* context)
74 {
76 
77  int32_t subset_size=get_num_labels();
78  for (int32_t i=0; i<subset_size; i++)
79  {
80  int32_t real_i = m_subset_stack->subset_idx_conversion(i);
81  int32_t label = int32_t(m_labels[real_i]);
82 
83  if (label<0 || float64_t(label)!=m_labels[real_i])
84  {
85  SG_ERROR("%s%sMulticlass Labels must be in range 0...<nr_classes-1> and integers!\n",
86  context?context:"", context?": ":"");
87  }
88  }
89 }
90 
92 {
93  return LT_MULTICLASS;
94 }
95 
97 {
98  SGVector<float64_t> binary_labels(get_num_labels());
99 
100  bool use_confidences = false;
102  {
103  use_confidences = true;
104  }
105  if (use_confidences)
106  {
107  for (int32_t k=0; k<binary_labels.vlen; k++)
108  {
109  int32_t label = get_int_label(k);
110  float64_t confidence = m_multiclass_confidences(label,k);
111  binary_labels[k] = label == i ? confidence : -confidence;
112  }
113  }
114  else
115  {
116  for (int32_t k=0; k<binary_labels.vlen; k++)
117  {
118  int32_t label = get_int_label(k);
119  binary_labels[k] = label == i ? +1.0 : -1.0;
120  }
121  }
122  return new CBinaryLabels(binary_labels);
123 }
124 
126 {
127  /* extract all labels (copy because of possible subset) */
128  SGVector<float64_t> unique_labels=get_labels_copy();
129  unique_labels.vlen=SGVector<float64_t>::unique(unique_labels.vector, unique_labels.vlen);
130 
131  SGVector<float64_t> result(unique_labels.vlen);
132  memcpy(result.vector, unique_labels.vector,
133  sizeof(float64_t)*unique_labels.vlen);
134 
135  return result;
136 }
137 
138 
140 {
142  return unique.vlen;
143 }
void allocate_confidences_for(int32_t n_classes)
SGVector< float64_t > m_labels
Definition: DenseLabels.h:210
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:62
CBinaryLabels * get_binary_for_class(int32_t i)
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:378
CSubsetStack * m_subset_stack
Definition: Labels.h:132
SGVector< float64_t > get_labels_copy()
Definition: DenseLabels.cpp:90
index_t num_rows
Definition: SGMatrix.h:376
int32_t size() const
Definition: SGVector.h:115
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
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)
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:787
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:74
ELabelType
Definition: LabelTypes.h:15

SHOGUN Machine Learning Toolbox - Documentation