-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
358 lines (283 loc) · 12.3 KB
/
main.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* File: main.cpp
* Author: heshan
*
* Created on April 8, 2018, 12:46 PM
*/
#include <vector>
#include "LSTMNet.h"
#include "DataProcessor.h"
#include "FileProcessor.h"
int univarPredicts() {
int memCells = 5; // number of memory cells
int trainDataSize = 300; // train data size
int inputVecSize = 60; // input vector size
int timeSteps = 60; // unfolded time steps
float learningRate = 0.01;
int predictions = 1300; // prediction points
int iterations = 10; // training iterations with training data
// Adding the time series in to a vector and preprocessing
DataProcessor * dataproc;
dataproc = new DataProcessor();
FileProcessor * fileProc;
fileProc = new FileProcessor();
std::vector<double> timeSeries;
////////// Converting the CVS ////////////////////////
// fileProc->writeUniVariate("datasets/internetTrafficData.csv","datasets/InternetTraff.txt",2,1);
// fileProc->writeUniVariate("datasets/monthlyReturnsOfValueweighted.csv","datasets/monthlyReturnsOfValueweighted.txt",2,1);
// fileProc->writeUniVariate("datasets/treeAlmagreMountainPiarLocat.csv","datasets/treeAlmagreMountainPiarLocat.txt",2,1);
// fileProc->writeUniVariate("datasets/dailyCyclistsAlongSudurlandsb.csv","datasets/dailyCyclistsAlongSudurlandsb.txt",2,1);
// fileProc->writeUniVariate("datasets/totalPopulation.csv","datasets/totalPopulation.txt",2,1);
// fileProc->writeUniVariate("datasets/numberOfUnemployed.csv","datasets/numberOfUnemployed.txt",2,1);
// fileProc->writeUniVariate("datasets/data.csv","datasets/data.txt",2,1);
// fileProc->writeUniVariate("datasets/monthlySunspotNumbers.csv","datasets/monthlySunspotNumbers.txt",2,1);
// fileProc->writeUniVariate("datasets/dailyMinimumTemperatures.csv","datasets/dailyMinimumTemperatures.txt",2,1);
///////////// Data Sets //////////////////////////////
std::string datasets[] = {
/* 0*/ "dummy2.txt",
/* 1*/ "InternetTraff.txt",
/* 2*/ "monthlyReturnsOfValueweighted.txt",
/* 3*/ "treeAlmagreMountainPiarLocat.txt",
/* 4*/ "dailyCyclistsAlongSudurlandsb.txt",
/* 5*/ "totalPopulation.txt",
/* 6*/ "numberOfUnemployed.txt",
/* 7*/ "data.txt",
/* 8*/ "monthlySunspotNumbers.txt",
/* 9*/ "dailyMinimumTemperatures.txt",
/*10*/ "hr2.txt"
};
std::string datasets2[] = {
/* 0*/ "dummy2Anml.txt",
/* 1*/ "dailyMinimumTemperaturesAnml.txt"
};
std::string inFile = datasets[1];
timeSeries = fileProc->read("datasets/univariate/input/"+inFile,1);
timeSeries = dataproc->process(timeSeries,1);
// Creating the input vector Array
std::vector<double> * input;
input = new std::vector<double>[trainDataSize];
std::vector<double> inputVec;
for (int i = 0; i < trainDataSize; i++) {
inputVec.clear();
for (int j = 0; j < inputVecSize; j++) {
inputVec.push_back(timeSeries.at(i+j));
}
inputVec = dataproc->process(inputVec,0);
input[i] = inputVec;
}
// Creating the target vector using the time series
std::vector<double>::const_iterator first = timeSeries.begin() + inputVecSize;
std::vector<double>::const_iterator last = timeSeries.begin() + inputVecSize + trainDataSize;
std::vector<double> targetVector(first, last);
// Training the LSTM net
LSTMNet lstm(memCells,inputVecSize);
lstm.train(input, targetVector, trainDataSize, timeSteps, learningRate, iterations);
// Open the file to write the time series predictions
std::ofstream out_file;
out_file.open("datasets/univariate/predictions/"+inFile,std::ofstream::out | std::ofstream::trunc);
std::vector<double> inVec;
input = new std::vector<double>[1];
double result;
double expected;
//double MSE = 0;
for (int i = 0; i < inputVecSize; i++) {
out_file<<dataproc->postProcess(timeSeries.at(i))<<"\n";
}
std::cout << std::fixed;
for (int i = 0; i < predictions; i++) {
inVec.clear();
for (int j = 0; j < inputVecSize; j++) {
inVec.push_back(timeSeries.at(i+j));
}
inVec = dataproc->process(inVec,0);
input[0] = inVec;
result = lstm.predict(input);
std::cout<<std::endl<<"result: "<<result<<std::endl;
expected = timeSeries.at(i+inputVecSize+1);
//MSE += std::pow(expected-result,2);
result = dataproc->postProcess(result);
out_file<<result-20000<<"\n";
std::cout<<"result processed: "<<result<<std::endl<<std::endl;
}
//MSE /= predictions;
//std::cout<<"Mean Squared Error: "<<MSE<<"\n";
std::cout << std::scientific;
return 0;
}
int multivarPredicts() {
///////////////////////// Multivariate time series data prediction ////////////////////////////////////
int memCells = 10; // number of memory cells
int inputVecSize = 5; // input vector size
int trainDataSize = 5000; // train data size
int timeSteps = 1; // data points used for one forward step
float learningRate = 0.0001;
int iterations = 10; // training iterations with training data
int lines = 5000;
DataProcessor * dataproc;
dataproc = new DataProcessor();
FileProcessor * fileProc;
fileProc = new FileProcessor();
int colIndxs[] = {0,0,1,1,1,1,1};
int targetValCol = 7;
std::vector<double> * timeSeries;
timeSeries = fileProc->readMultivariate("datasets/multivariate/input/occupancyData/datatraining.txt",lines,inputVecSize,colIndxs,targetValCol);
// Creating the input vector Array
std::vector<double> * input;
input = new std::vector<double>[trainDataSize];
for (int i = 0; i < trainDataSize; i++) {
input[i] = dataproc->process(timeSeries[i],0);
}
// Creating the target vector using the time series
std::vector<double>::const_iterator first = timeSeries[lines].begin();
std::vector<double>::const_iterator last = timeSeries[lines].begin() + trainDataSize;
std::vector<double> targetVector(first, last);
for (std::vector<double>::iterator it = targetVector.begin(); it != targetVector.end(); ++it) {
if (*it == 0) *it = -1;
}
// Training the LSTM net
LSTMNet * lstm = new LSTMNet(memCells,inputVecSize);
lstm->train(input, targetVector, trainDataSize, timeSteps, learningRate, iterations);
// Predictions
int predictions = 2000; // prediction points
lines = 2000; // lines read from the files
timeSeries = fileProc->readMultivariate("datasets/multivariate/input/occupancyData/datatest.txt",lines,inputVecSize,colIndxs,targetValCol);
input = new std::vector<double>[1];
double result;
double min = 0, max = 0;
std::vector<double> resultVec;
for (int i = 0; i < predictions; i++) {
input[0] = dataproc->process(timeSeries[i],0);
result = lstm->predict(input);
resultVec.push_back(result);
//std::cout<<std::endl<<"result: "<<result<<" ==> expected: "<<timeSeries[lines].at(i)<<std::endl;
if (i == 0){
min = result;
max = result;
} else {
if (min > result) min = result;
if (max < result) max = result;
}
}
std::cout<<"min: "<<min<<std::endl;
std::cout<<"max: "<<max<<std::endl;
double line = 0; //(min + max)/2;
std::cout<<"margin: "<<line<<std::endl<<std::endl;
int occu = 0, notoccu = 0;
int corr = 0;
int incorr = 0;
int truePos = 0;
int falsePos = 0;
int trueNeg = 0;
int falseNeg = 0;
int corrNwMgn = 0;
int incorrNwMgn = 0;
// Open the file to write the time series predictions
std::ofstream out_file;
std::ofstream out_file2;
out_file.open("datasets/multivariate/predictions/occupancyData/multiResults.txt",std::ofstream::out | std::ofstream::trunc);
out_file2.open("datasets/multivariate/predictions/occupancyData/multiTargets.txt",std::ofstream::out | std::ofstream::trunc);
for (int i = 0; i < predictions; i++) {
out_file<<timeSeries[lines].at(i)<<","<<resultVec.at(i)<<"\n";
out_file2<<timeSeries[lines].at(i)<<",";
if (timeSeries[lines].at(i) == 1) {
out_file2<<1<<"\n";
} else out_file2<<-1<<"\n";
if ( (resultVec.at(i) > line) && (timeSeries[lines].at(i) == 1)) {
corr++;
truePos++;
occu++;
} else if ( (resultVec.at(i) <= line) && (timeSeries[lines].at(i) == 0)) {
corr++;
trueNeg++;
notoccu++;
} else if ( (resultVec.at(i) <= line) && (timeSeries[lines].at(i) == 1)) {
incorr++;
falseNeg++;
occu++;
} else if ( (resultVec.at(i) > line) && (timeSeries[lines].at(i) == 0)) {
incorr++;
falsePos++;
notoccu++;
}
//std::cout<<resultVec.at(i)<<" ------ "<<timeSeries[lines].at(i)<<"\n";
}
std::cout<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Data "<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Occupied: "<<occu<<std::endl;
std::cout<<"NotOccupied: "<<notoccu<<std::endl<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"margin: "<<line<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Correct predictions: "<<corr<<std::endl;
std::cout<<"Incorrect predictions: "<<incorr<<std::endl<<std::endl;
std::cout<<"True Positive: "<<truePos<<std::endl;
std::cout<<"True Negative: "<<trueNeg<<std::endl;
std::cout<<"False Positive: "<<falsePos<<std::endl;
std::cout<<"False Negative: "<<falseNeg<<std::endl;
std::cout<<std::endl<<"Accuracy: "<<(corr/(double)predictions)*100<<"%"<<std::endl<<std::endl;
line = (min + max)/2;
occu = 0;
notoccu = 0;
corr = 0;
incorr = 0;
truePos = 0;
falsePos = 0;
trueNeg = 0;
falseNeg = 0;
for (int i = 0; i < predictions; i++) {
if ( (resultVec.at(i) > line) && (timeSeries[lines].at(i) == 1)) {
corr++;
truePos++;
occu++;
} else if ( (resultVec.at(i) <= line) && (timeSeries[lines].at(i) == 0)) {
corr++;
trueNeg++;
notoccu++;
} else if ( (resultVec.at(i) <= line) && (timeSeries[lines].at(i) == 1)) {
incorr++;
falseNeg++;
occu++;
} else if ( (resultVec.at(i) > line) && (timeSeries[lines].at(i) == 0)) {
incorr++;
falsePos++;
notoccu++;
}
if (line > 0) {
if ( (resultVec.at(i) <= line) && (resultVec.at(i) > 0)) {
if (timeSeries[lines].at(i) == 0) {
corrNwMgn++;
} else incorrNwMgn++;
}
} else {
if ( (resultVec.at(i) > line) && (resultVec.at(i) < 0)) {
if (timeSeries[lines].at(i) == 1) {
corrNwMgn++;
} else incorrNwMgn++;
}
}
}
std::cout<<"----------------------"<<std::endl;
std::cout<<"margin: "<<line<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Correct predictions: "<<corr<<std::endl;
std::cout<<"Incorrect predictions: "<<incorr<<std::endl<<std::endl;
std::cout<<"True Positive: "<<truePos<<std::endl;
std::cout<<"True Negative: "<<trueNeg<<std::endl;
std::cout<<"False Positive: "<<falsePos<<std::endl;
std::cout<<"False Negative: "<<falseNeg<<std::endl;
std::cout<<std::endl<<"Accuracy: "<<(corr/(double)predictions)*100<<"%"<<std::endl<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Within the new margin and 0"<<std::endl;
std::cout<<"----------------------"<<std::endl;
std::cout<<"Correct: "<<corrNwMgn<<std::endl;
std::cout<<"Incorrect: "<<incorrNwMgn<<std::endl<<std::endl<<std::endl;
return 0;
}
int main() {
// predicting univariate time series
univarPredicts();
// predicting multivariate series
//multivarPredicts();
}