两个不同的Numpy数组中的点之间的最小欧几里得距离,不在

发布于 2021-01-29 19:35:22

我有两个 x - y 坐标数组,我想找到一个数组中 每个 点与另一个数组中 所有 点之间的最小欧几里得距离。数组的大小不一定相同。例如:

xy1=numpy.array(
[[  243,  3173],
[  525,  2997]])

xy2=numpy.array(
[[ 682, 2644],
[ 277, 2651],
[ 396, 2640]])

我当前的方法遍历每个坐标xyxy1并计算该坐标与其他坐标之间的距离。

mindist=numpy.zeros(len(xy1))
minid=numpy.zeros(len(xy1))

for i,xy in enumerate(xy1):
    dists=numpy.sqrt(numpy.sum((xy-xy2)**2,axis=1))
    mindist[i],minid[i]=dists.min(),dists.argmin()

有没有一种方法可以消除for循环,并以某种方式在两个数组之间进行逐元素计算?我设想生成一个距离矩阵,为此我可以找到每一行或每一列中的最小元素。

看问题的另一种方法。假设我将xy1(length m )和xy2(length p )串联为xy(length n
),并存储原始数组的长度。从理论上讲,我应该能够从这些坐标中生成一个 nxn 距离矩阵,从中可以获取 mxp
子矩阵。有没有一种方法可以有效地生成此子矩阵?

关注者
0
被浏览
95
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    (数月后) scipy.spatial.distance.cdist( X, Y ) 给出了X和Y的所有距离对,2个暗角,3个暗角……
    它也有22个不同的范数,在此处详细介绍

    # cdist example: (nx,dim) (ny,dim) -> (nx,ny)
    
    from __future__ import division
    import sys
    import numpy as np
    from scipy.spatial.distance import cdist
    
    #...............................................................................
    dim = 10
    nx = 1000
    ny = 100
    metric = "euclidean"
    seed = 1
    
        # change these params in sh or ipython: run this.py dim=3 ...
    for arg in sys.argv[1:]:
        exec( arg )
    np.random.seed(seed)
    np.set_printoptions( 2, threshold=100, edgeitems=10, suppress=True )
    
    title = "%s  dim %d  nx %d  ny %d  metric %s" % (
            __file__, dim, nx, ny, metric )
    print "\n", title
    
    #...............................................................................
    X = np.random.uniform( 0, 1, size=(nx,dim) )
    Y = np.random.uniform( 0, 1, size=(ny,dim) )
    dist = cdist( X, Y, metric=metric )  # -> (nx, ny) distances
    #...............................................................................
    
    print "scipy.spatial.distance.cdist: X %s Y %s -> %s" % (
            X.shape, Y.shape, dist.shape )
    print "dist average %.3g +- %.2g" % (dist.mean(), dist.std())
    print "check: dist[0,3] %.3g == cdist( [X[0]], [Y[3]] ) %.3g" % (
            dist[0,3], cdist( [X[0]], [Y[3]] ))
    
    
    # (trivia: how do pairwise distances between uniform-random points in the unit cube
    # depend on the metric ? With the right scaling, not much at all:
    # L1 / dim      ~ .33 +- .2/sqrt dim
    # L2 / sqrt dim ~ .4 +- .2/sqrt dim
    # Lmax / 2      ~ .4 +- .2/sqrt dim
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看