forked from remzawi/CS229-theme-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNB.py
More file actions
214 lines (185 loc) · 7.63 KB
/
NB.py
File metadata and controls
214 lines (185 loc) · 7.63 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
import csv
import os
import sys
import numpy as np
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, precision_score, f1_score, recall_score,confusion_matrix
from sklearn.model_selection import train_test_split
import pickle
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
class Preprocess:
def __init__(self,X): #Creates the dictionary of feature words
self.word_dictionary = self.create_dict(X)
def create_dict(self,X):
worddict = {}
dictionary = {}
i = 0; percent = 0;
stopWords = set(stopwords.words('english')) #Implements stopwords
stopWords = self.get_words(" ".join(stopWords))
print("Creating dictionary:")
print("=" * 20)
for message in X:
#Prints progress
i += 1
if np.rint(100*i/len(X)) != percent:
percent = np.rint(100*i/len(X))
if percent%1 == 0:
sys.stdout.write(f"\r{int(percent)}%")
sys.stdout.flush()
if percent == 100: print();
#print(f"{percent}%")
#Counts occurances of words in the message
message = self.get_words(message)
previouslist = []
for word in message:
if word not in previouslist and word not in stopWords:
if word not in worddict.keys():
worddict[word] = 0
worddict[word] += 1
previouslist.append(word)
#Creates the dictionary if a word exists more than threshold times and returns dictionary.
threshold = 150
for key in worddict.keys():
if worddict[key] > threshold:
dictionary[key] = len(dictionary)
print(len(dictionary))
f=open("dict6.pkl","wb")
pickle.dump(dictionary,f)
f.close()
return dictionary
def get_words(self,message):
#Takes a string as input and returns a list consisting of lower case words of stem form
message = message.lower()
message = message.replace(".","")
message = message.replace(",", "")
message = word_tokenize(message)
ps = PorterStemmer()
stemmed_message = []
for word in message:
stemmed_message.append(ps.stem(word))
return stemmed_message
def transform_text(self,messages):
#Creates a matrix of features
featureMatrix = np.zeros((len(messages),len(self.word_dictionary.keys())), dtype=int)
#Prints progress
percent = 0
if len(messages) != 1:
print("Generating feature vectors from messages:")
print("=" * 20)
for counter, message in enumerate( messages):
if np.rint(100*counter/len(messages)) != percent:
percent = np.rint(100*counter/len(messages))
if percent % 1 == 0:
sys.stdout.write(f"\r{int(percent)}%")
sys.stdout.flush()
if percent == 100: print();
message = self.get_words(message)
#Converts the words into a feature matrix
for word in message:
if word in self.word_dictionary.keys():
featureMatrix[counter, self.word_dictionary[word]] += 1
return featureMatrix
class sentimentpredictor:
#Naive bayes classifier
def __init__(self, updateDictionary = True):
#Variable initialization
preprocesssavefile = "preprocessfile6.sav"
self.y = []
sentences = []
stop = 0 #Limit number of lines to be read
#Read from a CSV file
with open('newquotes6.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
line_count = 0
for row in csv_reader:
self.y.append(row[2])
sentences.append(row[0])
line_count += 1
if line_count == stop:
break
print(f'{line_count} lines read.')
#Turn the list into ndarrays
self.y = np.asarray(self.y)
sentences = np.asarray(sentences)
#If updateDictionary then calculate the new dictionary values, otherwise read object from storage
if updateDictionary or not os.path.isfile(preprocesssavefile):
file = open(preprocesssavefile, "wb")
self.preprocesser = Preprocess(sentences)
pickle.dump(self.preprocesser, file)
print("Dictionary created")
print("=" * 20+"\n")
else:
file = open(preprocesssavefile, "rb")
self.preprocesser = pickle.load(file)
print("=" * 20)
print("Dictionary Loaded")
print("=" * 20+"\n")
file.close()
#Transform sentences into featurevectors
self.X = self.preprocesser.transform_text(sentences)
self.clf = None
self.labels = None
def train(self):
#Train model
X_train, X_test, y_train, y_test = train_test_split(self.X, self.y, test_size = .1)#, random_state = 0)
self.clf = MultinomialNB(alpha=10)
self.clf.fit(X_train, y_train)
self.labels = self.clf.classes_
print("Model Trained")
y_pred = self.clf.predict(X_test)
y_predtrain=self.clf.predict(X_train)
cf=confusion_matrix(y_test,y_pred)
np.save("confusion_matrix_NB.npy",cf)
np.save('x_test_NB.npy',X_test)
##Calculating metrics
print(accuracy_score(y_train,y_predtrain))
warnings.simplefilter("ignore")
accuracy = accuracy_score(y_test, y_pred)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='macro')
fscore = f1_score(y_test, y_pred, average='macro')
warnings.simplefilter("default")
print(f"Model acheives {accuracy} accuracy on test set.")
print(f"Model acheives {precision} precision on test set.")
print(f"Model acheives {recall} recall on test set.")
print(f"Model acheives {fscore} fscore on test set.")
print("=" * 20)
def predict(self,sentence):
#Make predictions on model
Xpred = self.preprocesser.transform_text([sentence])
probabilities = self.clf.predict_proba(Xpred.reshape(1, -1))
return probabilities
def classes(self):
l=[]
for label in self.labels:
l.append(label)
return l
#True if loading model form memory, otherwise a new model will be generated
usenewmodel = False
#True if you want to updatedictionary before training model
updatedictionary = False
if usenewmodel or not os.path.isfile("sentimentfinder6.sav"):
#Train model
sentimentprobabilities2 = sentimentpredictor(updateDictionary = updatedictionary)
file = open("sentimentfinder6.sav", "wb")
pickle.dump(sentimentprobabilities2,file)
print("=" * 20)
print("New feature matrix used for training")
else:
file = open("sentimentfinder6.sav", "rb")
sentimentprobabilities2 = pickle.load(file)
print("=" * 20)
print("Previous feature matrix used for training")
file.close()
#Generate prediction
sentimentprobabilities2.train()
#predictions = sentimentprobabilities2.predict("This is an example sentence that we can use to predict different classes, mom mom mom mom")
#Creates a dictionary with probabilities
#a =predictions.flatten().tolist()
#b =sentimentprobabilities.labels.tolist()
#map = zip(b,a)
#print(set(map))