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)
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 */
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 {
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 }

SHOGUN Machine Learning Toolbox - Documentation