-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
59 lines (48 loc) · 1.59 KB
/
util.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
import numpy as np
from itertools import combinations, permutations
def spearman_footrule(x, y):
num_items = len(x)
delta = np.arange(num_items)
pi = np.arange(num_items)
for i in range(len(x)):
delta[x[i]] = i
pi[y[i]] = i
return np.linalg.norm((delta - pi), ord=1)
def spearman_rank_correlation(x, y):
num_items = len(x)
delta = np.arange(num_items)
pi = np.arange(num_items)
for i in range(len(x)):
delta[x[i]] = i
pi[y[i]] = i
return np.linalg.norm((delta - pi) ** 2, ord=1)
def kendall_tau(x, y):
tau = 0
n_candidates = len(x)
for i, j in combinations(range(n_candidates), 2):
tau += (np.sign(x[i] - x[j]) ==
-np.sign(y[i] - y[j]))
return float(tau)
def spearman_footrule_matrix(X):
return np.array([[spearman_footrule(a, b) for a in X] for b in X])
def spearman_rank_correlation_matrix(X):
return np.array([[spearman_rank_correlation(a, b) for a in X] for b in X])
def kendalltau_matrix(X):
return np.array([[kendall_tau(a, b) for a in X] for b in X])
def read_sushi_votes(same=False, fn='sushi3-2016/sushi3a.5000.10.order'):
votes = []
with open(fn) as f:
lines = f.readlines()
for line in lines[1:]:
votes.append(np.array(map(int, line.rstrip('\n').split(' ')[2:])))
r_ids = []
same = []
with open('sushi3-2016/sushi3.udata') as f:
lines = f.readlines()
for line in lines:
r_ids.append(map(int, line.split())[8])
same.append(map(int, line.split())[10])
if same:
votes = [votes[i] for i in range(len(votes)) if same[i]]
r_ids = [r_ids[i] for i in range(len(r_ids)) if same[i]]
return np.array(votes), np.array(r_ids)