-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaiveBayes.py
More file actions
322 lines (250 loc) · 9.47 KB
/
naiveBayes.py
File metadata and controls
322 lines (250 loc) · 9.47 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
#!/usr/bin/python
import sys
#alternative python path
sys.path.append("/usr/local/lib/python2.7/site-packages")
import os
import numpy as np
from sklearn.naive_bayes import MultinomialNB
import nltk
from copy import deepcopy
import math
#Haowen Qu (hq5rc)
#Machine Learning HW5
###############################################################################
vocabulary = {"love":0, "wonderful":0, "best":0, "great":0, "superb":0, "still":0,
"beautiful":0, "bad":0, "worst":0, "stupid":0,"waste":0, "boring":0, "?":0, "!":0, "UNK":0}
def transfer(fileDj, vocabulary):
BOWDj = deepcopy(vocabulary)
with open(fileDj) as f:
for line in f:
line.replace("loved","love")
line.replace("loves","love")
line.replace("loving","love")
for token in line.split():
if token not in vocabulary:
BOWDj['UNK']+=1
else:
BOWDj[token]+=1
return BOWDj
def loadData(Path):
Xtrain = np.empty(shape=[0,15])
Xtest = np.empty(shape=[0,15])
ytest = []
ytrain = []
wordList = ["love", "wonderful", "best", "great", "superb", "still", "beautiful",
"bad", "worst", "stupid", "waste", "boring", "?", "!", "UNK"]
#Training - Positive
for filename in os.listdir(Path+"training_set/pos"):
filepath = Path + "training_set/pos/" +filename
BOWDj=transfer(filepath,vocabulary)
row = []
for i in wordList:
row.append(BOWDj[i])
Xtrain = np.vstack([Xtrain,row])
ytrain.append(1)
#Training - Negative
for filename in os.listdir(Path+"training_set/neg"):
filepath = Path + "training_set/neg/" +filename
BOWDj=transfer(filepath,vocabulary)
row = []
for i in wordList:
row.append(BOWDj[i])
Xtrain = np.vstack([Xtrain,row])
ytrain.append(-1)
#Testing - Positive
for filename in os.listdir(Path+"test_set/pos"):
filepath = Path + "test_set/pos/" +filename
BOWDj=transfer(filepath,vocabulary)
row = []
for i in wordList:
row.append(BOWDj[i])
Xtest = np.vstack([Xtest,row])
ytest.append(1)
#Testing - Negative
for filename in os.listdir(Path+"test_set/neg"):
filepath = Path + "test_set/neg/" +filename
BOWDj=transfer(filepath,vocabulary)
row = []
for i in wordList:
row.append(BOWDj[i])
Xtest = np.vstack([Xtest,row])
ytest.append(-1)
#XTrain SHOULD BE INTEGER
return Xtrain, Xtest, ytrain, ytest
def naiveBayesMulFeature_train(Xtrain, ytrain):
thetaPos=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
thetaNeg=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
sumOfPos = 0
sumOfNeg = 0
for i in range(0,Xtrain.shape[0]):
if ytrain[i]==1:
for x in range(0,15):
sumOfPos += Xtrain.item((i,x))
thetaPos[x] += Xtrain.item((i,x))
else:
for x in range(0,15):
sumOfNeg += Xtrain.item((i,x))
thetaNeg[x] += Xtrain.item((i,x))
for x in range(0,15):
thetaPos[x] = ((thetaPos[x])+1)/float(sumOfPos+15)
thetaNeg[x] = ((thetaNeg[x])+1)/float(sumOfNeg+15)
print thetaPos
return thetaPos, thetaNeg
def naiveBayesMulFeature_test(Xtest, ytest,thetaPos, thetaNeg):
yPredict = []
for i in range (0, Xtest.shape[0]):
posPoss = 0
negPoss = 0
for j in range(0,15):
negPoss += Xtest.item((i,j)) * math.log(thetaNeg[j])
posPoss += Xtest.item((i,j)) * math.log(thetaPos[j])
if(negPoss<posPoss):
yPredict.append(1)
else:
yPredict.append(-1)
numOfCorrect = 0
len_ytest = len(ytest)
for i in range (0, len_ytest):
if ytest[i] == yPredict[i]:
numOfCorrect+=1
Accuracy = float(numOfCorrect)/len_ytest
# print Accuracy
return yPredict, Accuracy
def naiveBayesMulFeature_sk_MNBC(Xtrain, ytrain, Xtest, ytest):
clf = MultinomialNB()
clf.fit(Xtrain, ytrain)
yPredict = clf.predict(Xtest)
numOfCorrect = 0
len_ytest = len(ytest)
for i in range (0, len_ytest):
if ytest[i] == yPredict[i]:
numOfCorrect+=1
Accuracy = float(numOfCorrect)/len_ytest
return Accuracy
def naiveBayesMulFeature_testDirectOne(path,thetaPos, thetaNeg, vocabulary):
vocab= ["love","wonderful", "best","great", "superb", "still", "beautiful", "bad", "worst", "stupid",
"waste", "boring", "?", "!", "UNK"]
#The order will change
#vocabulary.keys() = ['beautiful', 'love', 'worst', 'wonderful', 'still', 'best', '!', 'great', 'UNK', 'boring', 'superb', 'bad', 'stupid', 'waste', '?']
# print vocabulary.keys()
posPoss = 0
negPoss = 0
yPredict = 0
with open(path) as f:
for line in f:
for token in line.split():
if token in vocab:
index = vocab.index(token)
posPoss += math.log(thetaPos[index])
negPoss += math.log(thetaNeg[index])
else:
posPoss += math.log(thetaPos[14])
negPoss += math.log(thetaNeg[14])
if posPoss>negPoss:
yPredict = 1
else:
yPredict = -1
return yPredict
def naiveBayesMulFeature_testDirect(path,thetaPos, thetaNeg, vocabulary):
yPredict = []
numOfCorrect = 0
total = 0
for filename in os.listdir(path+"neg/"):
filepath = path+"neg/" +filename
preY = naiveBayesMulFeature_testDirectOne(filepath,thetaPos, thetaNeg, vocabulary)
yPredict.append(preY)
total += 1
if (preY == -1):
numOfCorrect += 1
for filename in os.listdir(path+"pos/"):
filepath = path+"pos/" +filename
preY = naiveBayesMulFeature_testDirectOne(filepath,thetaPos, thetaNeg, vocabulary)
yPredict.append(preY)
total += 1
if (preY == 1):
numOfCorrect += 1
Accuracy = float(numOfCorrect)/total
print Accuracy
return yPredict, Accuracy
def naiveBayesBernFeature_train(Xtrain, ytrain):
thetaPosTrue=[]
thetaNegTrue=[]
numOfPos = 0
numOfNeg = 0
index = 0
pos_Matrix = Xtrain[0:700]
index = 0
for k in range(0,15):
sum = 0
for i in pos_Matrix:
if i[index] > 0:
sum+=1
thetaPosTrue.append(float(sum+1)/(700+2))
index+=1
neg_Matrix = Xtrain[700:1400]
index = 0
for k in range(0,15):
sum = 0
for i in neg_Matrix:
if i[index] > 0:
sum+=1
thetaNegTrue.append(float(sum+1)/(700+2))
index+=1
# print thetaPosTrue
# print thetaNegTrue
return thetaPosTrue, thetaNegTrue
def naiveBayesBernFeature_test(Xtest, ytest, thetaPosTrue, thetaNegTrue):
yPredict = []
for i in range (0, Xtest.shape[0]):
posPoss = 0
negPoss = 0
for j in range(0,15):
negPoss += Xtest.item((i,j)) * math.log(thetaNegTrue[j])
posPoss += Xtest.item((i,j)) * math.log(thetaPosTrue[j])
if(negPoss<posPoss):
yPredict.append(1)
else:
yPredict.append(-1)
numOfCorrect = 0
len_ytest = len(ytest)
for i in range (0, len_ytest):
if ytest[i] == yPredict[i]:
numOfCorrect+=1
Accuracy = float(numOfCorrect)/len_ytest
return yPredict, Accuracy
# textDataSetsDirectoryFullPath = './data_sets/'
# testFileDirectoryFullPath = './data_sets/test_set/'
# Xtrain, Xtest, ytrain, ytest = loadData(textDataSetsDirectoryFullPath)
# thetaPos, thetaNeg = naiveBayesMulFeature_train(Xtrain, ytrain)
# naiveBayesMulFeature_test(Xtest, ytest,thetaPos, thetaNeg)
# Accuracy = naiveBayesMulFeature_sk_MNBC(Xtrain, ytrain, Xtest, ytest)
# naiveBayesMulFeature_testDirectOne("data_sets/test_set/pos/cv701_14252.txt",thetaPos, thetaNeg, vocabulary)
# Accuracy = naiveBayesMulFeature_testDirect(testFileDirectoryFullPath, thetaPos, thetaNeg, vocabulary)
# thetaPosTrue, thetaNegTrue = naiveBayesBernFeature_train(Xtrain, ytrain)
# naiveBayesBernFeature_test(Xtest, ytest, thetaPosTrue, thetaNegTrue)
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: python naiveBayes.py dataSetPath testSetPath"
sys.exit()
print "--------------------"
textDataSetsDirectoryFullPath = sys.argv[1]
testFileDirectoryFullPath = sys.argv[2]
Xtrain, Xtest, ytrain, ytest = loadData(textDataSetsDirectoryFullPath)
thetaPos, thetaNeg = naiveBayesMulFeature_train(Xtrain, ytrain)
print "thetaPos =", thetaPos
print "thetaNeg =", thetaNeg
print "--------------------"
yPredict, Accuracy = naiveBayesMulFeature_test(Xtest, ytest, thetaPos, thetaNeg)
print "MNBC classification accuracy =", Accuracy
Accuracy_sk = naiveBayesMulFeature_sk_MNBC(Xtrain, ytrain, Xtest, ytest)
print "Sklearn MultinomialNB accuracy =", Accuracy_sk
yPredict, Accuracy = naiveBayesMulFeature_testDirect(testFileDirectoryFullPath, thetaPos, thetaNeg,vocabulary)
print "Directly MNBC tesing accuracy =", Accuracy
print "--------------------"
thetaPosTrue, thetaNegTrue = naiveBayesBernFeature_train(Xtrain, ytrain)
print "thetaPosTrue =", thetaPosTrue
print "thetaNegTrue =", thetaNegTrue
print "--------------------"
yPredict, Accuracy = naiveBayesBernFeature_test(Xtest, ytest, thetaPosTrue, thetaNegTrue)
print "BNBC classification accuracy =", Accuracy
print "--------------------"