-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBayes.cpp
339 lines (290 loc) · 8.5 KB
/
NaiveBayes.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//============================================================================
// Name : NaiveBayes.cpp
//
//============================================================================
/**************** Check the formula for m-estimate ***********/
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <vector>
#include <map>
#define PI 3.141592654
using namespace std;
vector<string> dataRow;
vector<vector<string> > dataFrameTrain;
vector<vector<string> > dataFrameTest;
vector<vector<string> > kfTrain;
vector<vector<string> > kfTest;
map<string, int> probIdentifiersOK; // for the identifiers vid and pid
map<string, int> probIdentifiersFraud; // for the identifiers vid and pid
map<int, int> probValues;
typedef map<string, int>::iterator stringMapIter;
//Function prototypes
double calculateMean(string,int, vector<vector<string> > );
double calculateSD(string , int , int , vector<vector<string> > );
double calculatePDF(string classifier, int , double , double, int);
int fraudCount = 0, OKCount = 0;
//If we get NA records, we just ignore the records.
void insertRecords()
{ int lineCounter = 0;
ifstream myfile;
myfile.open("sales.csv");
string line;
char * cptr;
//insert all the values in a map <int, vector>
while (getline(myfile, line))
{
int NAFlag = 0,knownLabel = 0;// to populate only known labels in the vector 0 indicates label is known
lineCounter++;
dataRow.clear();
char *c;
c = &line[0];
cptr = strtok(c,",");
dataRow.push_back(cptr);
while (cptr != NULL)
{
cptr = strtok(NULL, ",");
if(cptr != NULL) //&& strcmp(cptr,"NA") != 0)
{
if(strcmp(cptr,"NA") != 0){
dataRow.push_back(cptr);
if((strcmp(cptr,"fraud") == 0) || (strcmp(cptr,"ok") == 0))
{
knownLabel = 0;
}
/*
else{
knownLabel = 1;
}
*/
}
else
{
NAFlag = 1;
}
}// if cptr != NULL
//cout<<dataRow.at(3)<<endl;
}// while cptr != null
if(NAFlag != 1)
{
if (knownLabel != 1)
dataFrameTrain.push_back(dataRow);
else{
dataFrameTest.push_back(dataRow);
}
}
}
//cout<<lineCounter<<"----------------"<<endl;
myfile.close();
return;
}//insert Records
void enumerateDatasets(vector<vector<string> > dataFrame )
{
for(unsigned int ii=0; ii < dataFrame.size(); ii++)
{
string label = dataFrame[ii].at(4);
string salesID = dataFrame[ii].at(0);
string productID = dataFrame[ii].at(1);
if (label.compare("ok") == 0)
{
OKCount++;
if(probIdentifiersOK.count(salesID) > 0)
{
probIdentifiersOK[salesID]++;
}
else
{
probIdentifiersOK[salesID] = 1;
}
if(probIdentifiersOK.count(productID) > 0)
{
probIdentifiersOK[productID]++;
}
else
{
probIdentifiersOK[productID] = 1;
}
}
else if (label.compare("fraud") == 0)
{
fraudCount++;
if(probIdentifiersFraud.count(salesID) > 0)
{
probIdentifiersFraud[salesID]++;
}
else
{
probIdentifiersFraud[salesID] = 1;
}
if(probIdentifiersFraud.count(productID) > 0)
{
probIdentifiersFraud[productID]++;
}
else
{
probIdentifiersFraud[productID] = 1;
}
}
else if (label.compare("unkn") == 0)
{
continue;
}
}// end for ii over dataFrame
}//enumerateDatasets
// Classifier tells the label we are looking for computing the mean for
// parameter takes binary values 0 : quantity
// 1 : value
// Returns -1 on error
double calculateMean(string classifier, int parameter, vector<vector<string> > dataFrame)
{
int quantSum = 0, valueSum = 0;
double quantMean, valueMean;
int quant = 0, value = 0;
int numCounter = 0;
for(unsigned int ii=0; ii < dataFrame.size(); ii++)
{
string label = dataFrame[ii].at(4);
if (parameter == 0){
quant = atoi(dataFrame[ii].at(2).c_str());
if(label.compare(classifier) == 0)
{
quantSum += quant;
numCounter++;
}
}//Parameter == 0
else if (parameter == 1){
value = atoi(dataFrame[ii].at(3).c_str());
if(label.compare(classifier) == 0)
{
valueSum += value;
numCounter++;
}
}//Parameter == 1
}// for ii
if (parameter == 0){
quantMean = (double)quantSum/numCounter;
return quantMean;
}// if param == 0
else if(parameter == 1){
valueMean = (double)valueSum/numCounter;
return valueMean;
}//if param == 1
else
return -1.0;
}// End calculate mean
//Computes the statistical deviation for given field
//If param == 0, compute quant
//If param == 1, compute value
double calculateSD(string classifier, int parameter, int mean, vector<vector<string> > dataFrame)
{
double squareSum = 0.0;
double tempMean = 0.0;
int quant = 0, value = 0;
int numCounter = 0;
for(unsigned int ii=0; ii < dataFrame.size(); ii++)
{
string label = dataFrame[ii].at(4);
if (parameter == 0){
quant = atoi(dataFrame[ii].at(2).c_str());
if(label.compare(classifier) == 0)
{
squareSum += pow(double(mean - quant),2);
numCounter++;
}
}//Parameter == 0
else if (parameter == 1){
value = atoi(dataFrame[ii].at(3).c_str());
if(label.compare(classifier) == 0)
{
squareSum += pow(double(mean - value),2);
numCounter++;
}
}//Parameter == 1
}// for ii
if (parameter == 0){
tempMean = (double)squareSum/numCounter-1;
return sqrt(tempMean);
}// if param == 0
else if(parameter == 1){
tempMean = (double)squareSum/numCounter-1;
return sqrt(tempMean);
}//if param == 1
else
return -1;
}
//The value is 1 e^(-(mean-value)^2/(2SD^2))
// SDxsqrt(2PI)
double calculatePDF(string classifier, int parameter, double mean, double standardDeviation, int unit)
{
double constant = 1/(standardDeviation * sqrt(2 * PI));
double pdf = constant * exp(-(((double)mean - unit) * ((double)mean - unit))/(2 * pow(standardDeviation,2)));
return pdf;
}
int main(int argc, char ** argv){
double mValue = 5; // For m-estimate when the probability comes out to be equal to zero.
double pValue = 0.5;
insertRecords();
enumerateDatasets(dataFrameTrain);
double quantMeanOK = calculateMean("ok", 0, dataFrameTrain);
double quantMeanFraud = calculateMean("fraud", 0, dataFrameTrain);
double valueMeanOK = calculateMean("ok", 1, dataFrameTrain);
double valueMeanFraud = calculateMean("fraud", 1, dataFrameTrain);
/*
cout<<"Mean OK quant "<<quantMeanOK<<" Value "<<valueMeanOK <<endl;
cout<<"Mean Fraud quant"<<quantMeanFraud<<" Value "<<valueMeanFraud <<endl;
*/
double quantSDOK = calculateSD("ok",0,quantMeanOK, dataFrameTrain);
double quantSDFraud = calculateSD("fraud",0,quantMeanFraud, dataFrameTrain);
double valueSDOK = calculateSD("ok",1,valueMeanOK, dataFrameTrain);
double valueSDFraud = calculateSD("fraud",1,valueMeanFraud, dataFrameTrain);
/*
cout<<"SD OK quant "<<quantSDOK<<" Value "<<valueSDOK <<endl;
cout<<"SD Fraud quant"<<quantSDFraud<<" Value "<<valueSDFraud <<endl;
*/
string labels[dataFrameTrain.size()];
double probOK = 0.0,probFraud= 0.0;
//int truePos = 0, falsePos = 0;
int totalOKCounter = 0, totalFraudCounter =0;
int zerocounter = 0;
for(unsigned int i = 0;i< dataFrameTrain.size(); i++)
{
//iterate through labels and predict classes
string vid = dataFrameTrain[i].at(0), pid = dataFrameTrain[i].at(1);
int quant = atoi(dataFrameTrain[i].at(2).c_str()), value = atoi(dataFrameTrain[i].at(3).c_str());
string label = dataFrameTrain[i].at(4);
//calculate probabilities for each of the labels
probOK = ((((double)probIdentifiersOK[vid]/OKCount)+(mValue*pValue))/(OKCount+fraudCount + mValue))*
((((double)probIdentifiersOK[pid]/OKCount)+(mValue*pValue))/(OKCount+fraudCount + mValue))*
calculatePDF("ok", 0, quantMeanOK, quantSDOK, quant)*
calculatePDF("ok", 1, valueMeanOK, valueSDOK, value);
probFraud = ((((double)probIdentifiersFraud[vid]/fraudCount)+(mValue*pValue))/(OKCount+fraudCount + mValue))*
((((double)probIdentifiersFraud[pid]/fraudCount)+(mValue*pValue))/(OKCount+fraudCount + mValue))*
calculatePDF("fraud", 0, quantMeanFraud, quantSDFraud, quant)*
calculatePDF("fraud", 1, valueMeanFraud, valueSDFraud, value);
if(probOK == 0 || probFraud ==0)
{
zerocounter++;
}
if(probOK > probFraud){
labels[i] = "ok";
totalOKCounter++;
}
else if(probFraud > probOK)
{
labels[i] = "fraud";
totalFraudCounter++;
}
/*
if(labels[i].compare(label) == 0){
truePos++;
}
else{
falsePos++;
}
*/
//cout<<labels[i].compare(label)<<endl;
}//End for i = 0 through test
cout<<"OK "<<totalOKCounter << "----"<<" Fraud"<<totalFraudCounter <<endl;
}// main