SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
evaluation
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
11
#include <
shogun/evaluation/SplittingStrategy.h
>
12
#include <
shogun/labels/Labels.h
>
13
14
using namespace
shogun;
15
16
CSplittingStrategy::CSplittingStrategy
()
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;
35
SG_REF
(
m_labels
);
36
37
reset_subsets
();
38
}
39
40
void
CSplittingStrategy::reset_subsets
()
41
{
42
if
(
m_subset_indices
)
43
SG_UNREF
(
m_subset_indices
);
44
45
m_subset_indices
=
new
CDynamicObjectArray
();
46
SG_REF
(
m_subset_indices
);
47
48
/* construct all arrays */
49
for
(
index_t
i=0; i<
m_num_subsets
; ++i)
50
m_subset_indices
->
append_element
(
new
CDynamicArray<index_t>
());
51
52
m_is_filled
=
false
;
53
}
54
55
void
CSplittingStrategy::init()
56
{
57
m_labels
=NULL;
58
m_subset_indices
=NULL;
59
SG_REF
(
m_subset_indices
);
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
70
CSplittingStrategy::~CSplittingStrategy
()
71
{
72
SG_UNREF
(
m_labels
);
73
SG_UNREF
(
m_subset_indices
);
74
}
75
76
SGVector<index_t>
CSplittingStrategy::generate_subset_indices
(
index_t
subset_idx)
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 */
86
CDynamicArray<index_t>
* to_copy=(
CDynamicArray<index_t>
*)
87
m_subset_indices
->
get_element_safe
(subset_idx);
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
100
SGVector<index_t>
CSplittingStrategy::generate_subset_inverse
(
index_t
subset_idx)
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
109
CDynamicArray<index_t>
* to_invert=(
CDynamicArray<index_t>
*)
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
128
index_t
CSplittingStrategy::get_num_subsets
()
const
129
{
130
return
m_subset_indices
->
get_num_elements
();
131
}
SHOGUN
Machine Learning Toolbox - Documentation