-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVDataFile.h
149 lines (116 loc) · 3.75 KB
/
CSVDataFile.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
#pragma once
#include "DataFileBase.h"
#include "TestStatistics.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <tuple>
namespace Utils {
class CSVDataFile : public DataFileBase
{
public:
bool Open()
{
const std::string name = getFilePath();
file.open(name);
return file.is_open();
}
std::vector<std::string> readLine()
{
std::vector<std::string> res;
std::string line;
std::getline(file, line);
std::stringstream lineStream(line);
std::string field;
while (std::getline(lineStream, field, ','))
res.push_back(field);
return res;
}
std::ifstream& GetFile() { return file; }
private:
std::ifstream file;
};
class IrisDataset : public CSVDataFile {
public:
using Record = std::tuple<double, double, double, double, std::string>;
Record getRecord()
{
const std::vector<std::string> r = readLine();
if (r.size() < 5) return std::make_tuple(0., 0., 0., 0., std::string());
return std::make_tuple(std::stod(r[0]), std::stod(r[1]), std::stod(r[2]), std::stod(r[3]), r[4]);
}
std::vector<Record> getAllRecords()
{
std::vector<Record> res;
while (!GetFile().eof())
{
const Record record = getRecord();
if (std::get<4>(record).empty()) break;
res.emplace_back(record);
}
return res;
}
template<class OutClass> static void Get(const Record& record, OutClass& x, int ind = 0)
{
x(0, ind) = std::get<0>(record);
x(1, ind) = std::get<1>(record);
x(2, ind) = std::get<2>(record);
x(3, ind) = std::get<3>(record);
}
static void Get(const std::vector<Record>& recordSet, Eigen::MatrixXd& x, int i, int ind = 0)
{
Get(recordSet[i], x, ind);
}
template<class Model> static void PrintStats(const std::vector<Record>& records, int nrOutputs, Model& model)
{
TestStatistics setosaStats;
TestStatistics versicolorStats;
TestStatistics virginicaStats;
Eigen::VectorXd in(4);
Eigen::VectorXd out(nrOutputs);
long long int correct = 0;
for (const auto& record : records)
{
Get(record, in, 0);
out(0) = (std::get<4>(record) == "Iris-setosa") ? 1 : 0;
if (nrOutputs > 1) out(1) = (std::get<4>(record) == "Iris-versicolor") ? 1 : 0;
if (nrOutputs > 2) out(2) = (std::get<4>(record) == "Iris-virginica") ? 1 : 0;
Eigen::VectorXd res = model.Predict(in.col(0));
setosaStats.AddPrediction(res(0) > 0.5, out(0) > 0.5);
if (nrOutputs > 1) versicolorStats.AddPrediction(res(1) > 0.5, out(1) > 0.5);
if (nrOutputs > 2) virginicaStats.AddPrediction(res(2) > 0.5, out(2) > 0.5);
CountCorrect(res, out, nrOutputs, correct);
}
setosaStats.PrintStatistics("Setosa");
if (nrOutputs > 1) {
versicolorStats.PrintStatistics("Versicolor");
if (nrOutputs > 2) virginicaStats.PrintStatistics("Virginica");
}
std::cout << "Accuracy (% correct): " << 100.0 * static_cast<double>(correct) / static_cast<double>(records.size()) << "%" << std::endl << std::endl;
}
private:
static double getMax(const Eigen::VectorXd& res, int nrOutputs)
{
double limp = 0.5;
for (int j = 0; j < nrOutputs; ++j)
limp = std::max(limp, res(j));
return limp;
}
static void CountCorrect(const Eigen::VectorXd& res, const Eigen::VectorXd& out, int nrOutputs, long long int& correct)
{
const double limp = getMax(res, nrOutputs);
if (res(0) == limp && out(0) > 0.5) ++correct;
else if (nrOutputs > 1 && res(1) == limp && out(1) > 0.5) ++correct;
else if (nrOutputs > 2 && res(2) == limp && out(2) > 0.5) ++correct;
else if (limp == 0.5 && nrOutputs < 3)
{
// all predictions are either 0.5 or less
bool isCorrect = true;
if (out(0) > 0.5) isCorrect = false;
if (nrOutputs > 1 && out(1) > 0.5) isCorrect = false;
if (isCorrect) ++correct;
}
}
};
}