-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvm_classifier_utlilities.py
304 lines (250 loc) · 11.1 KB
/
svm_classifier_utlilities.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
from collections import Counter
import numpy as np
import os
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.base import TransformerMixin, clone
from sklearn.linear_model.base import LinearClassifierMixin
from sklearn.multiclass import OneVsRestClassifier
from sklearn.base import BaseEstimator
from sklearn.externals import joblib
from sklearn.pipeline import Pipeline
from typing import Dict, List, Any, Union
from copy import deepcopy
from numpy.random import RandomState
def oversample_data(X, y, verbose=False, seed=23):
"""
:param X: features
:param y: labels
:return: new balanced dataset
with oversampled minor class
"""
random_state = RandomState(seed=seed)
y_new = deepcopy(y)
X_new = deepcopy(X)
if verbose:
print('Oversampling...')
c = Counter(y)
labels = list(set(y))
major_label = max(labels, key=lambda x: c[x])
if verbose:
print("major: {}".format(major_label))
assert major_label == c.most_common()[0][0]
sampled_data = []
for label, count in c.most_common()[1:]:
offset = c[major_label] - count
y_new = np.hstack((y_new, [label] * offset))
tmp = X[np.array(y) == label]
sampled = random_state.choice(np.arange(len(tmp)), size=offset)
sampled_data.extend(tmp[sampled])
if verbose:
print("offset: {} for label: {}".format(offset, label))
X_new = np.concatenate((X_new, np.array(sampled_data)))
assert len(X_new) == len(c.keys()) * c[major_label]
assert len(X_new) == len(y_new)
return X_new, y_new
class FeatureExtractor(TransformerMixin):
def __init__(self, use_chars=False, stop_words=None):
self._been_fitten = False
self.stop_words = stop_words
self.use_chars = use_chars
# taking into account pairs of words
self.words_vectorizer = TfidfVectorizer(ngram_range=(1, 2), stop_words=self.stop_words)
# TODO: it breaks, why?
# self.words_vectorizer = TfidfVectorizer(ngram_range=(1, 2, 3), stop_words=self.stop_words)
if self.use_chars:
self.chars_vectorizer = TfidfVectorizer(analyzer='char_wb',
ngram_range=(2, 4)) # taking into account
# only n-grams into word boundaries
else:
self.chars_vectorizer = None
def fit(self, raw_docs, y=None):
"""
:param raw_docs: iterable with strings
:return: None
"""
self.words_vectorizer.fit(raw_docs)
if self.use_chars:
self.chars_vectorizer.fit(raw_docs)
def fit_transform(self, raw_docs, y=None):
"""
:param raw_docs: iterable with strings
:return: matrix of features
"""
self._been_fitten = True
if (not isinstance(raw_docs, list)) or (not isinstance(raw_docs[0], str)):
raise Exception("raw_docs expected to be list of strings")
mtx_words = self.words_vectorizer.fit_transform(raw_docs).toarray() # escape sparse representation
if self.use_chars:
mtx_chars = self.chars_vectorizer.fit_transform(raw_docs).toarray()
return np.hstack((mtx_words, mtx_chars))
else:
return mtx_words
def transform(self, raw_docs):
"""
:param raw_docs: str or iterable with str elements
:return: matrix with shape: [num_elements, num_features]
"""
if not self._been_fitten:
raise Exception("It is necessary to fit before transform")
# case if one sample
if isinstance(raw_docs, str):
raw_docs = [raw_docs]
mtx_words = self.words_vectorizer.transform(raw_docs).toarray() # escape sparse representation
if self.use_chars:
mtx_chars = self.chars_vectorizer.transform(raw_docs).toarray()
return np.hstack((mtx_words, mtx_chars))
else:
return mtx_words
class StickSentence(TransformerMixin):
@staticmethod
def _preproc(data):
if not isinstance(data[0], list):
data = [data]
if isinstance(data[0][0], dict):
return [" ".join([w['normal'] for w in sent]) for sent in data]
else:
return [" ".join(sent) for sent in data]
def fit_transform(self, data, y=None):
return self._preproc(data)
def transform(self, data, y=None):
return self._preproc(data)
class Embedder(TransformerMixin):
def __init__(self, fasttext, stop_words=()):
self.fasttext = fasttext
self.words_vectorizer = TfidfVectorizer(ngram_range=(1, 1), stop_words=stop_words)
self.default_k = 1.0
def _normalize(self, unnormalized: List[Dict]):
return [w['normal'] for w in unnormalized]
def fit(self, data: Union[List[List[Dict]], List[Dict]], y=None):
if not isinstance(data[0], list):
data = [data]
self.words_vectorizer.fit([' '.join(self._normalize(d)) for d in data], y)
return self
def transform(self, data, y=None):
if not isinstance(data[0], list):
data = [data]
res = []
for row in data:
vecs = []
word2id = self.words_vectorizer.vocabulary_
id2idf = self.words_vectorizer.idf_
for x, nw in zip(row, self._normalize(row)):
try:
v = self.fasttext[x['_text']]
except KeyError:
# print('FastText can''t find vector for "{}"'.format(x['_text']))
v = np.zeros(self.fasttext.vector_size)
wid = word2id.get(nw, None)
if wid is None:
k = self.default_k
else:
k = id2idf[wid]
vecs.append(k * v)
res.append(np.array(vecs).sum(axis=0))
return np.vstack(res)
class TextClassifier:
def predict_single(self, text: List[Dict[str, Any]]):
pass
class SentenceClassifier(TextClassifier):
def __init__(self, base_clf: Union[BaseEstimator, None], stop_words=None, use_chars=False, labels_list=None, model_path=None, model_name=None):
"""
:param stop_words: list of words to exclude from feature matrix
:param use_chars: default False
:param labels_list: list of possible targets; optional
:param model_path: path to load model from
"""
self.model_name = model_name
self.base_clf = base_clf
assert (base_clf is None) or isinstance(self.base_clf, BaseEstimator), "Wrong classifier type"
self.use_chars = use_chars
self.stop_words = stop_words
if labels_list is not None:
self.labels_list = sorted(list(set(labels_list)))
self.string2idx = {s: i for i, s in enumerate(self.labels_list)}
self.idx2string = {v: k for k, v in self.string2idx.items()}
else:
self.labels_list = None
self.model = None
self.feat_generator = None
if model_path and os.path.isfile(model_path):
self.load_model(model_path)
def train_model(self, X: List[List[Dict[str, Any]]], y: List):
self.feat_generator = FeatureExtractor(use_chars=self.use_chars, stop_words=self.stop_words)
self.clf = clone(self.base_clf)
self.model = Pipeline([('sticker_sent', StickSentence()),
('feature_extractor', self.feat_generator),
('classifier', self.clf)])
# from gensim.models.wrappers import FastText
# self.model = Pipeline([('vector_summer', Embedder(FastText.load('fasttext.sber.bin'))),
# ('classifier', self.clf)])
if None in y:
y = [i if i is not None else '_' for i in y]
if self.labels_list is not None:
y_idx = [self.string2idx[label] for label in y]
else:
self.labels_list = sorted(list(set(y)))
self.string2idx = {s: i for i, s in enumerate(self.labels_list)}
self.idx2string = {v: k for k, v in self.string2idx.items()}
if '_' in self.string2idx:
self.idx2string[self.string2idx['_']] = None
self.string2idx[None] = self.string2idx['_']
y_idx = [self.string2idx[label] for label in y]
self.model.fit(X, y_idx)
def predict_single(self, text: List[Dict[str, Any]]):
assert self.model, 'No model specified!'
label = self.model.predict(text)[0]
return self.idx2string[label]
def predict_batch(self, list_texts: List[List[Dict[str, Any]]]):
assert self.model, 'No model specified!'
labels = self.model.predict(list_texts)
return labels
def dump_model(self, model_path):
dump_dict = {'model': self.model,
'string2idx': self.string2idx,
'stop_words': self.stop_words,
'use_chars': self.use_chars}
joblib.dump(dump_dict, model_path)
def load_model(self, model_path):
if not os.path.exists(model_path):
raise Exception("Model path: '{}' doesnt exist".format(model_path))
dump_dict = joblib.load(model_path)
self.model = dump_dict['model']
self.clf = self.model.steps[2][1]
self.string2idx = dump_dict['string2idx']
self.stop_words = dump_dict['stop_words']
self.use_chars = dump_dict['use_chars']
self.labels_list = sorted(list(set(self.string2idx.keys())-{None}))
self.idx2string = {v: k for k, v in self.string2idx.items()}
if '_' in self.labels_list:
self.idx2string[self.string2idx['_']] = None
self.string2idx[None] = self.string2idx['_']
def get_feature_importance(self):
if isinstance(self.clf, OneVsRestClassifier):
names = self.model.named_steps['feature_extractor'].words_vectorizer.get_feature_names()
result = []
for est in self.clf.estimators_:
weights = sorted(list(zip(names, est.coef_)), key=lambda x: x[1], reverse=True)
result.append(weights)
return result
elif isinstance(self.clf, LinearClassifierMixin):
coefs = self.clf.coef_
names = self.model.named_steps['feature_extractor'].words_vectorizer.get_feature_names()
results = []
for line in coefs:
weights = sorted(list(zip(names, line)), key=lambda x: x[1], reverse=True)
results.append(weights)
return results
else:
return None
def get_description(self):
descr = str(type(self.clf))
params = sorted(['{}: {}'.format(repr(k), repr(v)) for k, v in self.clf.get_params().items()])
params = '{{{}}}'.format(', '.join(params))
result = "{}\n{}\nstop_words: {}\nuse_chars: {}".format(descr, params, self.stop_words, self.use_chars)
return result
def get_labels(self):
return self.labels_list
def encode2idx(self, labels):
return [self.string2idx[w] for w in labels]
def encode2string(self, indexes):
return [self.idx2string[i] for i in indexes]