-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
CrossValidationSplitting.cpp
62 lines (50 loc) · 1.74 KB
/
CrossValidationSplitting.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Heiko Strathmann
* Copyright (C) 2012 Berlin Institute of Technology and Max-Planck-Society
*/
#include <shogun/evaluation/CrossValidationSplitting.h>
#include <shogun/labels/Labels.h>
using namespace shogun;
CCrossValidationSplitting::CCrossValidationSplitting() :
CSplittingStrategy()
{
}
CCrossValidationSplitting::CCrossValidationSplitting(
CLabels* labels, index_t num_subsets) :
CSplittingStrategy(labels, num_subsets)
{
}
void CCrossValidationSplitting::build_subsets()
{
/* ensure that subsets are empty and set flag to filled */
reset_subsets();
m_is_filled=true;
/* permute indices */
SGVector<index_t> indices(m_labels->get_num_labels());
indices.range_fill();
CMath::permute(indices, m_rng.get());
index_t num_subsets=m_subset_indices->get_num_elements();
/* distribute indices to subsets */
index_t current_subset=0;
for (index_t i=0; i<indices.vlen; ++i)
{
/* fill current subset */
CDynamicArray<index_t>* current=(CDynamicArray<index_t>*)
m_subset_indices->get_element(current_subset);
/* add element of current index */
current->append_element(indices.vector[i]);
/* unref */
SG_UNREF(current);
/* iterate over subsets */
current_subset=(current_subset+1) % num_subsets;
}
/* finally shuffle to avoid that subsets with low indices have more
* elements, which happens if the number of class labels is not equal to
* the number of subsets (external random state important for threads) */
m_subset_indices->shuffle(m_rng.get());
}