-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweets_classification.py
402 lines (277 loc) · 12.4 KB
/
tweets_classification.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
from sklearn.pipeline import Pipeline
from sklearn import svm
from sklearn.model_selection import GridSearchCV,ShuffleSplit
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.base import BaseEstimator, ClassifierMixin, ClusterMixin
import copy
UNRELATED = "Unrelated"
NEG = "Neg"
NEG_LABEL = "Negative"
LABEL = "label"
TWEET = "tweet"
NORMALIZED_LABEL = "normalized label"
C = "C"
GAMMA = "gamma"
def process_tweets(tweets,labels,pipeline_steps):
"""
Create dictionaries containing data for each level of classification
:params:
tweets (list) : list of list of tokens
labels (list) : list of strings
:returns:
_all (dict) : all tweets (labels : `Unrelated`,'Related')
related (dict) : positive,negative,neutral tweets (labels : `Positive`,`Negative`,`Neutral`)
negative (dict) : negative tweets (labels : `NegOthers`,`NegResistant`...)
Dictionary keys are `tweet`,`label`,`normalized_label` (by level)
"""
# use this function to process list of (id, tweet) tuples
UNRELATED = "Unrelated"
NEG = "Neg"
NEG_LABEL = "Negative"
_all = {TWEET: [], LABEL: [], NORMALIZED_LABEL: []}
related = {TWEET: [], LABEL: [], NORMALIZED_LABEL: []}
negative = {TWEET: [], LABEL: [], NORMALIZED_LABEL: []}
for tweet,label in zip(tweets,labels):
# add tweet to all dict
_all[TWEET].append(tweet)
_all[LABEL].append(label)
_all[NORMALIZED_LABEL].append(label if label == UNRELATED else "Related")
# add tweet to related if applicable
if label != UNRELATED:
related[TWEET].append(tweet)
related[LABEL].append(label)
related[NORMALIZED_LABEL].append(label if NEG not in label else NEG_LABEL)
# add tweet to negative if applicable
if NEG in label:
negative[TWEET].append(tweet)
negative[LABEL].append(label)
negative[NORMALIZED_LABEL].append(label)
_all[TWEET] = pipeline_steps.fit_transform(_all[TWEET])
related[TWEET] = pipeline_steps.fit_transform(related[TWEET])
negative[TWEET] = pipeline_steps. fit_transform(negative[TWEET])
return _all, related, negative
class HierarchicalClassifier(BaseEstimator, ClassifierMixin):
"""
Classifier for hierarchical classification task. Perform by level classification with 3 different classifier.
Use only within this contest (tweet classification with 3 levels)
"""
def __init__(self,clfs,params):
"""
Construct new classifier object.
:params:
clfs (list) : list of sklearn classifiers (initialized). E.g. [LogisticRegression(),SVC(),LinearSVC()]
params (list) : list of parameters of each classifier. Each list in `params` should contain dictionaries as <param_name> : [<values>]. See
`optimize_hp` for more detail.
"""
self.clfs = clfs
self.params = params
def get_by_level_data(self,X,y,level):
"""
For each level of classfication retrieve correspondent feature matrix and create new set of labels
:params:
X (scipy.sparse.csr) : original feature matrix
y (list) : original labels
level (int) : hierarchy (0,1,2)
:return:
X_new (scipy.sparse.csr) : by level feature matrix
y_new (list) : by level labels (new) names
For instance with `level = 1` only the samples with label Positive,Neutral,Neg.* are retrieved. Then the labels `Ǹeg.*`
are replaced with `Negative`
"""
if level == 0:
y_new = [label if label == "Unrelated" else "Related" for label in y]
X_new = X
elif level == 1:
y_new = []
idx_X_new = []
for idx,label in enumerate(y):
if label != UNRELATED:
y_new.append(label if NEG not in label else NEG_LABEL)
idx_X_new.append(idx)
X_new = X[idx_X_new]
elif level == 2:
y_new = []
idx_X_new = []
for idx,label in enumerate(y):
if NEG in label:
y_new.append(label)
idx_X_new.append(idx)
X_new = X[idx_X_new]
return X_new,y_new
def optimize_classifiers(self,X,y):
"""
Optimize classifiers hyperparameters. See `optimize_hp`.
:params:
X (scipy.sparse.csr) : original feature matrix
y (list) : original labels
Replace classifiers in `self.clfs` with optimized ones
"""
best_f1s = []
for idx,clf in enumerate(self.clfs):
X_new,y_new = self.get_by_level_data(X = X,y = y,level = idx)
best_f1, self.clfs[idx] = optimize_hp(clf = self.clfs[idx],X = X_new,y = y_new, params = self.params[idx])
best_f1s.append(best_f1)
return best_f1s
def fit(self,X,y):
"""
Fit data with 3 different classifiers
"""
for idx,clf in enumerate(self.clfs):
X_new,y_new = self.get_by_level_data(X = X,y = y,level = idx)
clf.fit(X_new,y_new)
return self
def predict(self, X):
"""
Make by level predictions
"""
predictions = []
for i in range(X.shape[0]):
tweet = X[i,:]
# predict with all to get related or unrelated
pred = self.clfs[0].predict(tweet)[0] # the zero is because the classifier return a list with 1 element
if pred == UNRELATED:
# if its unrelated we are done with this tweet
predictions.append(pred)
continue
# if its related try to get next label
pred = self.clfs[1].predict(tweet)[0]
if NEG not in pred:
# if its not negative we are done with this tweet
predictions.append(pred)
continue
# if its negative try to get specific label
pred = self.clfs[2].predict(tweet)[0]
predictions.append(pred)
return predictions
def score(self, tweets, labels):
preds = self.predict(tweets)
return accuracy_score(labels, preds)
class BaseClf(BaseEstimator, ClusterMixin):
def __init__(self, pipeline_steps=None):
self.pipeline_steps = pipeline_steps
def fit(self, tweets, labels):
#HAVE TO DEEPCOPY HERE. This is because when building the pipeline we already give made objects to it.
# So, when passing the pipeline to multiple classifiers, the objects inside the pipeline are NOT copied
# This means that if in any other place, a pipeline is created and then fitted(like in the hierarchical CLF) it will override the objects
# in all other pipelines using this "pipeline_steps" list
self._pipeline_steps = copy.deepcopy(self.pipeline_steps)
self._vectorizer = Pipeline(self._pipeline_steps)
X = self._vectorizer.fit_transform(tweets)
print("Shape of the feature matrix to be fitted : {}".format(X.shape))
self._clf = self.get_clf()
self._clf.fit(X, labels)
def predict(self, tweets):
X = self._vectorizer.transform(tweets)
return self._clf.predict(X)
def score(self, tweets, labels):
X = self._vectorizer.transform(tweets)
return self._clf.score(X, labels)
def get_clf(self):
raise ValueError("get_clf function must be implemented!")
class TweetClassifierKNN(BaseClf):
def __init__(self, pipeline_steps=None, neighbors=10):
super(TweetClassifierKNN, self).__init__(pipeline_steps)
self.neighbors=neighbors
def get_clf(self):
return KNeighborsClassifier(self.neighbors)
class TweetClassifierLR(BaseClf):
def __init__(self, pipeline_steps=None, C=1.0, tol=1e-4):
super(TweetClassifierLR, self).__init__(pipeline_steps)
self.C = C
self.tol=tol
def get_clf(self):
return LogisticRegression(C=self.C, tol=self.tol)
class TweetClassifierRF(BaseClf):
#def __init__(self, pipeline_steps=None):
# super(TweetClassifierLR, self).__init__(pipeline_steps)
def get_clf(self):
return RandomForestClassifier(n_estimators=50)
class TweetClassifierBaseSVM(BaseClf):
def __init__(self, pipeline_steps=None, C=256, GAMMA=0.00002):
super(TweetClassifierBaseSVM, self).__init__(pipeline_steps)
self.C = C
self.GAMMA = GAMMA
def get_clf(self):
return svm.SVC(C=self.C, gamma=self.GAMMA)
class TweetClassifierH(BaseEstimator, ClassifierMixin):
def __init__(self, get_clf=None, kwargs=None):
"""
params:
get_clf: a function that return some classifier
kwargs: a dict contiainung the kwargs of the 3 differente classifier versions
{1: kwargs for first classifier, 2: kwargs for second classifier,, 3: kwargs for third classifier}
"""
self.get_clf = get_clf
self.kwargs = kwargs
def fit(self, pos_tweet, labels):
if self.get_clf is None:
raise ValueError("A classifier is needed")
tweet_list = zip(labels, pos_tweet)
all, related, negative = process_tweets(tweet_list)
self._all_clasifier = self.get_clf(1)(**self.kwargs[1])
self._related_clasifier = self.get_clf(2)(**self.kwargs[2])
self._negative_clasifier = self.get_clf(3)(**self.kwargs[3])
print("fittin data...")
print("fittin all...")
self._all_clasifier.fit(all[TWEET], all[NORMALIZED_LABEL])
print("fitting related...")
self._related_clasifier.fit(related[TWEET], related[NORMALIZED_LABEL])
print("fitting negative...")
self._negative_clasifier.fit(negative[TWEET], negative[NORMALIZED_LABEL])
print("finished fitting data!")
return self
def predict(self, tweets):
print("predicting...")
predictions = []
for tweet in tweets:
# predict with all to get related or unrelated
pred = self._all_clasifier.predict([tweet])[0] # the zero is because the classifier return a list with 1 element
if pred == UNRELATED:
# if its unrelated we are done with this tweet
predictions.append(pred)
continue
# if its related try to get next label
pred = self._related_clasifier.predict([tweet])[0]
if NEG not in pred:
# if its not negative we are done with this tweet
predictions.append(pred)
continue
# if its negative try to get specific label
pred = self._negative_clasifier.predict([tweet])[0]
predictions.append(pred)
return predictions
def score(self, tweets, labels):
preds = self.predict(tweets)
return accuracy_score(labels, preds)
def optimize_hp(clf,X,y,params):
"""
Optimize one classifier parameter at time. For each parameter:
- performs grid search (with train-test split for avoid training too many models), find best parameter value w.r.t. magro averaged f1 score
- instantiate new classifier with the found best parameter
- repeat
:params:
clf (sklearn classifier) : classifier to be optimized
X (scipy.sparse.csr_matrix or np.ndarray) : feature matrix
y ( np.ndarray) : labels vector
params (list) : list of dictionaries containing as key a parameter name and as value a list of possible parameters values. E.g.
params = [{'C' : [1,10,100]},{'gamma' : [2e-3,2e-4,2e-5]}]
random_state (int) : random seed for repruducibility
:returns:
clf (sklearn classifier) : optimized classifier
"""
best_f1 = 0
# rs = ShuffleSplit(n_splits = 1, test_size = 0.33) # reproduce baseline
for param in params:
# gs = GridSearchCV(clf,param,refit = False,cv = rs,scoring = 'f1_micro') # reproduce baseline
gs = GridSearchCV(clf,param,refit = False,scoring = 'f1_macro') # 3 fold CV
gs.fit(X,y)
searched_param = list(param.keys())[0]
best_value = gs.best_params_[searched_param]
print("Best value for {} : {}".format(searched_param,best_value))
best_f1 = gs.best_score_
print("Best F1 macro {}".format(best_f1))
clf.set_params(**{searched_param : best_value})
return best_f1,clf