-
Notifications
You must be signed in to change notification settings - Fork 0
/
bnb.cpp
202 lines (184 loc) · 5.45 KB
/
bnb.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
// SWAMI KARUPPASWAMI THUNNAI
#include "bnb.h"
#include <sstream>
#include <bits/stdc++.h>
void bnb::print(std::string message)
{
if (DEBUG) std::cout << message << "\n";
}
void bnb::get_unique_labels()
{
print("Getting unqiue labels...");
for (unsigned long int i : y)
{
unique_labels.insert(i);
}
if (DEBUG)
{
std::cout << unique_labels.size() << " unique lables found\n";
}
}
void bnb::get_probabilities_of_y()
{
print("Getting probabilities of y...");
unsigned long int y_size = y.size();
std::map<unsigned long int, unsigned long int> label_count;
for (unsigned long int label : unique_labels)
{
label_count[label] = 0;
}
for (unsigned long int i : y)
{
label_count[i]++;
}
std::map<unsigned long int, unsigned long int>::iterator itr1 = label_count.begin();
std::map<unsigned long int, unsigned long int>::iterator itr2 = label_count.end();
for (std::map<unsigned long int, unsigned long int>::iterator itr = itr1; itr != itr2; ++itr)
{
y_prob[itr->first] = itr->second / double(y.size());
std::stringstream stream;
stream << "p(" << itr->first << ") = " << y_prob[itr->first];
print(stream.str());
}
}
void bnb::update_icl_count(unsigned long int X_val, unsigned long int y_val, std::vector<__individual_column_label_count>& icl)
{
for (unsigned long int i = 0; i < icl.size(); i++)
{
__individual_column_label_count icl_count = icl[i];
if ((icl_count.get_column_value() == X_val) && (icl_count.get_label_value() == y_val))
{
icl_count.increment_count();
icl[i] = icl_count;
}
}
}
/*void bnb::calculate_X_prob()
{
for (unsigned long int label : unique_labels)
{
double p = y_prob[label];
if (p == 0) p = 1;
std::cout << "p = " << p << "\n";
std::map<unsigned long int, std::vector<__Xi_probability>>::iterator itr1 = individual_probability.begin();
std::map<unsigned long int, std::vector<__Xi_probability>>::iterator itr2 = individual_probability.end();
for (std::map<unsigned long int, std::vector<__Xi_probability>>::iterator itr = itr1; itr != itr2; ++itr)
{
std::vector<__Xi_probability> Xp = itr->second;
for (__Xi_probability probability : Xp)
{
if (probability.get_y() == label)
{
if (probability.get_p() != 0)
{
p *= probability.get_p();
std::cout << "p = " << p << "\n";
}
}
}
}
X_prob[label] = p;
}
if (DEBUG)
{
for (auto i : X_prob)
{
std::cout << "P(X|" << i.first << ")*P(" << i.first << ") = " << i.second << "\n";
}
}
}*/
void bnb::get_column_probability(unsigned long int column_index)
{
// Contains the column itself
std::vector<unsigned long int> column_vector;
// Contains only the unique values of the column
std::set<unsigned long int> unique_column_values;
for (std::vector<unsigned long int> i : X)
{
column_vector.push_back(i[column_index]);
unique_column_values.insert(i[column_index]);
}
std::vector<__individual_column_label_count> icl_count;
for (unsigned long int i : unique_column_values)
{
for (unsigned long int j : unique_labels)
{
__individual_column_label_count count(i, j, 0);
icl_count.push_back(count);
}
}
for (unsigned long int j = 0; j < column_vector.size(); j++)
{
unsigned long int X_val = column_vector[j];
unsigned long int y_val = y[j];
//std::cout << "Updating for X " << X_val << " and y " << y_val << "\n";
update_icl_count(X_val, y_val, icl_count);
}
std::vector<__Xi_probability> xi_prob;
// After individual col value count is found
for (__individual_column_label_count i : icl_count)
{
unsigned long int X_val = i.get_column_value();
unsigned long int y_val = i.get_label_value();
unsigned long int count = i.get_count();
double total_count = std::count(y.begin(), y.end(), y_val);
//std::cout << X_val << " - " << y_val << " - " << count << " - " << total_count << "\n";
double p = count / total_count;
__Xi_probability x(X_val, y_val, p);
xi_prob.push_back(x);
std::stringstream stream;
stream << "Col: " << column_index << " for " << i.get_column_value() << " and " << i.get_label_value() << " p(" << X_val << "/" << y_val << ")= " << count << "/" << total_count << " which is " << p;
print(stream.str());
}
individual_probability[column_index] = xi_prob;
}
void bnb::get_indiviudual_probabilities()
{
// Get the probabilities for each features in the dataset
for (unsigned long int column_index = 0; column_index < X[0].size(); column_index++)
{
get_column_probability(column_index);
}
}
void bnb::fit()
{
// Get the unique lables of y
get_unique_labels();
// Then find the probabilities of each unique variables
get_probabilities_of_y();
// Getting the individual probabilities
get_indiviudual_probabilities();
}
std::map<unsigned long int, double> bnb::predict(std::vector<unsigned long int> test)
{
std::map<unsigned long int, double> prediction;
for (unsigned long int label : unique_labels)
{
prediction[label] = y_prob[label];
}
// Check the below method
for (unsigned long int i=0; i<test.size(); i++)
{
unsigned long int X_val = test[i];
std::vector<__Xi_probability> p = individual_probability[i];
for (__Xi_probability j : p)
{
if (j.get_X() == X_val)
{
std::cout << X_val << " matches " << j.get_X() << " and y " << j.get_y() << " and p " << j.get_p() << "\n";
prediction[j.get_y()] *= j.get_p();
}
}
}
double total = 0.0;
for (auto i: prediction)
{
total += i.second;
}
for (auto i : prediction)
{
// std::cout << i.first << " -- " << i.second << "\n";
prediction[i.first] = i.second / total;
}
return prediction;
}