-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
executable file
·130 lines (112 loc) · 3.47 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
#include "ktiming.h"
#include "Histogram.h"
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
vector<int> readY(const string& filename) {
// Read in labels from y training data.
vector<int> labels;
ifstream file(filename.c_str());
if (!file.is_open()) {
cout << "Error opening file y. " << endl;
}
string line;
while (getline(file, line)) {
stringstream lineStream(line);
string cell;
while (std::getline(lineStream, cell, ',')) {
labels.push_back(stoi(cell));
}
}
file.close();
return labels;
}
vector<int> readYLetter(const string& filename) {
// Read in labels from y training data.
vector<int> labels;
ifstream file(filename.c_str());
if (!file.is_open()) {
cout << "Error opening file y. " << endl;
}
string line;
while (getline(file, line)) {
stringstream lineStream(line);
string cell;
while (std::getline(lineStream, cell, ',')) {
const char *cur = cell.c_str();
// cout << cur[0] << endl;
labels.push_back(cur[0] - 64);
}
}
file.close();
return labels;
}
vector<vector<double> > readX(const string& filename) {
// Read in features from x training data.
vector<vector<double> > features;
ifstream filex(filename.c_str());
if (!filex.is_open()) {
cout << "Error opening file x. " << endl;
}
string line;
while (getline(filex, line)) {
stringstream lineStream(line);
string cell;
vector<double> curFeature;
while (std::getline(lineStream, cell, ',')) {
curFeature.push_back(stod(cell));
}
features.push_back(curFeature);
}
filex.close();
return features;
}
vector<int> getOrigIndex(const vector<int> labels ) {
vector<int> origIndex;
for (int i = 0; i < labels.size(); ++i) {
origIndex.push_back(i);
}
return origIndex;
}
int main(int argv, char **argc) {
printf("Main. \n");
// vector<int> labels = readY("./data/yTr.csv");
// vector<vector<double>> features = readX("./data/xTr.csv");
vector<int> labels = readYLetter("./data/letter_yTr700.csv");
vector<vector<double>> features = readX("./data/letter_xTr700.csv");
// clockmark_t begin_rm = ktiming_getmark();
// Node *root = histTree(features, labels);
// clockmark_t end_rm = ktiming_getmark();
// printf("Elapsed time in seconds: %f\n", ktiming_diff_sec(&begin_rm, &end_rm));
vector<int> origIndex = getOrigIndex(labels);
vector<double> weights;
for (int i = 0; i < labels.size(); ++i) {
weights.push_back(1.0);
}
vector<int> origFeatureIndex;
for (int j = 0; j < features.size(); ++j) {
origFeatureIndex.push_back(j);
}
Node *root = new Node();
clockmark_t begin_rm = ktiming_getmark();
// buildTree(root, 1, labels, features, origIndex, origFeatureIndex);
rfOutput* output = randomForestHist(features, labels, 4, 50);
clockmark_t end_rm = ktiming_getmark();
printf("Elapsed time in seconds: %f\n", ktiming_diff_sec(&begin_rm, &end_rm));
// vector<int> labelsTest = readY("./data/yTe.csv");
// vector<vector<double>> featuresTest = readX("./data/xTe.csv");
vector<int> labelsTest = readYLetter("./data/letter_yTe300.csv");
vector<vector<double> > featuresTest = readX("./data/letter_xTe300.csv");
vector<int> pred = evalForestHist(output, 50, featuresTest);
int numTest = labelsTest.size();
int same = 0;
for (int i = 0; i < numTest; ++i) {
if (labelsTest[i] == pred[i]) {
same++;
}
}
double accuracy = 1.0 * same / numTest;
cout << "accuracy: " << accuracy << endl;
return 0;
}