-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnaive_bayes.py
265 lines (181 loc) · 6.77 KB
/
naive_bayes.py
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
import sys
import sys
def load_data(file_name):
data = list(open(file_name, 'r'))
m = int(data[0]) #number of input variables
N = int(data[1]) #number of data vectors
x = []
y = []
for vec in data[2:]:
instr, outstr = vec.split(':')
invec = [int(_) for _ in instr.split()]
x.append(invec)
y.append(int(outstr))
return m, N, x, y
#Training through Laplace Estimators or MAP
# lect 24, p. 52
def laplace_estimate(m, N, x, y):
# prob_y[0] = P(Y = 0)
# prob_y[1] = P(Y = 1)
# prob_x_y[0][0] = P(Xi = 0, Y = 0)/P(Y = 0)
# prob_x_y[1][0] = P(Xi = 1, Y = 0)/P(Y = 0)
#instance_x_y[n][m] = # of instances where Xi = n and Y = m
#inst = instances in class 0
prob_y = []
prob_x_y = dict()
#instance_x_y = []
zero_instances = 0
total_instances = N
for cl in y:
# cl = class (0 or 1)
if cl == 0:
zero_instances+=1
one_instances = total_instances - zero_instances
prob_y.append(float(zero_instances + 1)/(total_instances + 2))
prob_y.append(float(one_instances + 1)/(total_instances + 2)) # = 1 - prob_y[0]
for col in xrange(m):
instances_zero_zero = 0.0
instances_zero_one = 0.0
instances_one_zero = 0.0
instances_one_one = 0.0
for row in xrange(N):
instance = x[row][col]
classY = y[row]
#instances_zero_zero = instances where Xi = 0 and class Y = 0
if(instance == 0 and classY == 0):
instances_zero_zero += 1
if(instance == 0 and classY == 1):
instances_zero_one += 1
if(instance == 1 and classY == 0):
instances_one_zero += 1
if(instance == 1 and classY == 1):
instances_one_one += 1
# N = number of total instances
# P(Xi = 0, Y = 1) = instances_zero_one/N
# prob_x_y[col][0][1] = P(Xi = 0| Y = 1) = P(Xi = 0, Y = 1)/P(Y = 1)
# col = number of the column
prob_x_y[col] = [[(instances_zero_zero + 1)/(zero_instances + 2),\
(instances_zero_one + 1)/(one_instances + 2)],\
[(instances_one_zero + 1)/(zero_instances + 2), \
(instances_one_one + 1)/(one_instances + 2)]]
#print (instances_one_one + 1)/(one_instances + 2)
return prob_x_y, prob_y
#Training through Maximum Likelihood Estimators
def mle_estimate(m, N, x, y):
# prob_y[0] = P(Y = 0)
# prob_y[1] = P(Y = 1)
# prob_x_y[col][0][0] = P(Xi = 0, Y = 0)/P(Y = 0)
# prob_x_y[col][1][0] = P(Xi = 1, Y = 0)/P(Y = 0)
#instance_x_y[n][m] = # of instances where Xi = n and Y = m
#inst = instances in class 0
prob_y = []
prob_x_y = dict()
#instance_x_y = []
zero_instances = 0
total_instances = N
for cl in y:
# cl = class (0 or 1)
if cl == 0:
zero_instances+=1
prob_y.append(float(zero_instances)/(total_instances))
prob_y.append(float(total_instances - zero_instances)/total_instances) # = 1 - prob_y[0]
for col in xrange(m):
instances_zero_zero = 0.0
instances_zero_one = 0.0
instances_one_zero = 0.0
instances_one_one = 0.0
for row in xrange(N):
instance = x[row][col]
classY = y[row]
#instances_zero_zero = instances where Xi = 0 and class Y = 0
if(instance == 0 and classY == 0):
instances_zero_zero += 1
if(instance == 0 and classY == 1):
instances_zero_one += 1
if(instance == 1 and classY == 0):
instances_one_zero += 1
if(instance == 1 and classY == 1):
instances_one_one += 1
# N = number of total instances
# P(Xi = 0, Y = 1) = instances_zero_one/N
# prob_x_y[col][0][1] = P(Xi = 0| Y = 1) = P(Xi = 0, Y = 1)/P(Y = 1)
# col = number of the column
prob_x_y[col] = [[(instances_zero_zero/N)/prob_y[0],\
(instances_zero_one/N)/prob_y[1]],\
[(instances_one_zero/N)/prob_y[0], \
(instances_one_one/N)/prob_y[1]]]
return prob_x_y, prob_y
#Classifying
#lect 14, p. 26
def bayes_predictor(x, m, N, prob_x_y, prob_y):
pred_y = []
for x_row in xrange(N):
# pred_y_one = Y hat for Y = 1
pred_y_one = 1
pred_y_zero = 1
for x_col in xrange(m):
x_value = x[x_row][x_col]
pred_y_zero *= prob_x_y[x_col][x_value][0]
pred_y_one *= prob_x_y[x_col][x_value][1]
pred_y_zero *= prob_y[0]
pred_y_one *= prob_y[1]
#y_hat is the value of y that has higher likelihood
y_hat = 0 if pred_y_zero > pred_y_one else 1
pred_y.append(y_hat)
return pred_y
# pred_y = values of y we predicted
# y = actual values of y
def calculate_accuracy(pred_y, y):
total_zero = 0
total_one = 0
correct_zero = 0
correct_one = 0
for i in xrange(len(y)):
if y[i] == 0:
total_zero+=1
if y[i] == 1:
total_one+=1
if y[i] == pred_y[i]:
if y[i] == 0:
correct_zero += 1
if y[i] == 1:
correct_one += 1
accuracy = float(correct_zero + correct_one)/(total_zero + total_one)
print "Class 0: tested %d, correctly classified %d" %(total_zero, correct_zero)
print "Class 1: tested %d, correctly classified %d" %(total_one, correct_one)
print "Accuracy = %1.2f" %(accuracy)
if __name__ == '__main__' :
train_file = sys.argv[1]
test_file = sys.argv[2]
#lect 24 p. 11
# m = number of input variables (num of columns)
# N = number of data vectors (num of rows)
# x = input variables/observations (matrix)
#eg.: [[0,0],[0,1],[1,0]]
# y = output
m, N, x, y = load_data(train_file)
# lect 24 p. 24
#Training:
# Y = argmax P(X,Y) = argmax P(X|Y)P(Y)
#mle_x_probs = P(X|Y)
#mle_y_probs = P(Y)
print "\nUsing Maximum Likelihood Estimator\n"
prob_x_y, prob_y = mle_estimate(m, N, x, y);
# print prob_x_y
# print prob_y
m, N, x, y = load_data(test_file)
#pred_y is an array of predicted values of Y for each vector
pred_y = bayes_predictor(x, m, N, prob_x_y, prob_y)
# print pred_y
calculate_accuracy(pred_y, y);
#Now using Laplace Estimators instead of MLE
print "\nUsing Laplace Estimate\n"
m, N, x, y = load_data(train_file)
prob_x_y, prob_y = laplace_estimate(m, N, x, y);
# print prob_x_y
# print prob_y
m, N, x, y = load_data(test_file)
#pred_y is an array of predicted values of Y for each vector
pred_y = bayes_predictor(x, m, N, prob_x_y, prob_y)
# print pred_y
calculate_accuracy(pred_y, y);