-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistances.py
65 lines (55 loc) · 2.01 KB
/
distances.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
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances, manhattan_distances, cosine_distances
def euclidean_distances(X, Y):
"""
Arguments:
X {np.ndarray} -- First matrix, containing M examples with K features each.
Y {np.ndarray} -- Second matrix, containing N examples with K features each.
Returns:
D {np.ndarray}: MxN matrix with Euclidean distances between rows of X and rows of Y.
"""
m = X.shape[0]
n = Y.shape[0]
euclid_dist = np.zeros((m,n))
for i in range(m):
for j in range(n):
rowx = X[i,:]
rowy = Y[j,:]
euclid_dist[i,j] = np.linalg.norm(rowx-rowy, ord = None)
return euclid_dist
def manhattan_distances(X, Y):
"""
Arguments:
X {np.ndarray} -- First matrix, containing M examples with K features each.
Y {np.ndarray} -- Second matrix, containing N examples with K features each.
Returns:
D {np.ndarray}: MxN matrix with Manhattan distances between rows of X and rows of Y.
"""
m = X.shape[0]
n = Y.shape[0]
manhat_dist = np.zeros((m,n))
for i in range(m):
for j in range(n):
rowx = X[i,:]
rowy = Y[j,:]
manhat_dist[i,j] = np.linalg.norm(rowx-rowy, ord = 1)
return manhat_dist
def cos_distances(X, Y):
"""
Arguments:
X {np.ndarray} -- First matrix, containing M examples with K features each.
Y {np.ndarray} -- Second matrix, containing N examples with K features each.
Returns:
D {np.ndarray}: MxN matrix with Cosine distances between rows of X and rows of Y.
"""
m = X.shape[0]
n = Y.shape[0]
cos_dist = np.zeros((m,n))
for i in range(m):
for j in range(n):
rowx = X[i,:]
rowy = Y[j,:]
numer = np.dot(np.transpose(rowx), rowy)
denom = (np.linalg.norm(rowx, ord = 2) * np.linalg.norm(rowy, ord = 2))
cos_dist[i,j] = 1 - float(numer/denom)
return cos_dist