-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkfold.hpp
More file actions
73 lines (55 loc) · 1.47 KB
/
Copy pathkfold.hpp
File metadata and controls
73 lines (55 loc) · 1.47 KB
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
63
64
65
66
67
68
69
70
71
72
73
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
template<class In>
class Kfold {
public:
Kfold(int k, In _vecto);
template<class Out>
void getFold(int foldNo, Out training, Out testing);
private:
In vecto;
int folds;
vector<int> whichFoldToGo;
};
template<class In>
Kfold<In>::Kfold(int _folds, In _vecto) :
vecto(_vecto), folds(_folds) {
if (folds <= 0)
{
cout<<"No se pueden crear "<< folds<<" folds";
throw;
}
int foldNo = 0;
int cont = 0;
random_shuffle ( vecto.begin(), vecto.end() );
for (auto i = vecto.begin(); i != vecto.end(); i++) {
//if (end-i>=0)cout << "CICLO i: " << end-i << endl;
whichFoldToGo.push_back(++foldNo);
if (foldNo == folds)
foldNo = 0;
cont++;
}
if (foldNo)
{
cout<<"Con este numero de folds (folds="<< folds <<") no se puede dividir en parter iguales"<<endl;
throw;
}
random_shuffle(whichFoldToGo.begin(), whichFoldToGo.end());
}
template<class In>
template<class Out>
void Kfold<In>::getFold(int foldNo, Out training, Out testing) {
int k = 0;
auto i = vecto.begin();
while (i != vecto.end()) {
if (whichFoldToGo[k++] == foldNo) {
*testing++ = *i++;
} else
*training++ = *i++;
}
}