-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis.py
266 lines (217 loc) · 8.94 KB
/
sentiment_analysis.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
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
import csv
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import BernoulliNB
from sklearn import cross_validation
from sklearn.metrics import classification_report
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn import svm
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
# review.csv contains two columns
# first column is the review content (quoted)
# second column is the assigned sentiment (positive or negative)
NB = []
SVM = []
LR = []
def load_file():
with open('train.csv') as csv_file:
reader = csv.reader(csv_file,delimiter=",",quotechar='"')
reader.next()
data =[]
target = []
for row in reader:
# skip missing data
if row[1] and row[4]:
data.append(row[1])
target.append(row[4])
return data,target
# preprocess creates the term frequency matrix for the review data set
def preprocess_unigram():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(1, 1),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
#return data
def preprocess_bigram():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(2, 2),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
def preprocess_trigram():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(3, 3),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
# preprocess creates the term frequency matrix for the review data set
def preprocess_bigram_trigram():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(2, 3),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
#return data
def preprocess_unigram_bigram():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(1, 2),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
def preprocess_trigram_u_b():
data,target = load_file()
count_vectorizer = CountVectorizer(ngram_range=(1, 3),binary='False',max_df = 0.5, max_features = 18000)
data = count_vectorizer.fit_transform(data)
#print np.shape(data)
tfidf_data = TfidfTransformer(use_idf=True).fit_transform(data)
return tfidf_data
def learn_model(data,target):
# preparing data for split validation. 60% training, 40% test
data_train,data_test,target_train,target_test = cross_validation.train_test_split(data,target,test_size=0.50,random_state=43)
classifier = BernoulliNB().fit(data_train,target_train)
predicted = classifier.predict(data_test)
print np.shape(predicted)
#print target_test[0:10]
#evaluate_model(target_test,predicted)
NB.append(evaluate_model(target_test,predicted)*100)
return predicted
def learn_model_svm(data,target):
# preparing data for split validation. 60% training, 40% test
data_train,data_test,target_train,target_test = cross_validation.train_test_split(data,target,test_size=0.50,random_state=43)
# Perform classification with SVM, kernel=linear
classifier_linear = svm.LinearSVC()
#classifier_linear = svm.SVC(kernel='linear')
#classifier_linear = svm.SVC()
#t0 = time.time()
classifier_linear.fit(data_train,target_train)
#t1 = time.time()
predicted = classifier_linear.predict(data_test)
#print np.shape(predicted)
#print target_test[0:10]
#t2 = time.time()
#evaluate_model(target_test,predicted)
SVM.append(evaluate_model(target_test,predicted)*100)
return predicted
def learn_model_logistic(data,target):
# preparing data for split validation. 80% training, 20% test
data_train,data_test,target_train,target_test = cross_validation.train_test_split(data,target,test_size=0.50,random_state=43)
# Perform classification with SVM, kernel=linear
#classifier_linear = svm.LinearSVC()
classifier_linear = LogisticRegression()
#classifier_linear = svm.SVC()
#t0 = time.time()
classifier_linear.fit(data_train,target_train)
#t1 = time.time()
predicted = classifier_linear.predict(data_test)
#print np.shape(predicted)
#print target_test[0:10]
#t2 = time.time()
LR.append(evaluate_model(target_test,predicted)*100)
return predicted
# read more about model evaluation metrics here
# http://scikit-learn.org/stable/modules/model_evaluation.html
def evaluate_model(target_true,target_predicted):
print classification_report(target_true,target_predicted)
print "The accuracy score is {:.2%}".format(accuracy_score(target_true,target_predicted))
return accuracy_score(target_true,target_predicted)
def apply_model(tf_idf,target,data):
print "Naive Bayes"
nb = learn_model(tf_idf,target)
print "Support Vector Machine"
svm = learn_model_svm(tf_idf,target)
print "Logistic Regression"
lr = learn_model_logistic(tf_idf,target)
data_train,data_test,target_train,target_test = cross_validation.train_test_split(data,target,test_size=0.50,random_state=43)
final_pred = []
for i in range(0,19466):
c1 = 0
if nb[i] == 'happy':
c1 = c1 + 1
if lr[i] == 'happy':
c1 = c1 + 1
if svm[i] == 'happy':
c1 = c1 + 1
#print i
if c1 == 3 or c1 == 2:
final_pred.append('happy')
else:
final_pred.append('not happy')
print "-----------------------"
#print final_pred
print "-----------------------"
#print new_label
print "Results of ensemble: NB + SVM + ME::"
print "----------Confusion Matrix--------------"
print classification_report(target_test,final_pred)
print ""
print "The accuracy score of ensemble is {:.2%}".format(accuracy_score(target_test,final_pred))
print "##############################################"
#print nb[0:10],svm[0:10],lr[0:10]
def graph():
N = 3
ind = np.arange(N) # the x locations for the groups
width = 0.20 # the width of the bars
fig, ax = plt.subplots()
NB1 = (NB[0],NB[1],NB[2])
SVM1 = (SVM[0],SVM[1],SVM[2])
LR1 = (LR[0],LR[1],LR[2])
rects1 = ax.bar(ind, NB1, width, color='r')
rects2 = ax.bar(ind + width, SVM1, width, color='y')
rects3 = ax.bar(ind + width*2, LR1, width, color='g')
Ensembles_values = (82.28, 82.69, 82.48)
print Ensembles_values
rects4 = ax.bar(ind + width*3, Ensembles_values, width, color='b')
# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by Features and accuracy')
ax.set_xticks(ind + width)
ax.set_xticklabels(('Unigram', 'Bigram', 'trigram'))
plt.ylim([75,88])
ax.legend((rects1[0], rects2[0], rects3[0],rects4[0]), ('Naive Bayes', 'SVM', 'ME', 'Ensemble'))
def autolabel(rects):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2., 1.00*height,
'%.2f' % (height),
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
autolabel(rects3)
autolabel(rects4)
plt.show()
def main():
data,target = load_file()
print "--------------------- Unigram ---------------------------"
tf_idf = preprocess_unigram()
apply_model(tf_idf,target,data)
print "--------------------- Bigram ---------------------------"
tf_idf = preprocess_bigram()
apply_model(tf_idf,target,data)
print "----------------------- Trigram -------------------------"
tf_idf = preprocess_trigram()
apply_model(tf_idf,target,data)
print "--------------------- Unigram + Bigram---------------------------"
tf_idf = preprocess_unigram_bigram()
apply_model(tf_idf,target,data)
print "--------------------- Bigram + Trigram---------------------------"
tf_idf = preprocess_bigram_trigram()
apply_model(tf_idf,target,data)
print "----------------------- Trigram+Unigram+Bigram -------------------------"
tf_idf = preprocess_trigram_u_b()
apply_model(tf_idf,target,data)
print NB
print SVM
print LR
graph()
main()