-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes.py
196 lines (158 loc) · 6.92 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
from libsvm import read_libsvm
import numpy as np
from math import log
import csv
from helpers import trim_sparse_features
def count_labels(labels):
counts = {}
for label in labels:
current_count = counts.get(label, 0)
counts[label] = current_count + 1
return counts
def learn_naive_bayes(x, y, smoothing):
overall_counts = count_labels(y)
conditional_probs = {}
# loop over features to build distribution
for feature_index in range(x.shape[1]):
conditional_probs[feature_index] = {}
counts = {}
# count up the number of each value with each label
for i in range(len(y)):
val = x[i][feature_index]
label = y[i]
if val not in counts:
counts[val] = {}
count = counts[val].get(label, 0)
counts[val][label] = count + 1
# calculate conditional probabilities
for val, labels in counts.items():
conditional_probs[feature_index][val] = {}
for label in labels:
prob = (counts[val][label] + smoothing) / (overall_counts[label] + x.shape[1])
conditional_probs[feature_index][val][label] = prob
priors = {}
for label, count in overall_counts.items():
priors[label] = count / len(y)
return priors, conditional_probs, overall_counts
def test_accuracy(classifier, x, y, smoothing):
priors, conditional_probs, overall_counts = classifier
possible_labels = list(set(y))
correct = 0
# loop over each example
for i in range(len(y)):
probs = []
# check each label to find the one with the highest probability
for j in range(len(possible_labels)):
label = possible_labels[j]
probs.append(log(priors[label]))
# multiply the conditional probability for each value
for feature in range(len(x[i])):
val = x[i][feature]
# if we haven't seen this value/label combination before
if val not in conditional_probs[feature] or label not in conditional_probs[feature][val]:
probs[j] += log(smoothing / (overall_counts[label] + x.shape[1]))
else:
probs[j] += log(conditional_probs[feature][val][label])
best_index = np.array(probs).argmax()
best_label = possible_labels[best_index]
if best_label == y[i]:
correct += 1
return correct / len(y)
def cross_validate(smoothing):
scores = []
for i in range(1,6):
x_folds = []
y_folds = []
first = True
x, y, num_features = read_libsvm('data/data.train')
x = np.asarray(x.todense())
num_per_fold = len(x) // 6
count = 0
for j in range(1, 6):
# path = 'data/CVfolds/fold' + str(j)
if j != i and first:
x_folds.append(x[count:count + num_per_fold])
y_folds.append(y[count:count + num_per_fold])
count += num_per_fold
x_train = np.concatenate(x_folds)
y_train = np.concatenate(y_folds)
x_train, medians = binarize(x_train)
classifier = learn_naive_bayes(x_train, y_train, smoothing)
x_test = x[i * num_per_fold:i * num_per_fold + num_per_fold]
y_test = y[i * num_per_fold:i * num_per_fold + num_per_fold]
x_test, medians = binarize(x_test, medians)
result_accuracy = test_accuracy(classifier, x_test, y_test, smoothing)
scores.append(result_accuracy)
return sum(scores) / float(len(scores))
# Welp this doesn't work great
def binarize(x, medians=None):
if not medians:
medians = []
for i in range(x.shape[1]):
medians.append(np.percentile(x[:,i], 60))
for i in range(x.shape[0]):
for j in range(x.shape[1]):
if x[i][j] >= medians[j]:
x[i][j] = 1
else:
x[i][j] = 0
return x, medians
def write_answers(classifier, x, y, smoothing):
ids = []
with open('data/eval.id') as f:
for line in f:
ids.append(line.strip())
with open('answers_naive_bayes.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['example_id','label'])
priors, conditional_probs, overall_counts = classifier
possible_labels = list(priors.keys())
# loop over each example
for i in range(len(y)):
probs = []
# check each label to find the one with the highest probability
for j in range(len(possible_labels)):
label = possible_labels[j]
probs.append(log(priors[label]))
# multiply the conditional probability for each value
for feature in range(len(x[i])):
val = x[i][feature]
# if we haven't seen this value/label combination before
if val not in conditional_probs[feature] or label not in conditional_probs[feature][val]:
probs[j] += log(smoothing / (overall_counts[label] + x.shape[1]))
else:
probs[j] += log(conditional_probs[feature][val][label])
best_index = np.array(probs).argmax()
best_label = possible_labels[best_index]
writer.writerow([ids[i], best_label])
def run_naive_bayes(write=False):
best_smoothing = None
best_accuracy = 0
print('Cross Validation')
print('+----------------+--------------------+')
print('| Smoothing Term | Average Accuracy |')
print('+----------------+--------------------+')
for smoothing in [2, 1.5, 1, 0.5]:
result = cross_validate(smoothing)
print('|{:>16}'.format(str(smoothing))+'|{:>20}|'.format(str(result)))
if result > best_accuracy:
best_accuracy = result
best_smoothing = smoothing
print('+----------------+--------------------+')
print('Best hyper-parameter (smoothing term):', best_smoothing)
print('Average Accuracy for best hyper-parameter:', best_accuracy)
x_train, y_train, num_features = read_libsvm(fname='data/data.train')
x_train = np.asarray(x_train.todense())
x_train, medians = binarize(x_train)
classifier = learn_naive_bayes(x_train, y_train, best_smoothing)
print('Training Accuracy:', test_accuracy(classifier, x_train, y_train, best_smoothing))
x_test, y_test, num_features = read_libsvm(fname='data/data.test')
x_test = np.asarray(x_test.todense())
x_test, medians = binarize(x_test, medians)
print('Test Accuracy:', test_accuracy(classifier, x_test, y_test, best_smoothing))
if write:
x_test, y_test, num_features = read_libsvm(fname='data/data.eval.anon')
x_test = np.asarray(x_test.todense())
x_test, medians = binarize(x_test, medians)
write_answers(classifier,x_test, y_test, best_smoothing)