- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBayesian.java
More file actions
Latest commit
198 lines (188 loc) · 5.03 KB
/
Copy pathBayesian.java
File metadata and controls
198 lines (188 loc) · 5.03 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
importjava.util.*;
classtable
{ Stringdata[][]={
{"youth","high","no","fair","no"},
{"youth","high","no","excellent","no"},
{"middle_aged","high","no","fair","yes"},
{"senior","medium","no","fair","yes"},
{"senior","low","yes","fair","yes"},
{"senior","low","yes","excellent","no"},
{"middle_aged","low","yes","excellent","yes"},
{"youth","medium","no","fair","no"},
{"youth","low","yes","fair","yes"},
{"senior","medium","yes","excellent","yes"},
{"youth","medium","yes","excellent","yes"},
{"middle_aged","medium","no","fair","yes"},
{"middle_aged","high","yes","fair","yes"},
{"senior","medium","no","excellent","no"}
};
Stringattrib[][]={
{"age","youth","middle_aged","senior"},
{"income","high","medium","low"},
{"student","no","yes"},
{"credit_rating","fair","excellent"},
{"buys_comp","yes","no"}
};
Stringtemp[];
intyes,no;
intm,n,decide;
table()
{ n=5;
m=14;
Scannerin=newScanner(System.in);
for(inti=0;i<m;i++)
System.out.println(Arrays.toString(data[i]));
System.out.println("Enter the deciding attribute");
for(inti=0;i<n;i++)
System.out.println("press "+i+" for : "+attrib[i][0]);
decide=in.nextInt();
System.out.println("Enter the tuple you wish to classify");
temp=newString[n-1];
for(inti=0;i<n-1;i++)
{ System.out.println(attrib[i][0] +" :");
temp[i]=in.next();
}
for(inti=0;i<m;i++)
if(data[i][4].equals("yes"))
yes++;
else
no++;
for(inti=0;i<4;i++)
for(intj=1;j<attrib[i].length;j++)
showall(attrib[i][j],i);
compute(decide);
}
voidshowall(Stringatr,intt)
{ intcount=0,c1=0,c2=0;
for(inti=0;i<m;i++)
if(data[i][t].equals(atr))
count++;
System.out.println("\n");
System.out.println("for attribute : "+attrib[t][0]);
for(inti=0;i<m;i++)
if(data[i][t].equals(atr) && data[i][4].equals("yes"))
c1++;
System.out.println("P( "+atr+" | yes ) : ( "+c1+" / "+yes +" ) ");
c1=0;
for(inti=0;i<m;i++)
if(data[i][t].equals(atr) && data[i][4].equals("no"))
c1++;
System.out.println("P( "+atr+" | no ) : ( "+c1+" / "+no +" ) ");
}
voidcompute(intt)
{ intcount=0;
intyes[][]=newint[m][5];
intansy[]=newint[n];
intansn[]=newint[n];
for(intj=1;j<3;j++)
{ count=0;
for(inti=0;i<m;i++)
{ if(attrib[decide][j]==data[i][decide])
count++;
}
yes[decide][j]=count;
}
for(inti=0;i<temp.length;i++)
{ count=0;
for(intj=0;j<m;j++)
if(temp[i].equals(data[j][i]) && (data[j][n-1]).equals("yes"))
count++;
ansy[i]=count;
}
ansy[n-1]=yes[n-1][1];
ansn[n-1]=yes[n-1][2];
for(inti=0;i<temp.length;i++)
{ count=0;
for(intj=0;j<m;j++)
if(temp[i].equals(data[j][i]) && (data[j][n-1]).equals("no"))
count++;
ansn[i]=count;
}
System.out.println(Arrays.toString(temp));
System.out.println("\n\n");
System.out.println(Arrays.toString(ansy));
doubleproby=1;
for(inti=0;i<n;i++)
proby=proby * ((double)ansy[i]/ansy[n-1]);
System.out.println("The probability of yes is :"+proby);
doubleprobn=1;
for(inti=0;i<n;i++)
probn=probn * ((double)ansn[i]/ansn[n-1]);
System.out.println("The probability of no is :"+probn);
if(probn<proby)
System.out.println("BUYS : YES");
else
System.out.println("BUYS : NO");
}
}
classBayesian
{ publicstaticvoidmain (Stringargs[])
{ tablet=newtable();
}
}
/*
OUTPUT:[youth, high, no, fair, no]
[youth, high, no, excellent, no]
[middle_aged, high, no, fair, yes]
[senior, medium, no, fair, yes]
[senior, low, yes, fair, yes]
[senior, low, yes, excellent, no]
[middle_aged, low, yes, excellent, yes]
[youth, medium, no, fair, no]
[youth, low, yes, fair, yes]
[senior, medium, yes, excellent, yes]
[youth, medium, yes, excellent, yes]
[middle_aged, medium, no, fair, yes]
[middle_aged, high, yes, fair, yes]
[senior, medium, no, excellent, no]
Enterthedecidingattribute
press0for : age
press1for : income
press2for : student
press3for : credit_rating
press4for : buys_comp
4
Enterthetupleyouwishtoclassify
age :
youth
income :
medium
student :
yes
credit_rating :
fair
forattribute : age
P( youth | yes ) : ( 2 / 9 )
P( youth | no ) : ( 3 / 5 )
forattribute : age
P( middle_aged | yes ) : ( 4 / 9 )
P( middle_aged | no ) : ( 0 / 5 )
forattribute : age
P( senior | yes ) : ( 3 / 9 )
P( senior | no ) : ( 2 / 5 )
forattribute : income
P( high | yes ) : ( 2 / 9 )
P( high | no ) : ( 2 / 5 )
forattribute : income
P( medium | yes ) : ( 4 / 9 )
P( medium | no ) : ( 2 / 5 )
forattribute : income
P( low | yes ) : ( 3 / 9 )
P( low | no ) : ( 1 / 5 )
forattribute : student
P( no | yes ) : ( 3 / 9 )
P( no | no ) : ( 4 / 5 )
forattribute : student
P( yes | yes ) : ( 6 / 9 )
P( yes | no ) : ( 1 / 5 )
forattribute : credit_rating
P( fair | yes ) : ( 6 / 9 )
P( fair | no ) : ( 2 / 5 )
forattribute : credit_rating
P( excellent | yes ) : ( 3 / 9 )
P( excellent | no ) : ( 3 / 5 )
[youth, medium, yes, fair]
[2, 4, 6, 6, 9]
Theprobabilityofyesis :0.04389574759945129
Theprobabilityofnois :0.019200000000000002
BUYS : YES /*