diff options
author | Alex Auvolat <alex.auvolat@ens.fr> | 2015-04-27 13:08:56 -0400 |
---|---|---|
committer | Alex Auvolat <alex.auvolat@ens.fr> | 2015-04-27 13:08:56 -0400 |
commit | a25d4fb6e92f203183de2d89e8c467a6b14e1730 (patch) | |
tree | 6d448760e647572d52242f5726224cdf20e832ee /hdist.py | |
parent | ccd1245db7f6799ab4e1f45a8cead85ed67f1c72 (diff) | |
download | taxi-a25d4fb6e92f203183de2d89e8c467a6b14e1730.tar.gz taxi-a25d4fb6e92f203183de2d89e8c467a6b14e1730.zip |
Implement HDist, transformer that selects k at a random position.
Diffstat (limited to 'hdist.py')
-rw-r--r-- | hdist.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/hdist.py b/hdist.py new file mode 100644 index 0000000..4944cb8 --- /dev/null +++ b/hdist.py @@ -0,0 +1,25 @@ +from theano import tensor +import numpy + + +def hdist(a, b): + rearth = numpy.float32(6371) + deg2rad = numpy.float32(3.14159265358979 / 180) + + lat1 = a[:, 0] * deg2rad + lon1 = a[:, 1] * deg2rad + lat2 = b[:, 0] * deg2rad + lon2 = b[:, 1] * deg2rad + + dlat = abs(lat1-lat2) + dlon = abs(lon1-lon2) + + al = tensor.sin(dlat/2)**2 + tensor.cos(lat1) * tensor.cos(lat2) * (tensor.sin(dlon/2)**2) + d = tensor.arctan2(tensor.sqrt(al), tensor.sqrt(numpy.float32(1)-al)) + + hd = 2 * rearth * d + + return tensor.switch(tensor.eq(hd, float('nan')), (a-b).norm(2, axis=1), hd) + + + |