在python中使用scipy.spatial.distance.cdist(X,Y)查找一组点之间的距离 [英] finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python

查看:915
本文介绍了在python中使用scipy.spatial.distance.cdist(X,Y)查找一组点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为A的数据数组,看起来像:

I have an array of data, called A that looks something like:

array([[0.59, 1.23], [0.89, 1.67], [0.21,0.99]...])

,其中包含约400组[x,y]点.我想找到A中的每组点到B中的每组点之间的距离,这是另一个数组,看起来与A完全相同,但是长度的一半(所以大约有200组[x,y]点) .因此,如果我想找出B中第q对[x,y]值对与A中所有[x,y]值之间的距离,我已经尝试着沿着

and has about 400 sets of [x,y] points in it. I want to find the distance between every set of points in A to each sets of points in B, which is another array which looks exactly the same as A but is half the length (So about 200 sets of[x,y] points). So if I wanted to find the distance between the q-th pair of [x,y] values in B against all [x,y] values in A, I've tried doing something along the lines of

import scipy.spatial.distance
for q in range(0,len(B)):
    y=scipy.spatial.distance.cdist(A,B[:q,:])

但我认为这不起作用.我只想要一个显示B的第q行与A中所有点之间的距离的输出.

but I don't think this is working. I just want an output that shows the distance between the q-th row of B against all points in A.

推荐答案

两种解决方案:

直接计算完整的矩阵,并访问A和B [q]之间的值的第q列.

calculate the complete matrix directly, and the access the q-th column for the values between A and B[q].

d = scipy.spatial.distance.cdist(A,B)

for q in range(len(B)):
    y = d[:,q]
    print y

如果结果矩阵太大而无法保存在内存中.你可以做到这一点.

If the resulting matrix is too big to hold in memory. You could do this.

for q in range(len(B)):
    y = scipy.spatial.distance.cdist(A,[B[q]])
    print y

这篇关于在python中使用scipy.spatial.distance.cdist(X,Y)查找一组点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆