-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment4.java
474 lines (432 loc) · 11.9 KB
/
Assignment4.java
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class data
{
// String label;
int count;
public data()
{
count=1;
}
}
public class Assignment4 {
/**
* @param args
*/
public static HashMap<String,data> classlabel=new HashMap<String,data>();
static HashMap<Integer,HashMap<String,HashMap<String,data>>> features=new HashMap<Integer, HashMap<String,HashMap<String,data>>>();
//static List<HashMap<String,HashMap<String,data>>> features1=new ArrayList<HashMap<String,HashMap<String,data>>>();
//static List<HashMap<String,HashMap<String,data>>> features2=new ArrayList<HashMap<String,HashMap<String,data>>>();
static List<String> filedata1=new ArrayList<String>();
static List<String> missclasssified=new ArrayList<String>();
static List<String> testdata=new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
//readdata("breast-cancer-wisconsin.data");
readdata("BankMarketing_Discrete.csv");
//Collections.shuffle(filedata1);
System.out.println("Bank Data");
System.out.println("No. of lines readed: "+filedata1.size());
int conmat[][]=new int[2][2];
double accu[]=new double[10];double sum=0.0,max=0.0;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
conmat[i][j]=0;
}
for(int i=0;i<10;i++)
{
randshuffle();
separatedata();
double acc=bayes(conmat);
if(acc>max)
max=acc;
accu[i]=acc;
sum=sum+acc;
features.clear();
testdata.clear();
if(i!=9)
missclasssified.clear();
classlabel.clear();
}
double mean=sum/10;
sum=0;
for(int i=0;i<10;i++)
{
sum=sum+(mean-accu[i])*(mean-accu[i]);
}
double dev=Math.sqrt(sum/10);
System.out.println("Best Accuracy: "+max);
System.out.println("Mean Accuracy: "+mean);
System.out.println("Deviaton Accuracy: "+dev);
System.out.println("Misssclassified Data: "+missclasssified);
System.out.println("Confusion Matrix");
System.out.println(conmat[0][0]+" "+conmat[0][1]);
System.out.println(conmat[1][0]+" "+conmat[1][1]);
filedata1.clear();
missclasssified.clear();
readdata("breast-cancer-wisconsin.data");
System.out.println("Breast Cancer Data");
//System.out.println("No. of lines readed: "+filedata1.size());
int trainingmatrix[][]=new int[filedata1.size()/2+1][10];
int conmat2[][]=new int[2][2];
max=0.0;
for(int j=0;j<10;j++)
{
randshuffle();
separatedata2(trainingmatrix);
// System.out.println(filedata1.size()/2+1);
// System.out.println(classlabel.size());
// System.out.println(classlabel.get("2").count);
// System.out.println(classlabel.get("4").count);
double means[]=new double[10]; //means of all cols
double devs[]=new double[10]; //deviatopns of all cols
for(int i=0;i<10;i++)
{
means[i]=getmean(trainingmatrix,i);
devs[i]=getdev(trainingmatrix,i,means[i]);
}
double acc=bayes2(conmat2,means,devs);
//System.out.println(acc);
if(acc>max)
max=acc;
accu[j]=acc;
sum=sum+acc;
features.clear();
testdata.clear();
if(j!=9)
missclasssified.clear();
classlabel.clear();
}
mean=sum/10;
sum=0;
for(int i=0;i<10;i++)
{
sum=sum+(mean-accu[i])*(mean-accu[i]);
}
dev=Math.sqrt(sum/10);
System.out.println("Best Accuracy: "+max);
System.out.println("Mean Accuracy: "+mean);
System.out.println("Deviaton Accuracy: "+dev);
System.out.println("Misssclassified Data: "+missclasssified);
System.out.println("Confusion Matrix");
System.out.println(conmat2[0][0]+" "+conmat2[0][1]);
System.out.println(conmat2[1][0]+" "+conmat2[1][1]);
}
private static double bayes2(int[][] conmat,double[] means,double[] devs) {
// TODO Auto-generated method stub
int accuracy=0;
int count=0;
//int notin=0;
for(String line:testdata)
{
double ans=1.0,max=0.0,cur=0.0,problabel;
String arr[]=line.split(",");
String maxlabel="";
int len=arr.length;
int size=filedata1.size()/2+1; //training data size
for (Map.Entry<String, data> entry : classlabel.entrySet())
{
ans=1.0;
String label=entry.getKey();
int val=entry.getValue().count;
problabel=1.0*val/size; //p(wi)
//System.out.println(label+" "+val+" "+size+" "+problabel+" ");
for(int i=0;i<len-1;i++) //i=1 ignoring first col
{
if(!features.containsKey(i))
continue;
else
{
if(!features.get(i).containsKey(arr[i]))
{
//System.out.println(i+" "+arr[i]+" feature value not in training data");
continue;
}
else
{
if(!features.get(i).get(arr[i]).containsKey(label))
{
//System.out.println(i+" "+arr[i]+" "+label+" label not in training data");
//notin++;
continue;
}
int c=Integer.parseInt(arr[i]); //if c is zero than add something
double ds=gaussian(c,means[i],devs[i]);
ans=ans*ds; //count of intersection/class lbel count
c=features.get(i).get(arr[i]).get(label).count; //if c is zero than add something
ans=ans*ds*c/val; //count of intersection/class lbel count
}
}
}
cur=ans*problabel;
if(cur>max)
{
max=cur;
maxlabel=label;
}
}
if(maxlabel.equals(arr[len-1])) //maxlabel ==classlabel
{
accuracy++;
if(maxlabel.equals("2"))
conmat[0][0]++;
else
conmat[1][1]++;
}
else
{
if(count<3)
missclasssified.add(line);
if(maxlabel.equals("2") && arr[len-1].equals("4"))
conmat[0][1]++;
else
conmat[1][0]++;
count++;
}
}
//System.out.println("accuracy: "+accuracy);
//System.out.println("Misssclassified Data: "+missclasssified);
//System.out.println("% accuracy: "+accuracy*100.0/testdata.size());
return accuracy*100.0/testdata.size();
}
private static double gaussian(int c, double mean, double dev) {
// TODO Auto-generated method stub
double ans=1/(3.14*2*Math.sqrt(dev));
double p=(c-mean)/dev;
p=p*p;
ans=ans*Math.pow(Math.E, -p);
return ans;
}
private static double getdev(int[][] trainingmatrix, int i,double mean) {
// TODO Auto-generated method stub
int size=filedata1.size()/2+1;
double sum=0;
for(int j=0;j<size;j++)
{
sum=sum+(trainingmatrix[j][i]-mean)*(trainingmatrix[j][i]-mean);
}
return Math.sqrt(sum/size);
}
private static double getmean(int[][] trainingmatrix, int i) {
// TODO Auto-generated method stub
int size=filedata1.size()/2+1;
double sum=0;
for(int j=0;j<size;j++)
{
sum+=trainingmatrix[j][i];
}
return 1.0*sum/size;
}
private static void separatedata2(int [][]trainingmatrix) {
// TODO Auto-generated method stub
int c=0;
int size=filedata1.size();
for(String line:filedata1)
{
if(c<size/2+1) //80% training data
{
String arr[]=line.split(",");
int len=arr.length;
if(!classlabel.containsKey(arr[len-1]))
classlabel.put(arr[len-1], new data());
else
classlabel.get(arr[len-1]).count++;
for(int i=0;i<len-1;i++)
{
trainingmatrix[c][i]=Integer.parseInt(arr[i]);
if(!features.containsKey(i))
{
HashMap<String,data>lmap=new HashMap<String,data>();
lmap.put(arr[len-1], new data());
HashMap<String,HashMap<String,data>> lmap1=new HashMap<String,HashMap<String,data>>();
lmap1.put(arr[i], lmap);
features.put(i, lmap1);
}
else
{
if(!features.get(i).containsKey(arr[i]))
{
HashMap<String,data>lmap=new HashMap<String,data>();
lmap.put(arr[len-1], new data());
features.get(i).put(arr[i], lmap);
}
else
{
if(!features.get(i).get(arr[i]).containsKey(arr[len-1]))
{
features.get(i).get(arr[i]).put(arr[len-1], new data());
}
else
features.get(i).get(arr[i]).get(arr[len-1]).count++;
}
}
}
}
else //test data
{
testdata.add(line);
}
c++;
}
}
/*-------------------Function for Bank DATA---------------------------------------*/
private static double bayes(int[][] conmat) {
// TODO Auto-generated method stub
int accuracy=0;
int count=0;
//int notin=0;
for(String line:testdata)
{
double ans=1.0,max=0.0,cur=0.0,problabel;
String arr[]=line.split(",");
String maxlabel="";
int len=arr.length;
int size=filedata1.size()/2+1; //training data size
for (Map.Entry<String, data> entry : classlabel.entrySet())
{
ans=1.0;
String label=entry.getKey();
int val=entry.getValue().count;
problabel=1.0*val/size; //p(wi)
//System.out.println(label+" "+val+" "+size+" "+problabel+" ");
for(int i=0;i<len-1;i++) //i=1 ignoring first col
{
if(!features.containsKey(i))
continue;
else
{
if(!features.get(i).containsKey(arr[i]))
{
System.out.println(i+" "+arr[i]+" feature value not in training data");
continue;
}
else
{
if(!features.get(i).get(arr[i]).containsKey(label))
{
System.out.println(i+" "+arr[i]+" "+label+" label not in training data");
//notin++;
continue;
}
int c=features.get(i).get(arr[i]).get(label).count; //if c is zero than add something
double ds=1.0*c/val;
ans=ans*ds; //count of intersection/class lbel count
}
}
}
cur=ans*problabel;
if(cur>max)
{
max=cur;
maxlabel=label;
}
}
if(maxlabel.equals(arr[len-1])) //maxlabel ==classlabel
{
accuracy++;
if(maxlabel.equals("\"yes\""))
conmat[0][0]++;
else if(maxlabel.equals("\"no\""))
conmat[1][1]++;
}
else
{
if(count<3)
missclasssified.add(line);
if(maxlabel.equals("\"yes\"") && arr[len-1].equals("\"no\""))
conmat[0][1]++;
else
conmat[1][0]++;
count++;
}
}
//System.out.println("accuracy: "+accuracy);
//System.out.println("Misssclassified Data: "+missclasssified);
//System.out.println("% accuracy: "+accuracy*100.0/testdata.size());
return accuracy*100.0/testdata.size();
}
private static void separatedata() {
// TODO Auto-generated method stub
int c=0;
int size=filedata1.size();
for(String line:filedata1)
{
if(c<size/2+1) //80% training data
{
String arr[]=line.split(",");
int len=arr.length;
if(!classlabel.containsKey(arr[len-1]))
classlabel.put(arr[len-1], new data());
else
classlabel.get(arr[len-1]).count++;
for(int i=0;i<len-1;i++)
{
if(!features.containsKey(i))
{
HashMap<String,data>lmap=new HashMap<String,data>();
lmap.put(arr[len-1], new data());
HashMap<String,HashMap<String,data>> lmap1=new HashMap<String,HashMap<String,data>>();
lmap1.put(arr[i], lmap);
features.put(i, lmap1);
}
else
{
if(!features.get(i).containsKey(arr[i]))
{
HashMap<String,data>lmap=new HashMap<String,data>();
lmap.put(arr[len-1], new data());
features.get(i).put(arr[i], lmap);
}
else
{
if(!features.get(i).get(arr[i]).containsKey(arr[len-1]))
{
features.get(i).get(arr[i]).put(arr[len-1], new data());
}
else
features.get(i).get(arr[i]).get(arr[len-1]).count++;
}
}
}
}
else //test data
{
testdata.add(line);
}
c++;
}
}
private static void randshuffle() {
// TODO Auto-generated method stub
Collections.shuffle(filedata1);
// Collections.shuffle(filedata1);
// Collections.shuffle(filedata1);
}
private static void readdata(String filename) {
// TODO Auto-generated method stub
try {
BufferedReader br=new BufferedReader(new FileReader(filename));
String line=br.readLine();
while(line!=null)
{
if(line.contains("?")) //removing missing values
{
line=br.readLine();
continue;
}
filedata1.add(line);
line=br.readLine();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}