-
Notifications
You must be signed in to change notification settings - Fork 7
/
naive_bayes.py
388 lines (320 loc) · 13.3 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
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
"""
Simple Naive Bayes classifier implimentation for sequence prediction.
Author: Chang Liu (fluency03)
Data: 2016-05-12
"""
import cPickle as pickle
import glob
import os
import time
from math import log
import numpy as np
from rnn_sequence_analyzer import plot_hist, plot_and_write_prob
class NaiveBayes(object):
"""
Simple Naive Bayes classifier implimentation for sequence prediction.
"""
def __init__(self, window_size, nb_classes, alpha=1.0):
"""
Initialization. Set up some parameters. Build up the matrix.
Arguments:
window_size: {integer}, the size of input window.
nb_classes: {integer}, number of uniques classes.
alpha: {float}, the smoothing priors alpha >= 0 accounts for
features not present in the learning samples and prevents zero
probabilities in further computations. Setting alpha = 1 is
called Laplace smoothing, while alpha < 1 is called
Lidstone smoothing.
"""
self.window_size = window_size
self.nb_classes = nb_classes
self.alpha = alpha
self.build()
def build(self):
"""
Build up the matrix.
"""
self.ny = np.zeros((self.nb_classes,), dtype=np.int)
self.nx_y = np.zeros((self.window_size,
self.nb_classes,
self.nb_classes), dtype=np.int)
def train(self, X, y):
"""
Train the model.
Arguments:
X: {array}, X training data.
y: {array}, y training data.
"""
N = len(y)
for i in xrange(N):
self.ny[y[i]] += 1
for j in xrange(self.window_size):
self.nx_y[j, X[i, j], y[i]] += 1
def save_model(self, filename):
"""
Save the model information to a file.
"""
print " |-Write the model into %s ..." %filename
with open(filename, 'w') as pkl_file:
pickle.dump({'ny': self.ny, 'nx_y': self.nx_y,
'window_size': self.window_size,
'nb_classes': self.nb_classes,
'alpha': self.alpha}, pkl_file)
def load_model(self, filename):
"""
Load the model information from a file.
"""
if os.path.isfile(filename):
print "%s existing, loading it...\n" %filename
with open(filename) as pkl_file:
model = pickle.load(pkl_file)
self.ny = model['ny']
self.nx_y = model['nx_y']
# self.window_size = model['window_size']
# self.nb_classes = model['nb_classes']
# self.alpha = model['alpha']
else:
print "File does not exist!"
def evaluate(self, X, y, normalization=True, log_scale=False):
"""
Evaluate the model.
Arguments:
X: {array}, X evaluation data.
y: {array}, y evaluation data.
normalization: {bool}, whether do the normalization.
log_scale: {bool}, whether transfer probabilities on log scale.
"""
def scale(p):
"""
Probability in log scale.
"""
return log(p) if log_scale else p
def normalize(py_x):
"""
Normalize the probabilities.
"""
py_x_sum = np.sum(py_x)
return np.asarray([py_x[p] / py_x_sum
for p in xrange(self.nb_classes)])
N = np.sum(self.ny)
length = len(y)
print "length: %d " %length
correct = 0
probs = np.zeros(length)
if not log_scale:
probs[:self.window_size] = 1.0
# ------------------- Prior ------------------- #
py = np.zeros(self.nb_classes)
for i in xrange(self.nb_classes):
py[i] = ((self.ny[i] + self.alpha) /
(N + self.alpha * self.nb_classes))
for i in xrange(length):
print "evaluating %d ..." %i
# ------------------- Likelihood ------------------- #
px_y = np.zeros((self.nb_classes, self.window_size))
for p in xrange(self.nb_classes):
for k in xrange(self.window_size):
px_y[p, k] = ((self.nx_y[k, X[i, k], p] +
self.alpha) /
(self.ny[p] +
self.alpha * self.nb_classes))
# ------------------- Posterior ------------------- #
py_x = np.zeros(self.nb_classes)
for j in xrange(self.nb_classes):
py_x[j] = py[j] * np.prod(px_y[j])
# ------------------- Normalization ------------------- #
if normalization:
py_x = normalize(py_x)
# ------------------- Prediction ------------------- #
# check the prediction
y_pred = np.argmax(py_x)
y_true = y[i]
max_prob = scale(py_x[y_pred])
print ("y_pred: %d , max_prod: %.8f, y_true_prob: %.8f ,"
%(y_pred, max_prob, scale(py_x[y_true])))
if y_true == y_pred:
correct += 1
probs[i + self.window_size] = max_prob
accuracy = (correct * 100.0) / length
print "Accuracy: %.4f%%" %accuracy
print " |-Plot figures ..."
plot_and_write_prob(probs,
"nb_prob_",
[0, 50000, 0, 1],
'Log' if log_scale else 'Normal')
def evaluate_all(self, X, y, nb_options=3, normalization=True): # pylint: disable=R0912
"""
Evaluate the model.
Arguments:
X: {array}, X evaluation data.
y: {array}, y evaluation data.
nb_options: {interger}, number of predicted options.
normalization: {bool}, whether do the normalization.
"""
N = np.sum(self.ny)
length = len(y)
print "length: %d " %length
probs = np.zeros((nb_options+1, length + self.window_size))
for o in xrange(nb_options+1):
probs[o][:self.window_size] = 1.0
# probability in negative log scale
log_probs = np.zeros((nb_options+1, length + self.window_size))
# count the number of correct predictions
nb_correct = [0] * (nb_options+1)
# ------------------- Prior ------------------- #
py = np.zeros(self.nb_classes)
for i in xrange(self.nb_classes):
py[i] = ((self.ny[i] + self.alpha) /
(N + self.alpha * self.nb_classes))
try:
for i in xrange(length):
print "evaluating %d ..." %i
# ------------------- Likelihood ------------------- #
px_y = np.zeros((self.nb_classes, self.window_size))
for p in xrange(self.nb_classes):
for k in xrange(self.window_size):
px_y[p, k] = ((self.nx_y[k, X[i, k], p] +
self.alpha) /
(self.ny[p] +
self.alpha * self.nb_classes))
# ------------------- Posterior ------------------- #
py_x = np.zeros(self.nb_classes)
for j in xrange(self.nb_classes):
py_x[j] = py[j] * np.prod(px_y[j])
# ------------------- Normalization ------------------- #
if normalization:
py_x_sum = np.sum(py_x)
py_x = np.asarray([py_x[p] / py_x_sum
for p in xrange(self.nb_classes)])
# ------------------- Prediction ------------------- #
# check the prediction
y_pred = np.argsort(py_x)[-nb_options:][::-1]
y_true = y[i]
print y_pred, y_true
next_probs = [0.0] * (nb_options+1)
next_probs[0] = py_x[y_true]
for o in xrange(nb_options):
if y_true == y_pred[o]:
next_probs[o+1] = 1.0
nb_correct[o+1] += 1
next_probs = np.maximum.accumulate(next_probs)
print next_probs
for k in xrange(nb_options+1):
probs[k, i + self.window_size] = next_probs[k]
# get the negative log probability
log_probs[k, i + self.window_size] = -log(next_probs[k])
except:
print "KeyboardInterrupt"
nb_correct = np.add.accumulate(nb_correct)
for n in xrange(nb_options+1):
print "Accuracy %d: %.4f%%" %(n, (nb_correct[n] * 100.0 / (i + 1))) # pylint: disable=W0631
print " |-Plot figures ..."
for q in xrange(nb_options+1):
plot_and_write_prob(probs[q],
"nb_prob_"+str(q),
[0, 50000, 0, 1],
'Normal')
plot_and_write_prob(log_probs[q],
"nb_log_prob_"+str(q),
[0, 50000, 0, 25],
'Log')
def predict(self, X):
"""
Predict next sequence.
"""
pass
def get_sequence(filepath):
"""
Get the original sequence from file.
Arguments:
filename: {string}, the name/path of input log sequence file.
Returns:
{list}, the log sequence.
{integer}, the size of vocabulary.
{integer}, total length of the sequences.
"""
# read file and convert ids of each line into array of numbers
seqfiles = glob.glob(filepath)
sequences = []
total_length = 0
max_value = 0
for seqfile in seqfiles:
sequence = []
with open(seqfile, 'r') as f:
one_sequence = [int(id_) for id_ in f]
print " %s, sequence length: %d" %(seqfile,
len(one_sequence))
sequence.extend(one_sequence)
total_length += len(one_sequence)
max_new = np.amax(sequence)
max_value = max_new if max_new > max_value else max_value
sequences.append(sequence)
# add two extra positions for 'unknown-log' and 'no-log'
vocab_size = max_value + 2
return sequences, vocab_size, total_length
def get_data(sequence, sentence_length=40, step=3, random_offset=True):
"""
Retrieves data from a plain txt file and formats it using one-hot vector.
Arguments:
sequence: {lsit}, the original input sequence
vocab_size: {integer}, the number of unique id classes
sentence_length: {integer}, the length of each training sentence.
step: {integer}, the sample steps.
random_offset: {bool}, the offset is random between step or is 0.
Returns:
{np.array}, training input data X
{np.array}, training target data y
"""
X_sentences = []
next_ids = []
offset = np.random.randint(0, step) if random_offset else 0
# creat batch data and next sentences
for i in range(offset, len(sequence) - sentence_length, step):
X_sentences.append(sequence[i : i + sentence_length])
next_ids.append(sequence[i + sentence_length])
# number of sampes
# nb_samples = len(X_sentences)
# print "total # of sentences: %d" %nb_samples
return np.asarray(X_sentences), np.asarray(next_ids)
def main(sentence_length=3, mode='train'):
"""
Train the model.
Arguments:
sentence_length: {integer}, the length of each training sentence.
"""
# get parameters and dimensions of the model
print "Loading training data..."
train_sequence, input_len1, total_length1 = get_sequence("./train_data/*")
print "Loading validation data..."
val_sequence, input_len2, total_length2 = get_sequence("./validation_data/*")
input_len = max(input_len1, input_len2)
print "Training sequence length: %d" %total_length1
print "Validation sequence length: %d" %total_length2
print "#classes: %d\n" %input_len
start_time = time.time()
nb = NaiveBayes(window_size=sentence_length,
nb_classes=input_len,
alpha=1.0/input_len)
if mode == 'train':
print "Train the model...\n"
for sequence in train_sequence:
X_train, y_train = get_data(sequence, sentence_length=sentence_length,
step=1, random_offset=False)
nb.train(X_train, y_train)
# nb.save_model('2.pkl')
elif mode == 'load':
nb.load_model('2.pkl')
print "Evaluate the model...\n"
# for sequence in val_sequence:
# X_val, y_val = get_data(sequence, sentence_length=sentence_length,
# step=1, random_offset=False)
# nb.evaluate(X_val, y_val, normalization=True, log_scale=False)
for sequence in val_sequence:
X_val, y_val = get_data(sequence, sentence_length=sentence_length,
step=1, random_offset=False)
nb.evaluate_all(X_val, y_val, nb_options=3, normalization=True)
stop_time = time.time()
print "Stop...\n"
print "--- %s seconds ---\n" % (stop_time - start_time)
if __name__ == '__main__':
main()