SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
MulticlassSOLabels.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) 2013 Thoralf Klein
8  * Written (W) 2012 Fernando José Iglesias García
9  * Copyright (C) 2012 Fernando José Iglesias García
10  */
11 
13 
14 using namespace shogun;
15 
17 : CStructuredLabels(), m_labels_vector(16)
18 {
19  init();
20 }
21 
23 : CStructuredLabels(), m_labels_vector(num_labels)
24 {
25  init();
26 }
27 
29 : CStructuredLabels(src.vlen), m_labels_vector(src.vlen)
30 {
31  init();
32 
33  m_num_classes = CMath::max(src.vector, src.vlen) + 1;
34  m_labels_vector.resize_vector(src.vlen);
35 
36  for ( int32_t i = 0 ; i < src.vlen ; ++i )
37  {
38  if ( src[i] < 0 || src[i] >= m_num_classes )
39  SG_ERROR("Found label out of {0, 1, 2, ..., num_classes-1}")
40  else
41  add_label( new CRealNumber(src[i]) );
42  }
43 
44  //TODO check that every class has at least one example
45 }
46 
48 {
49 }
50 
52 {
53  // ensure_valid("CMulticlassSOLabels::get_label(int32_t)");
54  if ( idx < 0 || idx >= get_num_labels() )
55  SG_ERROR("Index must be inside [0, num_labels-1]\n")
56 
57  return (CStructuredData*) new CRealNumber(m_labels_vector[idx]);
58 }
59 
61 {
62  SG_REF(label);
64  SG_UNREF(label);
65 
66  //ensure_valid_sdt(label);
67  if (m_num_labels_set >= m_labels_vector.vlen)
68  {
69  m_labels_vector.resize_vector(m_num_labels_set + 16);
70  }
71 
72 
73  m_labels_vector[m_num_labels_set] = value;
74  m_num_labels_set++;
75 }
76 
78 {
79  SG_REF(label);
81  SG_UNREF(label);
82 
83  // ensure_valid_sdt(label);
84  int32_t real_idx = m_subset_stack->subset_idx_conversion(idx);
85 
86  if ( real_idx < get_num_labels() )
87  {
88  m_labels_vector[real_idx] = value;
89  return true;
90  }
91  else
92  {
93  return false;
94  }
95 }
96 
98 {
99  return m_num_labels_set;
100 }
101 
102 void CMulticlassSOLabels::init()
103 {
104  SG_ADD(&m_num_classes, "m_num_classes", "The number of classes",
106  SG_ADD(&m_num_labels_set, "m_num_labels_set", "The number of assigned labels",
108 
109  m_num_classes = 0;
110  m_num_labels_set = 0;
111 }
Base class of the labels used in Structured Output (SO) problems.
virtual CStructuredData * get_label(int32_t idx)
#define SG_ERROR(...)
Definition: SGIO.h:129
CSubsetStack * m_subset_stack
Definition: Labels.h:140
#define SG_REF(x)
Definition: SGObject.h:54
static CRealNumber * obtain_from_generic(CStructuredData *base_data)
virtual int32_t get_num_labels() const
index_t vlen
Definition: SGVector.h:494
double float64_t
Definition: common.h:50
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
static T max(T a, T b)
Definition: Math.h:168
#define SG_UNREF(x)
Definition: SGObject.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void add_label(CStructuredData *label)
void resize_vector(int32_t n)
Definition: SGVector.cpp:257
virtual bool set_label(int32_t idx, CStructuredData *label)
Class CRealNumber to be used in the application of Structured Output (SO) learning to multiclass clas...
#define SG_ADD(...)
Definition: SGObject.h:84
Base class of the components of StructuredLabels.

SHOGUN Machine Learning Toolbox - Documentation