-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBAdaBoost.py
More file actions
341 lines (267 loc) · 10.7 KB
/
NBAdaBoost.py
File metadata and controls
341 lines (267 loc) · 10.7 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
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
import sys
from random import random
from math import log
class NaiveBayes(object):
def __init__(self, training, max_idx):
self.train_data = training
self.neg_probs = {}
self.pos_probs = {}
self.neg = 0
self.pos = 0
self.total = 0
self.max_idx = max_idx
self._train()
def _train(self):
neg = 0
pos = 0
# count negative and positive classes in training data
# update attribute count
for data in self.train_data:
for attribute in data[1:]:
if data[0] == "-1":
if attribute not in self.neg_probs:
self.neg_probs[attribute] = 2
else:
self.neg_probs[attribute] += 1
else:
if attribute not in self.pos_probs:
self.pos_probs[attribute] = 2
else:
self.pos_probs[attribute] += 1
if data[0] == "-1":
neg += 1
else:
pos += 1
# lapacian correction, sets likelihood probablities to a minimum
# of 1 / class count for each attribute
# need to add max_idx (number of attributes) to account for correction
self.neg = (float(neg) + self.max_idx)
self.pos = (float(pos) + self.max_idx)
self.total = self.neg + self.pos
# calculate likelihood probabilities
self.neg_probs = dict(map(lambda(k, v): (k, v / self.neg), self.neg_probs.iteritems()))
self.pos_probs = dict(map(lambda(k, v): (k, v / self.pos), self.pos_probs.iteritems()))
def classify_train(self, train_data):
data = train_data[1:]
comb_likelihood_neg = 1
comb_likelihood_pos = 1
# calculate likelihood probabilities for set of attributes
# if attribute does not exist in likelihood probability list,
# use default 1 / total class count to eliminate zero-probability
for value in data:
if value in self.neg_probs:
comb_likelihood_neg *= self.neg_probs[value]
else:
comb_likelihood_neg *= (1 / self.neg)
if value in self.pos_probs:
comb_likelihood_pos *= self.pos_probs[value]
else:
comb_likelihood_pos *= (1/ self.pos)
# calculate posteriori probabilities
posteriori_neg = comb_likelihood_neg * (self.neg / self.total)
posteriori_pos = comb_likelihood_pos * (self.pos / self.total)
# return 0 if correctly classed, 1 if misclassed
if (posteriori_neg > posteriori_pos):
if train_data[0] == "-1":
return 0
else:
if train_data[0] == "+1":
return 0
return 1
# same as above, return predicted class only
def classify_test(self, test_data):
data = test_data[1:]
comb_likelihood_neg = 1
comb_likelihood_pos = 1
for value in data:
if value in self.neg_probs:
comb_likelihood_neg *= self.neg_probs[value]
else:
comb_likelihood_neg *= (1 / self.neg)
if value in self.pos_probs:
comb_likelihood_pos *= self.pos_probs[value]
else:
comb_likelihood_pos *= (1/ self.pos)
posteriori_neg = comb_likelihood_neg * (self.neg / self.total)
posteriori_pos = comb_likelihood_pos * (self.pos / self.total)
if (posteriori_neg > posteriori_pos):
return -1
else:
return 1
class NBAdaBoost(object):
def __init__(self, training, test):
self.train_file = training
self.test_file = test
self.neg_probs = {}
self.pos_probs = {}
self.true_total = 0
self.max_idx = 0
self.train_data = []
self.test_data = []
self.classifiers = []
self.weights = []
self.classifier_weights = []
self._read_files()
# generate specified number of weighted classifiers
self._generate_classifiers(7)
def _read_files(self):
with open(self.train_file, 'r') as data_file:
neg = 0
pos = 0
max_idx = 0
for line in data_file.readlines():
space_split = line.rstrip().split(' ')
train_class = space_split[0]
del space_split[0]
member = [train_class]
for attribute in space_split:
idx, val = map(int, attribute.split(':'))
if idx > max_idx:
max_idx = idx
member.append((idx, val))
# count number of positive and negative members in data
if train_class == "-1":
neg += 1
else:
pos += 1
self.train_data.append(tuple(member))
self.max_idx = max_idx
self.true_total = (neg + pos)
with open(self.test_file, 'r') as data_file:
for line in data_file.readlines():
data = []
space_split = line.rstrip().split(' ')
data.append(space_split[0])
del space_split[0]
for attribute in space_split:
idx, val = map(int,attribute.split(':'))
data.append((idx, val))
self.test_data.append(data)
# set all weights to 1 / total size
def _init_weights(self, size):
for idx in xrange(size):
self.weights.append(float(1) / size)
# normalize weights so they sum to 1
def _normalize_weights(self):
total = 0
for weight in self.weights:
total += weight
self.weights = map(lambda(x): x / total, self.weights)
# retrieve sample set of data based on weights and size
def _get_sample(self, size):
sample = []
for idx in xrange(size):
count = 0
idx = 0
rand = random()
while rand > count:
count += self.weights[idx]
idx += 1
sample.append(self.train_data[idx - 1])
return sample
# generates k classifiers through k iterations of the adaboost algorithm
# updating training member weights and classifier weights
def _generate_classifiers(self, k):
self._init_weights(self.true_total)
for idx in xrange(k):
sample = self._get_sample(self.true_total)
self.classifiers.append(NaiveBayes(sample, self.max_idx))
err_rate = 0
misclassified = []
# intialize classifer and train with sample data, then calculate
# error rate with original training data set
for t_idx, member in enumerate(self.train_data):
class_val = self.classifiers[idx].classify_train(member)
err_rate += class_val * self.weights[t_idx]
if class_val:
misclassified.append(True)
else:
misclassified.append(False)
# reduce weights for correctly classified members,
# paying more attention to incorrectly classified members
for t_idx, value in enumerate(misclassified):
if not value:
self.weights[t_idx] = self.weights[t_idx] * (err_rate / (1 - err_rate))
# update classifier weights
if err_rate:
if err_rate > 0.5:
self.classifier_weights.append(-1 * log((1 - err_rate) / err_rate))
else:
self.classifier_weights.append(log((1 - err_rate) / err_rate))
else:
self.classifier_weights.append(0)
self._normalize_weights()
# run training data against classifiers and calculated weighted final vote
def classify_train(self):
true_pos = 0
false_neg = 0
false_pos = 0
true_neg = 0
for member in self.train_data:
votes = 0
for idx, classifier in enumerate(self.classifiers):
votes += classifier.classify_test(member) * self.classifier_weights[idx]
if votes < 0:
if member[0] == "-1":
true_neg += 1
else:
false_neg += 1
else:
if member[0] == "+1":
true_pos += 1
else:
false_pos += 1
return (true_pos, false_neg, false_pos, true_neg)
# same as above, but with test data
def classify_test(self):
true_pos = 0
false_neg = 0
false_pos = 0
true_neg = 0
for member in self.test_data:
votes = 0
for idx, classifier in enumerate(self.classifiers):
votes += classifier.classify_test(member) * self.classifier_weights[idx]
if votes < 0:
if member[0] == "-1":
true_neg += 1
else:
false_neg += 1
else:
if member[0] == "+1":
true_pos += 1
else:
false_pos += 1
return (true_pos, false_neg, false_pos, true_neg)
def main(argv):
if len(argv) != 2:
print "Incorrect number of parameters"
print " Usage: python naive_payes.py <trainingFile> <testFile>"
sys.exit(-1)
training = argv[0]
test = argv[1]
nbadaboost = NBAdaBoost(training, test)
training_output = nbadaboost.classify_train()
test_output = nbadaboost.classify_test()
print("%i %i %i %i" % (training_output[0], training_output[1], training_output[2], training_output[3]))
print("%i %i %i %i" % (test_output[0], test_output[1], test_output[2], test_output[3]))
#print_metrics(training_output)
#print_metrics(test_output)
# calculate and print extra metrics
def print_metrics(data):
total = float(0)
for member in data:
total += member
accuracy = (data[0] + data[3]) / total
error = 1 - accuracy
sensitivity = float(data[0]) / (data[0] + data[1])
specificity = float(data[3]) / (data[2] + data[3])
precision = float(data[0]) / (data[0] + data[2])
recall = sensitivity
f_score = (2 * precision * recall) / (precision + recall)
f05 = ((1 + 0.5 * 0.5) * precision * recall) / (0.5 * 0.5 * precision + recall)
f2 = ((1 + 2 * 2) * precision * recall) / (2 * 2 * precision + recall)
print("%f %f %f %f" % (accuracy, error, sensitivity, specificity))
print("%f %f %f %f" % (precision, f_score, f05, f2))
if __name__ == "__main__":
main(sys.argv[1:])