-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeanEmbedding.py
More file actions
85 lines (69 loc) · 2.85 KB
/
MeanEmbedding.py
File metadata and controls
85 lines (69 loc) · 2.85 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
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from collections import defaultdict
class MinMaxMeanEmbeddingVectorizer(object):
def __init__(self, word2vec):
self.word2vec = word2vec
# if a text is empty we should return a vector of zeros
# with the same dimensionality as all the other vectors
# self.dim = len(word2vec.itervalues().next())
self.dim = len(word2vec[next(iter(word2vec))])
def fit(self, X, y):
return self
def transform(self, X):
X_mean = np.array([
np.mean([self.word2vec[w] for w in list(words) if w in self.word2vec]
or [np.zeros(self.dim)], axis=0)
for words in X
])
X_min = np.array([
np.amin([self.word2vec[w] for w in list(words) if w in self.word2vec]
or [np.zeros(self.dim)], axis=0)
for words in X
])
X_max = np.array([
np.amax([self.word2vec[w] for w in list(words) if w in self.word2vec]
or [np.zeros(self.dim)], axis=0)
for words in X
])
return np.concatenate((X_min,X_max,X_mean), axis = 1)
#http://nadbordrozd.github.io/blog/2016/05/20/text-classification-with-word2vec/
class MeanEmbeddingVectorizer(object):
def __init__(self, word2vec):
self.word2vec = word2vec
# if a text is empty we should return a vector of zeros
# with the same dimensionality as all the other vectors
# self.dim = len(word2vec.itervalues().next())
self.dim = len(word2vec[next(iter(word2vec))])
def fit(self, X, y):
return self
def transform(self, X):
return np.array([
np.mean([self.word2vec[w] for w in list(words) if w in self.word2vec]
or [np.zeros(self.dim)], axis=0)
for words in X
])
class TfidfEmbeddingVectorizer(object):
def __init__(self, word2vec):
self.word2vec = word2vec
self.word2weight = None
# self.dim = len(word2vec.itervalues().next())
self.dim = len(word2vec[next(iter(word2vec))])
def fit(self, X):
tfidf = TfidfVectorizer(analyzer=lambda x: x)
tfidf.fit(X)
# if a word was never seen - it must be at least as infrequent
# as any of the known words - so the default idf is the max of
# known idf's
max_idf = max(tfidf.idf_)
self.word2weight = defaultdict(
lambda: max_idf,
[(w, tfidf.idf_[i]) for w, i in tfidf.vocabulary_.items()])
return self
def transform(self, X):
return np.array([
np.mean([np.array(self.word2vec[w]) * self.word2weight[w]
for w in words if w in self.word2vec] or
[np.zeros(self.dim)], axis=0)
for words in X
])