forked from Bears-R-Us/arkouda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos_dist.py
executable file
·63 lines (49 loc) · 1.82 KB
/
cos_dist.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
#!/usr/bin/env python3
import arkouda as ak
import math
# dot product or two arkouda pdarrays
def ak_dot(u, v):
if isinstance(u, ak.pdarray) and isinstance(v, ak.pdarray):
return ak.sum(u*v)
else:
raise TypeError("u and v must be pdarrays")
# magnitude/L_2-norm of arkouda pdarray
def ak_mag2(u):
if isinstance(u, ak.pdarray):
return math.sqrt(ak_dot(u,u))
else:
raise TypeError("u must be a pdarray")
# cosine distance of two arkouda pdarrays
# should function similarly to scipy.spatial.distance.cosine
def ak_cos_dist(u, v):
if isinstance(u, ak.pdarray) and isinstance(v, ak.pdarray):
return (1.0 - ak_dot(u,v)/(ak_mag2(u)*ak_mag2(v)))
else:
raise TypeError("u and v must be pdarrays")
if __name__ == "__main__":
from scipy.spatial import distance
import numpy as np
import argparse, sys, gc, time
parser = argparse.ArgumentParser(description="Example of cosine distance/similarity in arkouda")
parser.add_argument('--server', default="localhost", help='server/Hostname of arkouda server')
parser.add_argument('--port', type=int, default=5555, help='Port of arkouda server')
args = parser.parse_args()
ak.v = False
ak.connect(server=args.server, port=args.port)
u1 = [1, 0, 0]
v1 = [0, 1, 0]
d1 = ak_cos_dist(ak.array(u1), ak.array(v1))
print("d1 = ", d1)
# d1 should be 1.0
assert (np.allclose(d1, distance.cosine(u1,v1)))
u2 = [100, 0, 0]
d2 = ak_cos_dist(ak.array(u2), ak.array(v1))
print("d2 = ", d2)
# d2 should be 1.0
assert (np.allclose(d2, distance.cosine(u2,v1)))
u3 = [1, 1, 0]
d3 = ak_cos_dist(ak.array(u3), ak.array(v1))
print("d3 = ", d3)
# d3 should be 0.29289321881345254
assert (np.allclose(d3, distance.cosine(u3,v1)))
ak.disconnect()