-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossvalidation.h
155 lines (117 loc) · 4.34 KB
/
crossvalidation.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef CROSSVALIDATION_H
#define CROSSVALIDATION_H
#include "modelselector.h"
#include <random>
#include "svmspp.h"
#include "lassospp.h"
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace std;
class CrossValidation: public ModelSelector{
private:
vector<uint> mTrainIdx;
vector<uint> mTestIdx;
vector<uint> mNumDiv;
public:
CrossValidation(){};
void calc_accuracy(const vector<vector<double> > &aYHats, const vector<double> &aY, vector<double> &aCnt,const uint &aLossType);
void next_train_test();
template<class T> uint select(const vector<double> &aSolutionPath, const Database &aDatabase, PrefixSpan &aPrefix, T &aLearner, const vector<uint> &aOptions){
uint tK = aOptions[0];
uint tAve = aOptions[1];
uint tLossType = aOptions[2];
vector<vector<Event> > tTransaction = aDatabase.get_transaction();
vector<double> tY = aDatabase.get_y();
uint tN = tY.size();
if(tN < tK){
cout << "error:CV,n<k" << '\n';
exit(1);
}
vector<double> tCnt(aSolutionPath.size() - 1, 0);
vector<uint> tNumDiv;
for(uint j = 0; j < tK; ++j){
tNumDiv.push_back((tN + j) / tK);
}
#pragma omp parallel
{
vector<double> tCnt_private(aSolutionPath.size() - 1, 0);
#pragma omp for
for(uint i = 0; i < tAve; ++i){
vector<uint> tTrainIdx;
for(uint j = 0; j < tN; ++j){
tTrainIdx.push_back(j);
}
for(uint j = 0; j < tN; ++j){
uint k = j + (rand() % (tN - j));
swap(tTrainIdx[j], tTrainIdx[k]);
}
k_fold(aSolutionPath, aPrefix, aLearner, tTransaction, tY, tK, tCnt_private, tTrainIdx, tNumDiv,tLossType);
}
#pragma omp critical
{
for(uint j = 0; j < tCnt.size(); ++j){
tCnt[j] += tCnt_private[j];
}
}
}
vector<double>::iterator tIter;
cout << '\n';
switch (tLossType) {
// L2SVM
case 1:
for(uint i = 0; i < tCnt.size(); ++i){
cout << "λ[" << i + 1 << "]=" << aSolutionPath[i + 1] << " " << tCnt[i] << "/" << aOptions[1] * tN << "=" << tCnt[i] / (double) (aOptions[1] * tN) << '\n';
}
tIter = max_element(tCnt.begin(), tCnt.end());
break;
//Lasso
case 2:
for(uint i = 0; i < tCnt.size(); ++i){
cout << "λ[" << i + 1 << "]=" << aSolutionPath[i + 1] << " RMSE:" <<sqrt(tCnt[i] / (double) (aOptions[1] * tN)) << '\n';
}
tIter = min_element(tCnt.begin(), tCnt.end());
break;
default:
std::cout << "error:CV output" << '\n';
}
return distance(tCnt.begin(), tIter) + 1;
}
template<class T> void k_fold(const vector<double> &aSolutionPath, PrefixSpan &aPrefix, T &aLearner, const vector<vector<Event>> &aTransaction, const vector<double> &aY, const uint &aK, vector<double> &aCnt, vector<uint> aTrainIdx, vector<uint> aNumDiv,const uint &aLossType){
vector<uint> tSVMOption = { (uint) aSolutionPath.size() };
#pragma omp parallel
{
PrefixSpan tPrefix_private = aPrefix;
T tLearner_private = aLearner;
#pragma omp for
for(uint i = 0; i < aK; ++i){
vector<uint> tTestIdx;
vector<uint> tTrainIdx = aTrainIdx;
uint tSumIdx = 0;
for(uint j = 0; j < i; ++j){
tSumIdx += aNumDiv[j];
}
copy(tTrainIdx.begin() + tSumIdx, tTrainIdx.begin() + tSumIdx + aNumDiv[i], back_inserter(tTestIdx));
tTrainIdx.erase(tTrainIdx.begin() + tSumIdx, tTrainIdx.begin() + tSumIdx + aNumDiv[i]);
vector<vector<Event> > tTrainTransaction(tTrainIdx.size());
vector<double> tTrainY(tTrainIdx.size());
for(uint j = 0; j < tTrainIdx.size(); ++j){
tTrainTransaction[j] = aTransaction[tTrainIdx[j]];
tTrainY[j] = aY[tTrainIdx[j]];
}
vector<vector<Event>> tTestTransaction(tTestIdx.size());
vector<double> tTestY(tTestIdx.size());
for(uint j = 0; j < tTestIdx.size(); ++j){
tTestTransaction[j] = aTransaction[tTestIdx[j]];
tTestY[j] = aY[tTestIdx[j]];
}
tPrefix_private.init(tTrainTransaction, tTrainY);
vector<vector<double> > tYHats = tLearner_private.get_all_predict(tPrefix_private, aSolutionPath, tTestTransaction, tSVMOption);
calc_accuracy(tYHats, tTestY, aCnt,aLossType);
}
}
}
void k_fold(const vector<double> &aSolutionPath, PrefixSpan &aPrefix, LearnerSPP &aLearner, const vector<vector<Event> > &aTransaction, const vector<double> &aY, const uint &aK, vector<double> &aCnt,const uint &aLossType);
uint select(const vector<double> &aSolutionPath, const Database &aDatabase, PrefixSpan &aPrefix, LearnerSPP &aLearner, const vector<uint> &aOptions);
};
#endif