-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLpnorm.py
More file actions
69 lines (51 loc) · 1.21 KB
/
Lpnorm.py
File metadata and controls
69 lines (51 loc) · 1.21 KB
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
import numpy as np
import matplotlib.pyplot as plt
# data object
np.random.seed(123)
X = np.random.randint(50,100,size=500)
Y = np.random.randint(50,100,size=500)
# dimension
P = np.arange(1,501)
# list
L1s = []
L2s = []
Linfs = []
for p in P:
# set features
X_dim = X[:p]
Y_dim = Y[:p]
# compute distance
L1 = sum(abs(X_dim-Y_dim))
L2 = np.sqrt(sum(np.power((X_dim-Y_dim),2)))
Linf = np.max(abs(X_dim-Y_dim))
# save result
L1s.append(L1)
L2s.append(L2)
Linfs.append(Linf)
# visualization
plt.plot(P,L1s,'-',label='Manhattan')
plt.plot(P,L2s,'-',label='Euclidean')
plt.plot(P,Linfs,'-',label='Chebyshev')
plt.ylabel(r'$Dist (X,Y)$')
plt.xlabel(r'$p:Dimension$')
plt.legend()
plt.show()
#%%
import numpy as np
def cos_sim(X,Y):
return np.dot(X,Y)/(np.linalg.norm(X)*np.linalg.norm(Y))
X = np.array([2,5])
Y = np.array([4,3])
cos_sim(X,Y)
#%%
def jaccard_sim(A,B):
return len(np.intersect1d(A,B))/len(np.union1d(A,B))
A = ['cat','dog','lion','bird']
B = ['lion','bird','rabbit','rat','tiger']
jaccard_sim(A,B)
#%%
def dice_sim(A,B):
return 2*len(np.intersect1d(A,B))/(len(A)+len(B))
A = ['cat','dog','lion','bird']
B = ['lion','bird','rabbit','rat','tiger']
dice_sim(A,B)