-
Notifications
You must be signed in to change notification settings - Fork 1
/
classifier.cpp
executable file
·149 lines (123 loc) · 3.86 KB
/
classifier.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
#include <iostream>
#include <sstream>
#include <fstream>
#include <math.h>
#include <vector>
#include "classifier.h"
/**
* Initializes GNB
*/
GNB::GNB() {
}
GNB::~GNB() {}
const std::map<std::string, int> GNB::label_values= {{"left", 0}, {"keep", 1}, {"right", 2}};
void GNB::train(vector<vector<double>> data, vector<string> labels)
{
/*
Trains the classifier with N data points and labels.
INPUTS
data - array of N observations
- Each observation is a tuple with 4 values: s, d,
s_dot and d_dot.
- Example : [
[3.5, 0.1, 5.9, -0.02],
[8.0, -0.3, 3.0, 2.2],
...
]
labels - array of N labels
- Each label is one of "left", "keep", or "right".
*/
int c_value;
double s, d, s_dot, d_dot;
// double means[n_features];
int n_data = labels.size();
// init
for (int j = 0; j < N_CLASSES; j++) {
class_count[j] = 0;
for (int k = 0; k < N_FEATURES; k++) {
means[j][k] = 0;
vars[j][k] = 0;
}
}
// Calculate the means
for (int i = 0; i< n_data; i++) {
c_value = label_values.at(labels[i]);
std::cout << "training label " << labels[i] << "," << c_value
<< "(" << data[i][0]<< ", "
<< data[i][1] << ", "
<< data[i][2] << ", "
<< data[i][3] << ") "
<< std::endl;
class_count[c_value]++;
for (int k =0; k < N_FEATURES; k++) {
means[c_value][k]+=data[i][k];
}
}
for (int j = 0; j < N_CLASSES; j++) {
for (int k =0; k < N_FEATURES; k++) {
means[j][k] /= class_count[j];
}
}
// Calculate the variances
double diff;
for (int i = 0; i < n_data; i ++) {
c_value = label_values.at(labels[i]);
for (int k = 0; k < N_FEATURES; k++) {
diff = data[i][k] - means[c_value][k];
vars[c_value][k] += diff * diff;
}
}
for (int j = 0 ; j < N_CLASSES; j++) {
for (int k = 0; k < N_FEATURES; k++) {
vars[j][k] /= class_count[j];
}
priors[j] = float(class_count[j]) / n_data;
}
cout << "total data counts: " << n_data << std::endl;
for (int j = 0; j < N_CLASSES; j++) {
std::cout << "priors" << j << ":" << priors[j] << std::endl;
std::cout << "class counts " << j << ":" << class_count[j] << std::endl;
std::cout << "means " << std::endl;
for (int k = 0; k < N_FEATURES; k++) {
std::cout << k << ":" << means[j][k] << " ";
}
std::cout << std::endl;
std::cout << "vars ";
for (int k = 0; k < N_FEATURES; k++) {
std::cout << k << ":" << vars[j][k] << " ";
}
std::cout << std::endl;
}
}
string GNB::predict(vector<double> obs)
{
/*
Once trained, this method is called and expected to return
a predicted behavior for the given observation.
INPUTS
observation - a 4 tuple with s, d, s_dot, d_dot.
- Example: [3.5, 0.1, 8.5, -0.2]
OUTPUT
A label representing the best guess of the classifier. Can
be one of "left", "keep" or "right".
"""
*/
std::cout << "predictions: ";
for (int j = 0; j < N_CLASSES; j++) {
posteriors[j] = priors[j];
for (int k = 0; k < N_FEATURES; k++) {
posteriors[j] *= gauss(obs[k], means[j][k], vars[j][k]);
}
std::cout << priors[j] << " " << posteriors[j] << " ";
}
std::cout << std::endl;
int max_index = std::distance(std::begin(posteriors),
std::max_element(std::begin(posteriors),
std::end(posteriors)));
cout << "predicts " << max_index << " " << possible_labels[max_index] << std::endl;
return this->possible_labels[max_index];
}
double GNB::gauss(double x, double m, double v) {
double d = x - m;
return 1/sqrt(2*M_PI*v)*exp(-1.0 *(d*d)/(2*v));
}