
from pymatreader import read_mat
import scipy
import random
import numpy as np
from sklearn.metrics import zero_one_loss
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
from sklearn.feature_selection import mutual_info_classif
import os

if os.path.isdir('scripts'):
    os.chdir('scripts')

data = None
Xtrain = None
Xtest = None

data = read_mat('../data/XwindowsDocData.mat')
Xtrain = data['xtrain']
Xtrain = scipy.sparse.csc_matrix.toarray(Xtrain)
Xtest = data['xtest']
Xtest = scipy.sparse.csc_matrix.toarray(Xtest)

ytrain = data['ytrain']
ytest = data['ytest']
model = GaussianNB()
ypred_train = model.fit(Xtrain, ytrain).predict(Xtrain)
err_train = np.mean(zero_one_loss(ytrain, ypred_train))
ypred_test = model.fit(Xtrain, ytrain).predict(Xtest)
err_test = np.mean(zero_one_loss(ytest, ypred_test))
print('misclassification rates  on train = '+str(err_train*100) +
      ' pc, on test = '+str(err_test*100)+' pc\n')


C = np.unique(data['ytrain']).size
for i in range(0, C):
    plt.bar(np.arange(0, 600, 1), model.theta_[i, :])
    plt.title('p(xj=1|y='+str(i)+')')
    fileName = 'naiveBayesBow'+str(i+1)+'ClassCond'
    plt.savefig(r'../figures/'+fileName)
    plt.show()

@@ -1,29 +1,39 @@
 %0A
-from pymatreader
+import scipy%0Afrom scipy.io
  import 
 read
@@ -32,30 +32,16 @@
 ort 
-re
+lo
 ad
-_
 mat%0A
-import scipy%0A
 impo
@@ -143,16 +143,42 @@
  import 
+BernoulliNB,MultinomialNB,
 Gaussian
@@ -285,114 +285,21 @@
 os%0A%0A
-if os.path.isdir('scripts'):%0A    os.chdir('scripts')%0A%0Adata = None%0AXtrain = None%0AXtest = None%0A%0Adata = read_
+%0A%0Adata = load
 mat(
@@ -530,35 +530,23 @@
 l = 
-GaussianNB()%0Aypred_train = 
+BernoulliNB()%0A%0A
 mode
@@ -558,32 +558,53 @@
 (Xtrain, ytrain)
+%0A%0Aypred_train = model
 .predict(Xtrain)
@@ -604,16 +604,17 @@
 Xtrain)%0A
+%0A
 err_trai
@@ -661,16 +661,17 @@
 train))%0A
+%0A
 ypred_te
@@ -685,28 +685,8 @@
 del.
-fit(Xtrain, ytrain).
 pred
@@ -696,16 +696,17 @@
 (Xtest)%0A
+%0A
 err_test
@@ -750,16 +750,17 @@
 _test))%0A
+%0A
 print('m
@@ -869,17 +869,16 @@
 pc%5Cn')%0A%0A
-%0A
 C = np.u
@@ -903,16 +903,25 @@
 '%5D).size
+%0A%0Aprint()
 %0Afor i i
@@ -973,20 +973,39 @@
 1), 
-model.theta_
+np.exp(model.feature_log_prob_)
 %5Bi, 
@@ -1026,16 +1026,24 @@
 tle(
-'p
+r'$P
 (x
+_
 j=1
-%7C
+ %5Cmid 
 y='+
@@ -1051,12 +1051,15 @@
 tr(i
++1
 )+')
+$
 ')%0A 

Update tmp.py