-
Notifications
You must be signed in to change notification settings - Fork 2
/
statistics.py
173 lines (146 loc) · 4.74 KB
/
statistics.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
"""
includes functions (metrics) required to evaluate Locality Sensitive Hashing.
"""
import numpy as np
def jaccard(x, a, signature_matrix):
"""This function finds jaccard similarity between two documents
Parameters
----------
x: int
1-d signature array of document with docid=x
a: int
1-d signature array of document with docid=a
signature_matrix: pandas DataFrame
contains signature vectors of all documents as columns
Returns
-------
int
jaccard similarity between documents x and a
"""
x = signature_matrix[x]
a = signature_matrix[a]
return sum(x & a)/sum(x | a)
def euclid(x, a, signature_matrix):
"""This function finds euclidean similarity between two documents
Parameters
----------
x: int
1-d signature array of document with docid=x
a: int
1-d signature array of document with docid=a
signature_matrix: pandas DataFrame
contains signature vectors of all documents as columns
Returns
-------
int
euclidean distance between documents x and a
"""
x = signature_matrix[x]
a = signature_matrix[a]
return np.sum(a**2 - x**2)**0.5
def cosine(x, a, signature_matrix):
"""This function finds cosine similarity between two documents
Parameters
----------
x: int
1-d signature array of document with docid=x
a: int
1-d signature array of document with docid=a
signature_matrix: pandas DataFrame
contains signature vectors of all documents as columns
Returns
-------
int
cosine similarity between documents x and a
"""
x = signature_matrix[x]
a = signature_matrix[a]
return np.dot(a,x)/(np.sum(a**2) * np.sum(x**2))**0.5
def compute_similarity(x, similar_docs, signature_matrix, sim_type="jaccard"):
"""This function finds cosine similarity between two documents
Parameters
----------
x: int
1-d signature array of document with docid=x
similar_docs: list
a list of docids which are similar to x.
signature_matrix: pandas DataFrame
contains signature vectors of all documents as columns
sim_type: string
can take values jaccard, euclid, cosine.
Returns
-------
list
sorted list of (docid, score) tuples.
"""
if sim_type == "jaccard": sim_fun = jaccard
elif sim_type == "euclid": sim_fun = euclid
elif sim_type == "cosine": sim_fun = cosine
# write for all other funcs
ranked_list = []
for i in similar_docs:
if i == x: continue
score = sim_fun(x, i, signature_matrix)
ranked_list.append((i, score))
if sim_type == "euclid":
return sorted(ranked_list, key=lambda x: x[1], reverse=False)
else:
return sorted(ranked_list, key=lambda x: x[1], reverse=True)
def precision(threshold, output):
"""This function finds cosine similarity between two documents
Parameters
----------
threshold: float
value of similarity above which retrieved docs are considered relevant
output: list
a list of retrieved items.
Returns
-------
float
precision value for the given set of retrieved items.
"""
req = [ i for f, i in output if i>=threshold ]
return len(req)/len(output)
def recall(threshold, x, size, output, signature_matrix, sim_type):
"""This function finds cosine similarity between two documents
Parameters
----------
threshold: float
value of similarity above which retrieved docs are considered relevant
x: int
1-d signature array of document with docid=x
size: int
number of all documents in the corpus
output: list
a list of retrieved items.
signature_matrix: pandas DataFrame
contains signature vectors of all documents as columns
sim_type: string
can take values jaccard, euclid, cosine.
Returns
-------
float
recall value for the given set of retrieved items.
"""
docs = compute_similarity(x, [ i for i in range(size) ], signature_matrix, sim_type)
req = [ i for f, i in output if i>=threshold ]
den = [ i for f, i in docs if i>=threshold and f!=x ]
if len(den) == 0:
return "not defined"
return len(req)/len(den)
def get_file_name(file_id, files):
"""This function finds cosine similarity between two documents
Parameters
----------
threshold: float
value of similarity above which retrieved docs are considered relevant
files: list
a list of tuples containing filename and file id.
Returns
-------
string
name of the file with given file_id.
"""
for filename, f_id in files:
if file_id == f_id:
return filename