简体   繁体   English

点集之间的符号距离

[英]Signed distances between sets of points

Say I have two sets of points X and Y possibly holding a different number of points, and of different dimensionality. 假设我有两组点XY可能持有不同数量的点,并且具有不同的维数。 We can assume that X and Y are nxm numpy arrays (n points, m dimensions each) 我们可以假设XYnxm numpy数组(n个点,每个m个维)

I would like to obtain the distribution (median and std) of sum(yx) distances between the points in Y and X . 我想获得YX点之间的sum(yx)距离的分布(中值和标准差)。

Eg if one y point is (2,4) and one x point is (3,5) the sum(yx) distance would be 2-3 + 4-5 = -2 . 例如,如果一个y点是(2,4)而一个x点是(3,5)sum(yx)距离将为2-3 + 4-5 = -2

How can I do that in Python without looping? 如何在Python中做到这一点而无需循环?

A quick browse through scipy.spatial.distance did not yield any results so you likely need to use broadcasting: 快速浏览scipy.spatial.distance不会产生任何结果,因此您可能需要使用广播:

>>> a = np.random.rand(5,3) #(N x M)
>>> b = np.random.rand(4,3) #(K X M)
>>> dists = np.sum(a[:,None,:] - b, axis=-1)
>>> dists
array([[-0.57713957, -1.88996939, -0.13993727, -1.17222018],
       [ 0.89288677, -0.41994304,  1.33008907,  0.29780616],
       [ 0.45866859, -0.85416123,  0.89587088, -0.13641203],
       [ 1.12909228, -0.18373754,  1.56629457,  0.53401166],
       [ 0.64299673, -0.66983308,  1.08019903,  0.04791612]])

Now just grab the median and std: 现在只需获取中位数和std:

>>> np.median(dists)
0.17286113728020264
>>> np.std(dists)
0.88228393506243197

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM