/
nbTest.py
153 lines (99 loc) · 4.09 KB
/
nbTest.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
import numpy as np
from sklearn.naive_bayes import GaussianNB
import configparser
config = configparser.ConfigParser()
config.read(r'C:\Python34\configFile.txt')
activeFeatureIndex = [int(config['FEATURES'][x]) for x in config['FEATURES']]
def trainNB():
featureVector = []
classVector = []
temp= []
headerLine = True
#training
train = open(r'C:\Python34\alchemyapi_python\TrainingDataDummy.csv')
for line in train:
if(headerLine):
headerLine = False
else:
temp = line.split(",")
x = [float(temp[i]) for i in activeFeatureIndex]
#print(x)
featureVector.append(x)
#temp = [int(x) for x in line.split(",")[-1].rstrip("\n")]
classVector.append(int(line.split(",")[-1].rstrip("\n")))
fVector = np.array(featureVector)
cVector = np.array(classVector)
#print(classVector)
print(fVector.shape)
print(cVector.shape)
clf = GaussianNB()
clf.fit(fVector,cVector)
train.close()
return clf
def predictNB(clf):
out = open(r'C:\Python34\alchemyapi_python\resultNB.txt','w')
headerLine = True
print("going to test")
#print(activeFeatureIndex)
#testing
out.write("#reviewID;#businessID;Rating;#secondPronouns;#capitalWords;#excSentences;Length;EentityCount;SentimentScore;#reviewerFriend;reviewerReviewCount;Class\n")
test = open(r'C:\Python34\alchemyapi_python\TestingDataDummy.csv')
for line in test:
if(headerLine):
headerLine = False
else:
#print("-->" + line)
tokens = line.split(",")
#print(tokens)
temp = [float(tokens[i]) for i in activeFeatureIndex]
#print(x)
#featureVector.append(temp)
#print(str(type(clf.predict([temp]))))
x = [temp]
print(x)
classPrediction = clf.predict(x)
## print(type(classPrediction[0]))
## print(classPrediction.shape)
## print(classPrediction[0])
out.write(str(tokens[0]) + ";" + str(tokens[1])+ ";" + str(tokens[2]) + ";" + str(tokens[3]) + ";" + str(tokens[4])+ ";" + str(tokens[5]) + ";" + str(tokens[6])+ ";" + str(tokens[7])+ ";" + str(tokens[8])+ ";" + str(tokens[9])+ ";" + str(tokens[10])+ ";" + str(classPrediction[0]) + "\n")
out.close()
test.close()
def predictNBKBest(clf,featureSelectorArray,outputFile):
#out = open(r'C:\Python34\alchemyapi_python\resultNB.txt','w')
headerLine = True
print("going to test")
print(featureSelectorArray)
#testing
outputFile.write("#reviewID;#businessID;Rating;#secondPronouns;#capitalWords;#excSentences;Length;EentityCount;SentimentScore;#reviewerFriend;reviewerReviewCount;Class\n")
test = open(r'C:\Python34\alchemyapi_python\TestingData_New.csv')
for line in test:
if(headerLine):
headerLine = False
else:
#print("-->" + line)
tokens = line.split(",")
#print(tokens)
temp = [float(tokens[i+2]) for i,val in enumerate(featureSelectorArray) if val== True]
#print(x)
#featureVector.append(temp)
#print(str(type(clf.predict([temp]))))
x = [temp]
#print(x)
classPrediction = clf.predict(x)
## print(type(classPrediction[0]))
## print(classPrediction.shape)
## print(classPrediction[0])
outputFile.write(str(tokens[0]) + ";" + str(tokens[1])+ ";" + str(tokens[2]) + ";" + str(tokens[3]) + ";" + str(tokens[4])+ ";" + str(tokens[5]) + ";" + str(tokens[6])+ ";" + str(tokens[7])+ ";" + str(tokens[8])+ ";" + str(tokens[9])+ ";" + str(tokens[10])+ ";" + str(classPrediction[0]) + "\n")
print("NB Classification Done")
outputFile.close()
test.close()
return 0
def trainNBKBest(featureVector):
clf = GaussianNB()
clf.fit(featureVector,cVector)
train.close()
def main():
classifier = trainNB()
predictNB(classifier)
if __name__ == "__main__":
main()