我有两个数组centroids
和nodes
我需要找到centroids
中每个点到nodes
中任何点的最短距离
centroids
的输出如下
array([[12.52512263, 55.78940022],
[12.52027731, 55.7893347 ],
[12.51987146, 55.78855611]])
nodes
的输出如下
array([[12.5217378, 55.7799275],
[12.5122589, 55.7811443],
[12.5241664, 55.7843297],
[12.5189395, 55.7802709]])
我使用以下代码获得最短距离
shortdist_from_centroid_to_node = np.min(cdist(centroids,nodes))
然而,这是我得到的输出(我应该得到3行输出)
Out[131]: 3.0575613850140956e-05
有人能说明问题出在哪里吗?谢谢
如果我没有弄错的话,您的代码会说您正在尝试访问
min
值,因此您会得到一个值。删除np.min()
尝试:我想您需要的只是添加一个名为
axis
的arg,如下所示:至于轴arg的含义,您可以参考numpy.min。总而言之,您需要在每一行上使用最小值,而不是在整个矩阵上使用最小值
执行np.min时,它返回2d数组的最小值。 您需要每个质心的最小值
要获得关联索引,一种方法是获取对应值的索引。另一种方法是编写一个自定义的min()函数,该函数也会返回索引(因此只能解析列表一次)
具有自定义功能的解决方案:
相关问题 更多 >
编程相关推荐