-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngagement.py
More file actions
300 lines (167 loc) · 7.1 KB
/
Engagement.py
File metadata and controls
300 lines (167 loc) · 7.1 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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Data Manipulation
import pandas as pd
import numpy as np
import re
# NLP
from nltk.corpus import stopwords
from sklearn.naive_bayes import MultinomialNB
# Vectorizer
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
# ML
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.linear_model import LinearRegression, Ridge, Lasso, LogisticRegression
from sklearn.multiclass import OneVsRestClassifier
# import xgboost
# Warnings
import warnings
warnings.filterwarnings('ignore')
# In[2]:
# Loading the data
source = pd.read_csv('data.csv', parse_dates=['published'])
source = source.fillna(0)
source['published'] = pd.to_datetime(source['published'], errors='coerce')
source['engagement'] = source['engagement'].astype(int)
source['word_count'] = source['word_count'].astype(int)
# source['Tweet_type'] = source['Tweet_type'].astype('category')
source = source[['published', 'content', 'word_count', 'engagement']]
print(source.shape)
print(source.head(5))
# Taking a copy of the source file
data = source.copy()
# In[5]:
# Preprocessing the text data without removing negative stopwords
REPLACE_BY_SPACE_RE = re.compile("[/(){}\[\]\|@,;!]")
BAD_SYMBOLS_RE = re.compile("[^0-9a-z #+_]")
negation = ["no", "nor", "not", "don", "don't", "aren", "aren't", "couldn", "couldn't", "didn", "didn't", "doesn", "doesn't",
"hadn", "hadn't", "hasn", "hasn't", "haven", "haven't", "isn", "isn't", "mightn", "mightn't", "mustn", "mustn't",
"needn", "needn't", "shan", "shan't", "shouldn", "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
stop = set(stopwords.words('english')) - set(negation)
# Custom stopwords
stoplist = ['i','me','my','myself','we','our','ours','ourselves','you',"you're","you've","you'll","you'd",'your',
'yours','yourself','yourselves','he','him','his','himself','she',"she's",'her','hers','herself','it',
"it's",'its','itself','they','them','their','theirs','themselves','what','which','who','whom','this','that',"that'll",
'these','those','am','is','are','was','were','be','been','being','have','has','had','having','do','does','did',
'doing','a','an','the','and','but','if','or','because','as','until','while','of','at','by','for','with','about',
'against','between','into','through','during','before','after','above','below','to','from','up','down','in','out',
'on','off','over','under','again','further','then','once','here','there','when','where','why','all','any',
'both','each','few','more','most','other','some','such','only','own','same','so','than','too',
'very','s','t','can','will','just','should',"should've",'now','d','ll','m','o','re','ve','y','rt','rt','qt','for',
'the','with','in','of','and','its','it','this','i','have','has','would','could','you','a','an',
'be','am','can','edushopper','will','to','on','is','by','ive','im','your','we','are','at','as','any','ebay','thank','hello','know',
'need','want','look','hi','sorry','http','body','dear','hello','hi','thanks','sir','tomorrow','sent','send','see','there','welcome','what','well','us']
stop.update(set(stoplist))
def text_preprocess(text):
"""
text: a string
return: modified initial string
"""
text = re.sub(r'\d', '', str(text)) # removing digits
text = re.sub(r"(?:\@|https?\://)\S+", "", str(text)) #removing mentions and urls
text = text.lower() # lowercase text
text = re.sub('[0-9]+', '', text)
text = REPLACE_BY_SPACE_RE.sub(" ", text) # replace REPLACE_BY_SPACE_RE symbols by space in text
text = BAD_SYMBOLS_RE.sub(" ", text) # delete symbols which are in BAD_SYMBOLS_RE from text
text = ' '.join([word for word in text.split() if word not in stop]) # delete stopwors from text
text = text.strip()
return text
# In[6]:
data['content'] = data['content'].apply(text_preprocess)
print(data.head())
# In[7]:
data = data[data['engagement'] > 0]
print(data.shape)
data.head()
# In[9]:
# Now lets check the descriptive stats
data.describe()
# In[10]:
data['engagement_bucket'] = pd.qcut(data['engagement'], q=[0,0.5, 0.75, 1], labels=['Low', 'Medium', 'High'])
data.head()
# In[11]:
# sns.countplot(x='engagement_bucket', data=data)
# plt.show()
# In[12]:
# Creating time related features such as time, day, etc.
data['day'] = data['published'].dt.day
data['hour'] = data['published'].dt.hour
data['week_day'] = data['published'].dt.weekday
# In[13]:
data.head()
# In[14]:
# Which hour of the day, the posts are getting higher average engagement?
hour = data.groupby('hour')['engagement'].mean()
# hour.plot(figsize=(12,5))
# plt.xlabel("Hour of Day")
# plt.ylabel("Engagement")
# plt.show()
# In[15]:
# Which day of the week, the posts are getting higher average engagement? (Monday=0, Sunday=6)
weekday = data.groupby('week_day')['engagement'].mean()
# weekday.plot(figsize=(12,5))
# plt.xlabel("Day of the Week")
# plt.ylabel("Engagement")
# plt.show()
# In[16]:
# Which day of the month, the posts are getting higher average engagement? (Monday=0, Sunday=6)
dayofmonth = data.groupby('day')['engagement'].mean()
# dayofmonth.plot(figsize=(12,5))
# plt.xlabel("Day of the Month")
# plt.ylabel("Engagement")
# plt.show()
# In[17]:
# Creating Features and Labels
X = data[['word_count', 'hour', 'week_day']]
X = pd.get_dummies(X, drop_first=True)
X['content'] = data['content']
X.reset_index(drop=True,inplace=True)
y= data['engagement_bucket'].values
y = pd.get_dummies(y)
# In[18]:
X.head()
# In[19]:
y.head()
# In[20]:
# Splitting the data into Train and Validation set
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2, stratify=y, random_state=123)
print(X_train.shape)
print(X_valid.shape)
# In[21]:
# Extracting features from the text column
vec = TfidfVectorizer(strip_accents='unicode', ngram_range=(1,2), max_features=3000, smooth_idf=True, sublinear_tf=True)
train_vec = vec.fit_transform(X_train['content'])
valid_vec = vec.transform(X_valid['content'])
# In[22]:
train_vec.toarray().shape
# In[23]:
X_train = np.hstack([X_train.drop('content', axis=1), train_vec.toarray()])
X_valid = np.hstack([X_valid.drop('content', axis=1), valid_vec.toarray()])
# In[24]:
# After adding the vectorized text columns
print(X_train.shape)
print(X_valid.shape)
# In[25]:
# Fitting a baseline model
logreg = OneVsRestClassifier(LogisticRegression(solver='sag'))
clf = MultinomialNB()
cv_score = cross_val_score(logreg, X_train, y_train.values, cv=10, scoring='roc_auc')
print(cv_score.mean())
# In[26]:
logreg.fit(X_train,y_train)
pred = logreg.predict(X_valid)
# pred2 = clf.predict(X_valid)
# In[27]:
from sklearn.metrics import roc_auc_score, f1_score
roc_auc_score(y_valid, pred)
# In[28]:
f1_score(y_valid, pred, average='weighted')
# In[ ]:
import pickle
file1 = open("vectorizer.pkl", 'wb')
pickle.dump(vec, file1, pickle.HIGHEST_PROTOCOL)
file2 = open("logisticreg.pkl", 'wb')
pickle.dump(logreg, file2, pickle.HIGHEST_PROTOCOL)
file1.close()
file2.close()