forked from oir/soft-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassify.cpp
More file actions
182 lines (149 loc) · 3.91 KB
/
classify.cpp
File metadata and controls
182 lines (149 loc) · 3.91 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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>
#include <map>
#include "SoftTree.cpp"
#include "common.cpp"
map<string, double> classes;
void readFromFile(string filename, vector< vector<double> > &X, vector<double> &Y) {
ifstream file;
file.open(filename.c_str());
assert(file.is_open());
string word; // class label might be any string
vector<double> row;
while(!file.eof())
{
file >> word;
if (file.peek() == '\n') { // if line ends, 'val' is the response value
if (classes.find(word) == classes.end())
classes[word] = classes.size()-1;
Y.push_back((double)(classes[word]));
X.push_back(row);
row.clear();
} else { // else, it is an entry of the feature vector
row.push_back(str2double(word));
}
}
}
void normalize(vector<vector<double> > &X, vector<vector<double> > &V, vector<vector<double> > &U)
{
for (int dim=0; dim<X[0].size(); dim++)
{
double mean = 0;
for (int i=0; i<X.size(); i++)
mean += X[i][dim];
mean /= (X.size());
double std = 0;
for (int i=0; i<X.size(); i++) {
std += (X[i][dim]-mean)*(X[i][dim]-mean);
X[i][dim] -= mean;
}
for (int i=0; i<V.size(); i++) {
V[i][dim] -= mean;
}
for (int i=0; i<U.size(); i++) {
U[i][dim] -= mean;
}
std /= (X.size()-1);
std = sqrt(std);
for (int i=0; i<X.size(); i++) {
X[i][dim] /= std;
if (isnan(X[i][dim])) cout << "nan" <<endl;
//cout << X[i][dim] << " ";
}
for (int i=0; i<V.size(); i++) {
V[i][dim] /= std;
if (isnan(V[i][dim])) cout << "nan" <<endl;
//cout << V[i][dim] << " ";
}
for (int i=0; i<U.size(); i++) {
U[i][dim] /= std;
if (isnan(U[i][dim])) cout << "nan" <<endl;
//cout << U[i][dim] << " ";
}
//cout << endl;
}
}
void normalize(vector<double> &X, vector<double> &V, vector<double> &U)
{
double mean = 0;
for (int i=0; i<X.size(); i++)
mean += X[i];
mean /= (X.size());
double std = 0;
for (int i=0; i<X.size(); i++) {
std += (X[i]-mean)*(X[i]-mean);
X[i] -= mean;
}
for (int i=0; i<V.size(); i++)
V[i] -= mean;
for (int i=0; i<U.size(); i++)
U[i] -= mean;
std /= (X.size()-1);
std = sqrt(std);
for (int i=0; i<X.size(); i++) {
X[i] /= std;
if (isnan(X[i])) cout << "nan" <<endl;
//cout << X[i][dim] << " ";
}
for (int i=0; i<V.size(); i++) {
V[i] /= std;
if (isnan(V[i])) cout << "nan" <<endl;
//cout << V[i][dim] << " ";
}
for (int i=0; i<U.size(); i++) {
U[i] /= std;
if (isnan(U[i])) cout << "nan" <<endl;
//cout << U[i][dim] << " ";
}
}
int main(int argc, char *argv[])
{
assert(argc >= 3);
uint fold = atoi(argv[2]);
string name = (string)argv[1];
uint m = (fold/2)+1;
uint n = (fold%2)+1;
vector< vector< double> > X, V, U;
vector<double> Y, R, T;
//srand(time(NULL)); // random seed
srand(123457);
cout.precision(5); // # digits after decimal pt
cout.setf(ios::fixed,ios::floatfield);
string dataset = "data/" + name + "/" + name;
string filename;
filename = dataset
+ "-train-"
+ (char)(m+'0')
+ '-'
+ (char)(n+'0')
+ ".txt";
readFromFile(filename, X, Y);
filename = dataset
+ "-validation-"
+ (char)(m+'0')
+ '-'
+ (char)(n+'0')
+ ".txt";
readFromFile(filename, V, R);
filename = dataset + "-test.txt";
readFromFile(filename, U, T);
normalize(X, V, U);
//normalize(Y, R, T);
SoftTree st(X, Y, V, R, 'c');
double y;
double acc=0;
//ofstream outf("out");
acc = 1-st.errRate(X, Y);
cout << "SRT: ";
cout << "n: " << st.size() << "\t";
cout << "tra: " << acc << "\t";
acc = 1-st.errRate(V, R);
cout << "val: " << acc << "\t";
acc = 1-st.errRate(U, T);
cout << "tst: " << acc << "\t";
cout << endl;
return 0;
}