/
analysis.py
319 lines (248 loc) · 9.5 KB
/
analysis.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
# linear algebra
import numpy as np
import pandas as pd
# text vectorizer
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer
# for nearest neighbor
from sklearn.neighbors import NearestNeighbors
# cosine similarity
from sklearn.metrics.pairwise import linear_kernel as cosine_similarity
# note that we can use linear kernel because
# vectors from text vectorizers are already normalized
def down(nparray):
return nparray.reshape(nparray.shape[0], 1)
def right(nparray):
return nparray.reshape(1, nparray.shape[0])
'''
Likelihood Matrix
- matrix quantifying the likelihood of a match
between row and column elements
- performs an initial content-based filtering upon construction
- applicable for collaborative updating
- nature of being a matrix allows chain multiplication
'''
class LikelihoodMatrix(object):
def __str__(self):
return str(self.dataframe)
def _repr_html_(self):
return self.dataframe.to_html()
'''
Constructor
- initializes a likelihood data frame based on row and column labels
- performs a content-based filtering technique using features
- vectorizer can be 'tfidf' or 'count'
- uses cosine similarity for label matching
'''
def __init__(self, rows, columns, vectorizer='tfidf', ngram_range=(3,4), all_zeroes=False, threshold=0.3):
# start vectorizing
if vectorizer == 'tfidf': vectorizer = TfidfVectorizer
elif vectorizer == 'count': vectorizer = CountVectorizer
else: raise ValueError("vectorizer should be 'tfidf' or 'count'")
vectorizer = vectorizer(analyzer='char', ngram_range=ngram_range)
# extract vectors
unique_labels = list(set(rows).union(set(columns)))
features = vectorizer.fit_transform(unique_labels).toarray()
# cache a mapping of label to corresponding vector
vector_cache = {label:vector for label, vector in zip(unique_labels, features)}
# get all pairwise cosine similarity
# this will be the initial content of the matrix
row_vectors = [vector_cache[label] for label in rows]
column_vectors = [vector_cache[label] for label in columns]
# create matrix
if all_zeroes:
matrix = np.zeroes(len(rows) * len(columns)).reshape(len(rows), len(columns))
else:
matrix = cosine_similarity(row_vectors, column_vectors)
# create pandas data frame object
self.dataframe = pd.DataFrame(matrix, index=rows, columns=columns)
# append important attributes
self.rows = rows
self.columns = columns
self.row_vectors = row_vectors
self.column_vectors = column_vectors
self.vectorizer = vectorizer
self.vector_cache = vector_cache
self.threshold = threshold
self.matrix = self.dataframe.values
# self.row_similarity_cache = {}
# self.column_similarity_cache = {}
'''
Find Matches
- a vector of quantified matches on opposite axis
- equivalent to LM * down(SV2) (on_rows)
- equivalent to right(SV1) * LM (not on_rows)
- one can return a dataframe for readability
'''
def find_matches(self, text, on_rows = False, with_labels = True, percentage = True):
similarities = self.similarity_vector(text, on_rows=not on_rows, with_labels=False, percentage=False)
if on_rows:
matrix = np.dot(self.matrix, down(similarities))
matches = matrix.reshape(matrix.shape[0])
else:
matches = np.dot(right(similarities), self.matrix)[0]
if percentage and matches.any():
matches *= 100. / matches.sum()
if with_labels:
return pd.Series(matches, index=self.rows if on_rows else self.columns)
else:
return matches
# convenience methods for match finding
def find_row_matches(self, text, with_labels = True, percentage = True):
return self.find_matches(text, on_rows=True, with_labels=with_labels, percentage=percentage)
def find_column_matches(self, text, with_labels = True, percentage = True):
return self.find_matches(text, on_rows=False, with_labels=with_labels, percentage=percentage)
'''
Add Match
- updates the matrix count by adding the delta
- uses frequency-based collaborative filtering
'''
def add_match(self, row_text, column_text):
# computing delta matrix is slow. Only compute for necessary labels
# self.dataframe += self.delta(row_text, column_text, with_labels=False)
SV1 = self.similarity_vector(row_text, on_rows=True, with_labels=False)
SV2 = self.similarity_vector(column_text, on_rows=False, with_labels=False)
cartesian_SV1 = filter(lambda item: item[1] >= self.threshold, enumerate(SV1))
cartesian_SV2 = filter(lambda item: item[1] >= self.threshold, enumerate(SV2))
matrix = self.matrix
for i, a in cartesian_SV1:
for j, b in cartesian_SV2:
matrix[i,j] += a * b
'''
Subtract Match
- updates the matrix count by subtracting the delta
- uses frequency-based collaborative filtering
'''
def subtract_match(self, row_text, column_text):
# computing delta matrix is slow. Only compute for necessary labels
# self.dataframe += self.delta(row_text, column_text, with_labels=False)
SV1 = self.similarity_vector(row_text, on_rows=True, with_labels=False)
SV2 = self.similarity_vector(column_text, on_rows=False, with_labels=False)
cartesian_SV1 = filter(lambda item: item[1] >= self.threshold, enumerate(SV1))
cartesian_SV2 = filter(lambda item: item[1] >= self.threshold, enumerate(SV2))
matrix = self.matrix
for i, a in cartesian_SV1:
for j, b in cartesian_SV2:
matrix[i,j] -= a * b
'''
Recommendation Score
- obtain a quantified score of two labels
- equivalent to right(SV1) * LM * down(SV2)
'''
def recommendation_score(self, row_text, column_text):
SV1 = self.similarity_vector(row_text, on_rows=True, with_labels=False, percentage=False)
SV2 = self.similarity_vector(column_text, on_rows=False, with_labels=False, percentage=False)
LM = self.matrix
return right(SV1).dot(LM).dot(SV2)[0]
'''
Features Vector
- extract relative features vector of a text
- vectorizes with respect to dataframe labels
'''
def features_vector(self, text):
try: # obtain from cache
return self.vector_cache[text]
except AttributeError:
self.vector_cache = {}
except KeyError:
pass
# not in cache. calculate first
vector = self.vectorizer.transform([text]).toarray()[0]
self.vector_cache[text] = vector
return vector
'''
Similarity Vector
- extract vector of cosine similarity on same axis
- one can return a series for readability
'''
def similarity_vector(self, text, on_rows = True, with_labels = True, percentage = False):
# function to process parameter options
def process(similarities):
if percentage and similarities.any():
similarities *= 100.0 / similarities.sum()
if with_labels:
return pd.Series(similarities, index=self.rows if on_rows else self.columns)
return similarities
try: # obtain from cache
cache = self.row_similarity_cache if on_rows else self.column_similarity_cache
return process(cache[text])
except AttributeError:
if on_rows: self.row_similarity_cache = {}
else: self.column_similarity_cache = {}
except KeyError:
pass
# not in cache. calculate first
text_features = self.features_vector(text)
cache = self.row_similarity_cache if on_rows else self.column_similarity_cache
X = [text_features]
Y = self.row_vectors if on_rows else self.column_vectors
cos_sim = cosine_similarity(X, Y)[0]
similarities = np.array(map(lambda x: x if x >= self.threshold else 0, cos_sim))
cache[text] = similarities
return process(similarities)
'''
Delta Matrix
- garners which row/column labels the input most likely is
- returns a matrix of most likely label match
- uses content-based matching of features
- note: the content of self.dataframe does not affect
the values returned by the matrix
- computes delta likelihood matrix by multiplying
the similarity vectors of two samples from both axes
- equivalent to down(SV1) * right(SV2)
'''
def delta(self, row_text, column_text, with_labels = True):
# get similarity vectors
# note: downward for rows, rightward for columns
SV1 = self.similarity_vector(row_text, on_rows=True, with_labels=False, percentage=False)
SV2 = self.similarity_vector(column_text, on_rows=False, with_labels=False, percentage=False)
#combine computations by multiplying
matrix = np.dot(down(SV1), right(SV2))
if with_labels:
return pd.DataFrame(matrix, index=self.rows, columns=self.columns)
else:
return matrix
'''
Combining of Matrices
- to derive combined likelihood, matrices are multiplied
- note that matrix multiplication rules apply
'''
def __mul__(self, other):
if isinstance(other, LikelihoodMatrix):
product = LikelihoodMatrix(self.rows, other.columns)
product.dataframe += self.dataframe.dot(other.dataframe)
return product
else:
raise TypeError('Can only with multiply likelihood_matrix')
'''
Copy
- returns a clone with a copy of attribute pointers
- copy with cache is optional
'''
def copy(self, copy_cache = False):
cls = self.__class__
other = cls.__new__(cls)
# copy important attribute pointers
other.dataframe = self.dataframe
other.rows = self.rows
other.columns = self.columns
other.row_vectors = self.row_vectors
other.column_vectors = self.column_vectors
other.vectorizer = self.vectorizer
other.threshold = self.threshold
other.matrix = self.matrix
if copy_cache:
names = [
'vector_cache',
'row_similarity_cache',
'column_similarity_cache'
]
for name in names:
try:
cache = getattr(self, name)
except AttributeError:
pass
else:
setattr(other, name, cache)
# for copy module
def __copy__(self):
return self.clone()