SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SplittingStrategy.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) 2011 Heiko Strathmann
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
12 #include <shogun/features/Labels.h>
13 
14 using namespace shogun;
15 
17 {
18  init();
19 }
20 
21 CSplittingStrategy::CSplittingStrategy(CLabels* labels, int32_t num_subsets)
22 {
23  init();
24 
25  /* "assert" that num_subsets is smaller than num labels */
26  if (labels->get_num_labels()<num_subsets)
27  {
28  SG_ERROR("Only %d labels for %d subsets!\n", labels->get_num_labels(),
29  num_subsets);
30  }
31 
32  /* check for "stupid" combinations of label numbers and num_subsets.
33  * if there are of a class less labels than num_subsets, the class will not
34  * appear in every subset, leading to subsets of only one class in the
35  * extreme case of a two class labeling. */
36  SGVector<index_t> labels_per_class(labels->get_num_classes());
37  SGVector<float64_t> classes=labels->get_classes();
38 
39  for (index_t i=0; i<labels->get_num_classes(); ++i)
40  {
41  labels_per_class.vector[i]=0;
42  for (index_t j=0; j<labels->get_num_labels(); ++j)
43  {
44  if (classes.vector[i]==labels->get_label(j))
45  labels_per_class.vector[i]++;
46  }
47  }
48 
49  for (index_t i=0; i<labels->get_num_classes(); ++i)
50  {
51  if (labels_per_class.vector[i]<num_subsets)
52  {
53  SG_WARNING("There are only %d labels of class %.18g, but %d "
54  "subsets. Labels of that class will not appear in every "
55  "subset!\n", labels_per_class.vector[i], classes.vector[i], num_subsets);
56  }
57  }
58 
59  labels_per_class.destroy_vector();
60  classes.destroy_vector();
61 
62  m_labels=labels;
64 
65  /* construct all arrays */
66  for (index_t i=0; i<num_subsets; ++i)
67  m_subset_indices->append_element(new CDynamicArray<index_t> ());
68 }
69 
70 void CSplittingStrategy::init()
71 {
72  m_labels=NULL;
75 
76  m_parameters->add((CSGObject**)m_labels, "labels", "Labels for subsets");
77  m_parameters->add((CSGObject**)m_subset_indices, "subset_indices",
78  "Set of sets of subset indices");
79 }
80 
82 {
85 }
86 
88 {
89  /* construct SGVector copy from index vector */
90  CDynamicArray<index_t>* to_copy=m_subset_indices->get_element_safe(
91  subset_idx);
92 
93  index_t num_elements=to_copy->get_num_elements();
94  SGVector<index_t> result(num_elements, true);
95 
96  /* copy data */
97  memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements);
98 
99  SG_UNREF(to_copy);
100 
101  return result;
102 }
103 
105 {
106  CDynamicArray<index_t>* to_invert=m_subset_indices->get_element_safe(
107  subset_idx);
108 
109  SGVector<index_t> result(
110  m_labels->get_num_labels()-to_invert->get_num_elements(), true);
111 
112  index_t index=0;
113  for (index_t i=0; i<m_labels->get_num_labels(); ++i)
114  {
115  /* add i to inverse indices if it is not in the to be inverted set */
116  if (to_invert->find_element(i)==-1)
117  result.vector[index++]=i;
118  }
119 
120  SG_UNREF(to_invert);
121 
122  return result;
123 }
SGVector< float64_t > get_classes()
Definition: Labels.cpp:129
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
int32_t get_num_elements() const
Definition: DynamicArray.h:76
int32_t get_num_labels()
Definition: Labels.cpp:240
#define SG_ERROR(...)
Definition: SGIO.h:75
Parameter * m_parameters
Definition: SGObject.h:297
#define SG_REF(x)
Definition: SGObject.h:44
CDynamicObjectArray< CDynamicArray< index_t > > * m_subset_indices
T * get_array() const
Definition: DynamicArray.h:202
void add(bool *param, const char *name, const char *description="")
Definition: Parameter.cpp:23
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:76
virtual void destroy_vector()
Definition: DataType.h:223
float64_t get_label(int32_t idx)
Definition: Labels.cpp:223
SGVector< index_t > generate_subset_inverse(index_t subset_idx)
Template Dynamic array class that creates an array that can be used like a list or an array...
Definition: DynArray.h:21
int32_t find_element(T element)
Definition: DynamicArray.h:169
int32_t get_num_classes()
Definition: Labels.cpp:118
#define SG_UNREF(x)
Definition: SGObject.h:45
SGVector< index_t > generate_subset_indices(index_t subset_idx)
int32_t index_t
Definition: DataType.h:25
#define SG_WARNING(...)
Definition: SGIO.h:74

SHOGUN Machine Learning Toolbox - Documentation