Naive Bayes
Naive Bayes
plt.show()
print('Class probabilities', py)
Class probabilities
[0.09871688 0.11236461 0.09930012 0.10218297 0.09736711 0.09035161
0.09863356 0.10441593 0.09751708 0.09915014]
<NDArray 10 @cpu(0)>
Naive Normalization (without logsum)
In [4]: # get the first test item
data, label = mnist_test[0]
data = data.reshape((784,1))
Unnormalized Probabilities
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
<NDArray 10 @cpu(0)>
Normalized Probabilities
[nan nan nan nan nan nan nan nan nan nan]
<NDArray 10 @cpu(0)>
Normalization (with logsum)
In [5]: logpx = nd.log(px)
logpxneg = nd.log(1-px)
logpy = nd.log(py)
def bayespost(data):
# we need to incorporate the prior probability p(y) since p(y|x) is
# proportional to p(x|y) p(y)
logpost = logpy.copy()
logpost += (logpx * data + logpxneg * (1-data)).sum(0)
# normalize to prevent overflow or underflow by subtracting the largest
# value
logpost -= nd.max(logpost)
# and compute the softmax using logpx
post = nd.exp(logpost).asnumpy()
post /= np.sum(post)
return post
In [6]: fig, figarr = plt.subplots(2, 10, figsize=(10, 3))
ctr = 0
for data, label in mnist_test:
x = data.reshape((784,1))
y = int(label)
post = bayespost(x)
figarr[1, ctr].bar(range(10), post)
figarr[1, ctr].axes.get_yaxis().set_visible(False)
figarr[0, ctr].imshow(x.reshape((28, 28)).asnumpy(), cmap='hot')
figarr[0, ctr].axes.get_xaxis().set_visible(False)
figarr[0, ctr].axes.get_yaxis().set_visible(False)
ctr += 1
if ctr == 10: break
plt.show()
Computing the Accuracy
In [7]: # initialize counter
ctr = 0
err = 0
post = bayespost(x)
if (post[y] < post.max()):
err += 1