Sorting points from distance to a given point x,y here in my case (x=0,y=o)

老子叫甜甜 提交于 2019-12-02 10:48:38

I like to keep this handy to calculate distances in several array formats... it isn't a one-liner, but it works. Details on its implementations can be found elsewhere on stack by searching for 'einsum' and numpy as keywords. Import numpy as np required, this is just the def and you need 2 arrays

import numpy as np

def e_dist(a, b, metric='euclidean'):

"""Distance calculation for 1D, 2D and 3D points using einsum
: a, b   - list, tuple, array in 1,2 or 3D form
: metric - euclidean ('e','eu'...), sqeuclidean ('s','sq'...),
:-----------------------------------------------------------------------
"""
a = np.asarray(a)
b = np.atleast_2d(b)
a_dim = a.ndim
b_dim = b.ndim
if a_dim == 1:
    a = a.reshape(1, 1, a.shape[0])
if a_dim >= 2:
    a = a.reshape(np.prod(a.shape[:-1]), 1, a.shape[-1])
if b_dim > 2:
    b = b.reshape(np.prod(b.shape[:-1]), b.shape[-1])
diff = a - b
dist_arr = np.einsum('ijk,ijk->ij', diff, diff)
if metric[:1] == 'e':
    dist_arr = np.sqrt(dist_arr)
dist_arr = np.squeeze(dist_arr)
return dist_arr

Then you can sort the result if needed, for example

a = np.random.randint(0, 10, size=(10,2))

orig = np.array([0,0])

    e_dist(a, orig)

    array([  4.12,   9.9 ,   7.07,   6.08,   3.16,  10.63,   8.54,   7.28,   7.21,
             6.08])
    np.sort(e_dist(a, orig))

    array([  3.16,   4.12,   6.08,   6.08,   7.07,   7.21,   7.28,   8.54,   9.9 ,
            10.63])

ADDENDUM

I should have added that you can get the sorted values using argsort as exemplified below

np.argsort(e_dist(a, orig))
array([4, 0, 3, 9, 2, 8, 7, 6, 1, 5], dtype=int64)

idx = np.argsort(art.e_dist(a, orig))

closest = a[idx]
array([[3, 1],
       [1, 4],
       [1, 6],
       [6, 1],
       [5, 5],
       [4, 6],
       [2, 7],
       [8, 3],
       [7, 7],
       [7, 8]])
def distance_squared(x1,y1,x2,y2):
    return (x1-x2)**2 + (y1-y2)**2
target_point = 0,0
sorted(a,key=lambda point:distance_squared(target_point[0],target_point[1],*point[0]))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!