-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp-project-sentiment-analysis.py
291 lines (208 loc) · 8.46 KB
/
nlp-project-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
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
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
#load preprocessed dataset
df_reviews = pd.read_csv('data/pre_yelp_500k.csv')
#for the testing purpose, we will only use 100k entries
df_reviews_100k = df_reviews[:100000]
#label positive, negative, neutral sentiments based on the star ratings
condition = [(df_reviews_100k['stars']>=4), (df_reviews_100k['stars']<=2), (df_reviews_100k['stars']==3)]
values = ['positive','negative','neutral']
df_reviews_100k['sentiment']=np.select(condition, values)
#save in CSV
#df_reviews_100k.to_csv('data/pre_yelp_100k_sentiment.csv', index=False)
#checkpoint: load 100k sentiment data from csv
df_reviews_sentiment = pd.read_csv('data/pre_yelp_100k_sentiment.csv')
#plot the sentiment distribution
plt.figure(figsize=(6,6))
df_reviews_sentiment['sentiment'].value_counts().plot(kind='pie', autopct='%1.0f%%')
plt.title('Review Sentiments');
#we will exclude neutral sentiments as we want to focus on extracting positive/negative sentiments
#for the testing purpose, we will only use 10k entries
df_reviews_sentiment = df_reviews_sentiment[df_reviews_sentiment['sentiment']!='neutral'][:10000]
df_reviews_sentiment
##### TF-IDF - Feature Extraction #####
#TF-IDF using Scikit-Learn library
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
#max_features:uses the 2,500 most frequently occuring words - removed
#min_df:include words that occur in at least 7 documents
#max_df:use words that occur in a maximum of 80% of the documents
vectorizer = TfidfVectorizer (min_df=7, max_df=0.8, stop_words=stopwords.words('english'))
processed_features = vectorizer.fit_transform(df_reviews_sentiment['text'].values.astype('U')).toarray()
#split into training and test datasets in 8:2 ratio
X_train, X_test, y_train, y_test = train_test_split(processed_features, df_reviews_sentiment['sentiment'], test_size=0.2, random_state=0)
######## 1. LOGISTIC REGRESSION ########
# fitting a logistic regression model
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import warnings
warnings.filterwarnings("ignore")
# Fitting Logistic regression to the training set
log_model = LogisticRegression(solver='lbfgs',multi_class='auto',random_state=1)
log_model.fit(X_train, y_train)
# Predicting the test set results
log_pred = log_model.predict(X_test)
# Training score
print("Score on training set: ", log_model.score(X_train,y_train))
print("Score on test set: ",log_model.score(X_test,y_test))
print(confusion_matrix(y_test,log_pred))
print(classification_report(y_test,log_pred))
print("accuracy score: ",accuracy_score(y_test, log_pred))
"""
[[ 364 151]
[ 26 1459]]
precision recall f1-score support
negative 0.93 0.71 0.80 515
positive 0.91 0.98 0.94 1485
accuracy 0.91 2000
macro avg 0.92 0.84 0.87 2000
weighted avg 0.91 0.91 0.91 2000
0.9115
"""
def plot_confusion_matrix(data, labels, output_filename):
"""Plot confusion matrix using heatmap.
Args:
data (list of list): List of lists with confusion matrix data.
labels (list): Labels which will be plotted across x and y axis.
output_filename (str): Path to output file.
"""
sns.set(color_codes=True)
plt.figure(1, figsize=(9, 6))
plt.title("Confusion Matrix")
sns.set(font_scale=1.4)
ax = sns.heatmap(data, annot=True, cmap="YlGnBu", cbar_kws={'label': 'Scale'}, fmt='g')
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)
ax.set(ylabel='Actual values', xlabel='Predication')
#plt.savefig(output_filename, bbox_inches='tight', dpi=300)
plt.show()
plt.close()
# define data
data = confusion_matrix(y_test,log_pred)
# define labels
labels = ["Negative", "Positive"]
# plot confusion matrix
plot_confusion_matrix(data, labels, "confusion_matrix.png")
######## 2. CART CLASSIFIER ########
from sklearn.tree import DecisionTreeClassifier
# Create and train decision tree classifer object
clf = DecisionTreeClassifier()
clf = clf.fit(X_train,y_train)
# Predicting the test set
clf_pred = clf.predict(X_test)
# training score
print(confusion_matrix(y_test,clf_pred))
print(classification_report(y_test,clf_pred))
print("accuracy score: ",accuracy_score(y_test, clf_pred))
"""
[[ 329 186]
[ 172 1313]]
precision recall f1-score support
negative 0.66 0.64 0.65 515
positive 0.88 0.88 0.88 1485
accuracy 0.82 2000
macro avg 0.77 0.76 0.76 2000
weighted avg 0.82 0.82 0.82 2000
accuracy score: 0.821
"""
#visualize the tree
import graphviz
dot_data = tree.export_graphviz(clf, out_file=None,
feature_names=vectorizer.get_feature_names(),
filled=True, max_depth=2)
graphviz.Source(dot_data, format="png")
# define data
data = confusion_matrix(y_test,clf_pred)
# plot confusion matrix
plot_confusion_matrix(data, labels, "confusion_matrix.png")
######## 3. RANDOM FOREST ########
#Build a Random Forest classifier model
text_classifier = RandomForestClassifier(n_estimators=200, random_state=0)
text_classifier.fit(X_train, y_train)
# Predicting the test set
predictions = text_classifier.predict(X_test)
#training score
print(confusion_matrix(y_test,predictions))
print(classification_report(y_test,predictions))
print("accuracy score: ",accuracy_score(y_test, predictions))
"""
[[ 312 203]
[ 22 1463]]
precision recall f1-score support
negative 0.93 0.61 0.73 515
positive 0.88 0.99 0.93 1485
accuracy 0.89 2000
macro avg 0.91 0.80 0.83 2000
weighted avg 0.89 0.89 0.88 2000
0.8875
"""
# define data
data = confusion_matrix(y_test,predictions)
# plot confusion matrix
plot_confusion_matrix(data, labels, "confusion_matrix.png")
######## 4. XGBOOST ########
from xgboost import XGBClassifier
from sklearn.metrics import mean_squared_error
#Build XGB model
xgb_model = XGBClassifier(random_state=1)
xgb_model.fit(X_train, y_train)
# Predicting the test set
xgb_pred = xgb_model.predict(X_test)
#training score
print("XGBoost score - trainnig set: ",xgb_model.score(X_train, y_train))
print("XGBoost score - test set: ",xgb_model.score(X_test, y_test))
print(confusion_matrix(y_test,xgb_pred))
print(classification_report(y_test,xgb_pred))
print("accuracy score: ",accuracy_score(y_test, xgb_pred))
"""
XGBoost score - trainnig set: 0.985875
XGBoost score - test set: 0.9135
[[ 396 119]
[ 54 1431]]
precision recall f1-score support
negative 0.88 0.77 0.82 515
positive 0.92 0.96 0.94 1485
accuracy 0.91 2000
macro avg 0.90 0.87 0.88 2000
weighted avg 0.91 0.91 0.91 2000
accuracy score: 0.9135
"""
# define data
data = confusion_matrix(y_test,XGB_pred)
# plot confusion matrix
plot_confusion_matrix(data, labels, "confusion_matrix.png")
######## 5. Multilayer Perceptron Classifier ########
from sklearn.neural_network import MLPClassifier
#multilayer perceptron classifier
mlp = MLPClassifier()
mlp.fit(X_train,y_train)
# Predicting the test set
mlp_pred = mlp.predict(X_test)
#training score
print("Confusion Matrix for Multilayer Perceptron Classifier:")
print(confusion_matrix(y_test,mlp_pred))
print("Classification Report:")
print(classification_report(y_test,mlp_pred))
print("accuracy score:",accuracy_score(y_test,mlp_pred))
"""
Confusion Matrix for Multilayer Perceptron Classifier:
[[ 414 101]
[ 62 1423]]
Classification Report:
precision recall f1-score support
negative 0.87 0.80 0.84 515
positive 0.93 0.96 0.95 1485
accuracy 0.92 2000
macro avg 0.90 0.88 0.89 2000
weighted avg 0.92 0.92 0.92 2000
accuracy score: 0.9185
"""
# define data
data = confusion_matrix(y_test,mlp_pred)
# plot confusion matrix
plot_confusion_matrix(data, labels, "confusion_matrix.png")