-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.py
More file actions
29 lines (26 loc) · 763 Bytes
/
distance.py
File metadata and controls
29 lines (26 loc) · 763 Bytes
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
import numpy as np
def distance(f1,f2):
diff = f1 - f2
return diff.T.dot(diff)
def getDistanceMatrix(X):
mm = X.shape[0]
nn = X.shape[1]
distanceMatrix = np.nan * np.eye(mm)
for i in range(0,mm):
for j in range(0,mm):
if (i==j):
continue
f1 = X[i,:]
f2 = X[j,:]
distanceMatrix[i,j] = distance(f1,f2)
return distanceMatrix
def getDistanceMatrixTrainTest(train, test):
mtrain = train.shape[0]
mtest = test.shape[0]
distanceMatrix = np.zeros((mtest,mtrain))
for i in range(0,mtest):
for j in range(0,mtrain):
f1 = train[j,:]
f2 = test[i,:]
distanceMatrix[i,j] = distance(f1,f2)
return distanceMatrix