-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_code.py
430 lines (368 loc) · 14.1 KB
/
application_code.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import streamlit as st
import time
import altair as alt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler, OneHotEncoder, MinMaxScaler, MaxAbsScaler
from utilities import create_pipeline_object, create_df
from sklearn.metrics import classification_report, ConfusionMatrixDisplay
import streamlit as st
import streamlit.components.v1 as components
from sklearn.utils import estimator_html_repr
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay, accuracy_score
from sklearn.metrics import precision_score, recall_score, f1_score
from pprint import pprint
from sklearn.metrics import plot_confusion_matrix, plot_roc_curve, plot_precision_recall_curve
st.set_page_config(layout="wide")
st.set_option('deprecation.showPyplotGlobalUse', False)
# Title and subtitle
st.write("""
# Twitter Sentiment Analysis
#### Building a Classification Model for Twitter Sentiment Analysis using Sklearn.
"""
)
try:
df = pd.read_csv('data/train_data.csv')
print('df loading successful')
except:
print('Something went wrong in loading data.')
# add introduction about the data
st.write("""
### Introduction
- Many of us scroll through twitter first thing in the morning.
- Tweets in Twitter can convey different sentiments to people and it is interesting to build a model that can identify the sentiment of a tweet beforehand.
- This project is about building a classification model on tweets to identify whether they are Positive or Negative.
- The dataset is from Analytics Vidhya website open competitions.
""")
# ADD SEPARATOR
st.write("""
-----
""")
# Feature explanation tab
int_tab_1, int_tab_2 = st.tabs(
[
'Text Features', 'Numerical Features',
]
)
with int_tab_1:
st.write("""
#### There are three types of textual features
- i will be using straighforward methods like tfidfvectorizer and countvectorizer to extract features from preprocessed text.
- i used the below mentioned preprocessing steps:
- removing urls mentioned in the tweets
- removing usernames mentioned as @ and replacing them with just username
- removing and replacing all multiple whitespaces with single white space
- remove topics mentioned as #some_topic. we extract them and use them as a separate feature.
##### tweet
- the tweet is itself a feature, i will be using a tfidf vectorizer to extract features from the original tweet.
##### topics
- so i used the hashtags mentioned in a tweet as topics. i just extracted them and again use a tfidfvectorizer to extract features from them.
##### emojis
- the emojis present in the tweets are also good features. after extracting i convert them into features using countvectorizer.
""")
with int_tab_2:
st.write("""
#### There are five types of numerical features. These features are extracted from text only.
##### Length of Tweet
- Basically this is the number of words in a Tweet.
##### Number of Topics
- Total number of hashtags mentioned in the tweet.
##### Number of Emojis
- Total number of emojis present in the tweet.
##### Number of Slurrs
- Total number of slurrs or abusive words mentioned in the tweet.
##### Emoji Score
- Using scoring dict each emoji is given a score.
- This score is aggregated sum for all the emojis in the tweet.
""")
# ADD SEPARATOR
st.write("""
-----
""")
# show label distribution
st.write("""
### Label Distribution And Preparing Train and Test Data
- We wont be using Validation set as we will be using K-fold cross validation for parameter tuning.
- Below is the distribution of the labels in the entire dataset.
- We can see that the dataset is not balanced.
- Next step to split dataframe into train and test.
""")
chart_1 = alt.Chart(df).mark_bar(size=10).encode(
x='label:N',
y='count()',
).properties(
width=1000,
height=500
)
st.altair_chart(chart_1, use_container_width=True)
with st.sidebar:
# ask for test and valid percentage
train_test_split_size = st.sidebar.slider('Data split ratio (% for Training Set)', 70, 90, 80, 5)
# split data into train, test and validation
train_df, test_df = train_test_split(
df, test_size=(100 - train_test_split_size)/100.0, shuffle=True,
random_state=12, stratify=df['label']
)
st.write(
'Train Data has : {} tweets and Test Data has : {} tweets. Lets get to modelling.'.format(train_df.shape[0], test_df.shape[0])
)
# ask which text and numerical features to use to users
st.write("""
#### Please select the Numerical Features you are interested in using for model fitting.
""")
all_numeric_features = [
'num_topics', 'num_emojis', 'length_of_tweet',
'num_of_slurrs', 'emoji_score'
]
all_textual_features = ['tweet', 'label', 'topics', 'extracted_emojis']
numerical_features_to_use = st.multiselect(
'Numerical Features to use for modelling :',
all_numeric_features, all_numeric_features
)
all_columns = all_textual_features + numerical_features_to_use
train_df = train_df[all_columns]
test_df = test_df[all_columns]
# give options for scaling the numerical columns (standard, minmax , max abs)
st.write("""
#### Please select which scaling to use to preprocess numerical features:
""")
scaling_type = st.radio(
'Select the type of Scaling :',
('StandardScaler', 'MinMaxScaler', 'MaxAbsSclaer')
)
if scaling_type == "StandardScaler":
scaler = StandardScaler()
elif scaling_type == "MinMaxScaler":
scaler = MinMaxScaler()
else:
scaler = MaxAbsScaler()
# ask which classifiers to Fit for Voting Classifier
st.write("""
#### Please select the classifiers to be used inside the Voting Classifer.
- A Voting Classifier takes the output of the all the classifiers that are selected and gives that label as output which has the majority vote among the classifiers.
""")
all_classifiers = [
'logistic', 'svm', 'random_forest', 'knn', 'ada_boost'
]
classifiers_to_use = st.multiselect(
'Numerical Classifiers to use for modelling :',
all_classifiers, all_classifiers
)
# import pipeline object
classifier_pipeline = create_pipeline_object(
scaler, numerical_features_to_use, classifiers_to_use
)
with open("pipeline_image.html", "w") as f:
f.write(estimator_html_repr(classifier_pipeline))
# giving time for the html to save before loading
time.sleep(5)
# showing classifier_pipeline
# by rendering the html of the pipeline in an iframe.
st.write("""
#### Currently our Sklearn Pipeline looks like below:
""")
HtmlFile = open("pipeline_image.html", 'r', encoding='utf-8')
source_code = HtmlFile.read()
print(source_code)
components.html(source_code, height=400)
# ask for kfolds to use
st.write("""
#### Please select K-Fold value to tune hyper parameters using GridSearchCv :
""")
kfold_value = st.radio(
'',
(3, 5, 8, 9, 10)
)
# ask for scoring metric to use
st.write("""
#### Please select Scoring Methods for Evaluation:
""")
scoring_metric_option = st.radio(
'',
('Accuracy', 'F1', 'ROC_AUC')
)
# show the heyperparameter and ask user to fill
# tfidf_ranges = ['(1, 1)', '(1, 2)', '(1, 3)']
# general_tfidf__tfidf__ngram_range = st.multiselect(
# 'Select the Text TFIDF Ngram Ranges you want to experiment with :',
# tfidf_ranges, tfidf_ranges
# )
# min_df_ranges = ['0.0', '0.05', '0.1']
# general_tfidf__tfidf__min_df = st.multiselect(
# 'Select the Min_DF TFIDF Vocab Ranges you want to experiment with :',
# min_df_ranges, min_df_ranges
# )
# max_df_ranges = ['0.85', '0.9', '0.95']
# general_tfidf__tfidf__ngram_range = st.multiselect(
# 'Select the Max_DF Text TFIDF Vocan Ranges you want to experiment with :',
# max_df_ranges, max_df_ranges
# )
# split_size = st.sidebar.slider('Data split ratio (% for Training Set)', 50, 90, 80, 5)
# classifier params
parameters = {
# tweet text features
# "preprocessor__text__tweet_tfidf__tfidf__ngram_range": [(1, 1)],
# "preprocessor__text__tweet_tfidf__tfidf__min_df": [5],
# "preprocessor__text__tweet_tfidf__tfidf__max_df": [0.90],
# topic text features
# "preprocessor__text__topic_tfidf__tfidf__ngram_range": [(1, 1)],
# "preprocessor__text__topic_tfidf__tfidf__min_df": [2],
# "preprocessor__text__topic_tfidf__tfidf__max_df": [0.90],
# emoji text features
# "preprocessor__text__emoji_tfidf__tfidf__ngram_range": [(1, 1)],
# "preprocessor__text__emoji_tfidf__tfidf__min_df": [1],
# "preprocessor__text__emoji_tfidf__tfidf__max_df": [0.90],
}
with st.sidebar:
st.sidebar.write('---')
st.sidebar.subheader('Learning Parameters')
# for logistic regression
if 'logistic' in classifiers_to_use:
logistic_C_values = [0.1, 1, 10]
logistic_c = st.multiselect(
'Select the values of C for logistic regression you want to experiment with :',
logistic_C_values, [1]
)
logistic_penalty_options = ['l2', 'elasticnet']
logistic_penalty = st.multiselect(
'Select the values of penalty for logistic regression you want to experiment with :',
logistic_penalty_options, ['l2']
)
logistic_params_dict = {
"classifier__logistic__C": logistic_c,
'classifier__logistic__penalty': logistic_penalty
}
parameters.update(logistic_params_dict)
# for svm
if 'svm' in classifiers_to_use:
svm_kernel = st.sidebar.selectbox(
'Select SVM Kernel ', ('linear', 'poly', 'rbf')
)
svm_C_options = [0.1, 1, 10]
svm_c = st.multiselect(
'Select the values of C for SVM you want to experiment with :',
svm_C_options, [1]
)
svm_params_dict = {
'classifier__svm__C': svm_c,
'classifier__svm__kernel': [svm_kernel]
}
parameters.update(svm_params_dict)
# for random forest
if 'random_forest' in classifiers_to_use:
rf_n_estimators = st.sidebar.slider(
'Number of estimators for Random Forest (n_estimators)',
0, 500, (10,50), 50
)
rf_max_depth = st.sidebar.slider(
'Random Forest Maximum depth', 5, 15, (5,8), 2
)
rf_criterion = st.sidebar.selectbox(
'Random Forest criterion',('gini', 'entropy')
)
rf_params_dict = {
'classifier__random_forest__n_estimators': [
i for i in range(rf_n_estimators[0], rf_n_estimators[1], 50)
],
'classifier__random_forest__max_depth': [
i for i in range(rf_max_depth[0], rf_max_depth[1], 3)
],
'classifier__random_forest__criterion': [rf_criterion],
}
parameters.update(rf_params_dict)
# for knn
if 'knn' in classifiers_to_use:
knn_parameter_neighbors = st.sidebar.number_input(
'KNN Select the Number of Neighbors:', 2, 10
)
knn_params_dict = {
'classifier__knn__n_neighbors': [knn_parameter_neighbors]
}
parameters.update(knn_params_dict)
# for ada boost
if 'ada_boost' in classifiers_to_use:
ab_n_estimators = st.sidebar.slider(
'Number of estimators for AdaBoost',
0, 500, (10,50), 50
)
ab_learning_rate = st.sidebar.slider(
'Ada Boost learning Rate', 0, 10, 1, 1)
adaboost_params_dict = {
'classifier__ada_boost__n_estimators': [
i for i in range(ab_n_estimators[0], ab_n_estimators[1], 50)
],
'classifier__ada_boost__learning_rate': [ab_learning_rate]
}
parameters.update(adaboost_params_dict)
st.write("""
#### The parameters dict is printed below :
""")
st.write("{}".format(parameters))
# train model ; show animation
clf = GridSearchCV(
classifier_pipeline,
parameters,
cv=kfold_value,
scoring=scoring_metric_option.lower()
)
with st.spinner('Please Wait while the model is training.....'):
clf.fit(train_df, train_df["label"])
st.success('Done!')
# show best model and parameters
st.write("""
### Trained Model Best Score and parameters are mentioned below :
""")
st.write("Best Score achieved is : {}".format(clf.best_score_))
st.write("Best Parameters according to GridSearchCV are : ")
st.write("{}".format(clf.best_params_))
# get results on test set
with st.spinner('Please Wait while the Inference is going on the Test data....'):
test_df['predictions'] = list(clf.predict(test_df))
st.success('Done!')
st.write('------')
st.write("""
#### The Results on the Test Set are:
""")
model_accuracy = accuracy_score(test_df['label'], test_df['predictions']).round(2)
model_precision = precision_score(
test_df['label'], test_df['predictions'],
labels=clf.classes_).round(2)
model_recall = recall_score(
test_df['label'], test_df['predictions'],
labels=clf.classes_).round(2)
model_f1_score = f1_score(
test_df['label'], test_df['predictions'],
labels=clf.classes_).round(2)
col1, col2, col3, col4 = st.columns(4)
col1.metric("Accuracy", "{}".format(model_accuracy))
col2.metric("Precision", "{}".format(model_precision))
col3.metric("Recall", "{}".format(model_recall))
col4.metric("F1 Score", "{}".format(model_f1_score))
st.subheader("Confusion Matrix")
cm = confusion_matrix(test_df['label'], test_df['predictions'], labels=clf.classes_)
print(cm)
disp = ConfusionMatrixDisplay(
confusion_matrix=cm,
display_labels=clf.classes_
)
disp.plot()
st.pyplot()
# optional plot learning curve
# give option to input sentence and get output.
st.write("""
### Please go ahead and try your own Tweet below :
""")
input_text = st.text_input('Tweet', 'We love this! Would you go? #talk #makememories #unplug #relax #iphone #smartphone #wifi #connect')
st.write('The input tweet is : ', input_text)
temp_df = create_df(input_text)
prediction = list(clf.predict(temp_df))[0]
if prediction == '0':
output = 'Negative'
else:
output = 'Positive'
st.write("The prediction for the provided tweet is : {}".format(output))