forked from tuandat95cbn/20162_DataMining_SMS_challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_bayes_onehotvector_dicths.py
85 lines (79 loc) · 1.79 KB
/
naive_bayes_onehotvector_dicths.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
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 15 18:27:49 2017
@author: T420
"""
f = open('train.txt', 'r')
g = open('test.txt','r')
test= g.read().splitlines()
g.close()
g=open('spam-harm-dict.txt','r')
e=g.read();
dictHarmSpam=e.split(' ');
print set(dictHarmSpam).__len__()
g.close()
def getInputOneHotVector(data,dict):
inputY=[]
inputWord=[];
for x in data:
y=x.split(" ")
inputWord.append(y[1:])
inputY.append(y[0])
inputX=[]
for x in inputWord:
v=[]
for xx in dict:
if xx in x:
v.append(1)
else:
v.append(0)
for xx in dictHarmSpam:
if xx in x:
v.append(1)
else:
v.append(0)
inputX.append(v)
return inputX,inputY
#f.read()
LTrain=100
nTimes=1
t= f.read().splitlines()
f.close()
nTrain=t.__len__()
dict=[]
def aRunProcess(dict):
inputY=[]
for x in t:
y=x.split(" ")
dict.extend(y[1:])
dict=set(dict)
inputX=[]
[inputX,inputY]=getInputOneHotVector(t,dict);
[testX,testY]=getInputOneHotVector(test,dict);
trainX=inputX[:LTrain]
trainY=inputY[:LTrain]
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(trainX, trainY)
res=gnb.predict(testX)
print res
#sol=(res==testY)
#return sol.sum()
return res
acc=0;
g0=open("test_nvb.txt","w");
res=aRunProcess(dict)
for i in range(0,test.__len__()):
y=test[i].split(" ")
g0.write(str(res[i])+" ")
g0.write(" ".join(y[1:]))
g0.write("\n");
g0.close()
testX=[]
testY=[]
g = open('test_human.txt','r')
test= g.read().splitlines()
g.close()
[testX,testY]=getInputOneHotVector(test,dict);
acc=(res==testY).sum()*1.0/test.__len__()
print "accuracy of "+str(1)+" is : "+ str(acc)