SHOGUN
v2.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
CrossValidationSplitting.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) 2012 Heiko Strathmann
8
* Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society
9
*/
10
11
#include <
shogun/evaluation/CrossValidationSplitting.h
>
12
#include <
shogun/labels/Labels.h
>
13
14
using namespace
shogun;
15
16
CCrossValidationSplitting::CCrossValidationSplitting
() :
17
CSplittingStrategy
(0, 0)
18
{
19
}
20
21
CCrossValidationSplitting::CCrossValidationSplitting
(
22
CLabels
* labels,
index_t
num_subsets) :
23
CSplittingStrategy
(labels, num_subsets)
24
{
25
}
26
27
void
CCrossValidationSplitting::build_subsets
()
28
{
29
/* ensure that subsets are empty and set flag to filled */
30
reset_subsets
();
31
m_is_filled
=
true
;
32
33
/* permute indices */
34
SGVector<index_t>
indices(
m_labels
->
get_num_labels
());
35
indices.
range_fill
();
36
for
(
index_t
i=0; i<indices.vlen; ++i)
37
{
38
CMath::swap
(indices.vector[i],
39
indices.vector[
CMath::random
(0, indices.vlen-1)]);
40
}
41
42
index_t
num_subsets=
m_subset_indices
->
get_num_elements
();
43
44
/* distribute indices to subsets */
45
index_t
current_subset=0;
46
for
(
index_t
i=0; i<indices.vlen; ++i)
47
{
48
/* fill current subset */
49
CDynamicArray<index_t>
* current=(
CDynamicArray<index_t>
*)
50
m_subset_indices
->
get_element
(current_subset);
51
52
/* add element of current index */
53
current->
append_element
(indices.vector[i]);
54
55
/* unref */
56
SG_UNREF
(current);
57
58
/* iterate over subsets */
59
current_subset=(current_subset+1) % num_subsets;
60
}
61
62
/* finally shuffle to avoid that subsets with low indices have more
63
* elements, which happens if the number of class labels is not equal to
64
* the number of subsets */
65
m_subset_indices
->
shuffle
();
66
}
SHOGUN
Machine Learning Toolbox - Documentation