SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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-2012 Heiko Strathmann
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
12 #include <shogun/labels/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  m_num_subsets=num_subsets;
26 
27  /* "assert" that num_subsets is smaller than num labels */
28  if (labels->get_num_labels()<num_subsets)
29  {
30  SG_ERROR("Only %d labels for %d subsets!\n", labels->get_num_labels(),
31  num_subsets);
32  }
33 
34  m_labels=labels;
36 
37  reset_subsets();
38 }
39 
41 {
42  if (m_subset_indices)
44 
47 
48  /* construct all arrays */
49  for (index_t i=0; i<m_num_subsets; ++i)
51 
52  m_is_filled=false;
53 }
54 
55 void CSplittingStrategy::init()
56 {
57  m_labels=NULL;
58  m_subset_indices=NULL;
60  m_is_filled=false;
61  m_num_subsets=0;
62 
63  m_parameters->add((CSGObject**)&m_labels, "labels", "Labels for subsets");
64  m_parameters->add((CSGObject**)&m_subset_indices, "subset_indices",
65  "Set of sets of subset indices");
66  m_parameters->add(&m_is_filled, "is_filled", "Whether ther are index sets");
67  m_parameters->add(&m_num_subsets, "num_subsets", "Number of index sets");
68 }
69 
71 {
74 }
75 
77 {
78  if (!m_is_filled)
79  {
80  SG_ERROR("Call %s::build_subsets() before accessing them! If this error"
81  " stays, its an implementation error of %s::build_subsets()\n",
82  get_name(), get_name());
83  }
84 
85  /* construct SGVector copy from index vector */
88 
89  index_t num_elements=to_copy->get_num_elements();
90  SGVector<index_t> result(num_elements, true);
91 
92  /* copy data */
93  memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements);
94 
95  SG_UNREF(to_copy);
96 
97  return result;
98 }
99 
101 {
102  if (!m_is_filled)
103  {
104  SG_ERROR("Call %s::build_subsets() before accessing them! If this error"
105  " stays, its an implementation error of %s::build_subsets()\n",
106  get_name(), get_name());
107  }
108 
110  m_subset_indices->get_element_safe(subset_idx);
111 
112  SGVector<index_t> result(
113  m_labels->get_num_labels()-to_invert->get_num_elements(), true);
114 
115  index_t index=0;
116  for (index_t i=0; i<m_labels->get_num_labels(); ++i)
117  {
118  /* add i to inverse indices if it is not in the to be inverted set */
119  if (to_invert->find_element(i)==-1)
120  result.vector[index++]=i;
121  }
122 
123  SG_UNREF(to_invert);
124 
125  return result;
126 }
127 
129 {
131 }

SHOGUN Machine Learning Toolbox - Documentation