在pdist凝聚距离矩阵中找到最小值的索引 [英] Find the index of the min value in a pdist condensed distance matrix

查看:139
本文介绍了在pdist凝聚距离矩阵中找到最小值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用scipy.spatial.distance.pdist(X)来计算以下列表X的每对元素之间的欧氏距离度量:

I have used scipy.spatial.distance.pdist(X) to calculate the euclidian distance metric between each pair of elements of the below list X:

X = [[0, 3, 4, 2], [23, 5, 32, 1], [3, 4, 2, 1], [33, 54, 5, 12]]

这将返回一个简化的距离矩阵:

This returns a condensed distance matrix:

array([ 36.30426972,   3.87298335,  61.57109712,  36.06937759,
        57.88782255,  59.41380311])

对于每个元素X,我需要找到最接近的其他元素的索引.

For each element X, I need to find the index of the closest other element.

将压缩距离矩阵转换为正方形有助于可视化结果,但是我无法弄清楚如何以编程方式识别X中每个元素的最接近元素X的索引.

Converting the condensed distance matrix to square form help visualize the results, but I can't figure out how to programmatically identify the index of the closest element X for each element in X.

array([[  0.        ,  36.30426972,   3.87298335,  61.57109712],
       [ 36.30426972,   0.        ,  36.06937759,  57.88782255],
       [  3.87298335,  36.06937759,   0.        ,  59.41380311],
       [ 61.57109712,  57.88782255,  59.41380311,   0.        ]])

我相信argmin()是要使用的函数,但是我从这里迷失了.感谢您的任何提前帮助.

I believe argmin() is the function to use, but I'm lost from here. Thanks for any help in advance.

推荐答案

我们将对结果的平方形式进行操作.首先,排除纽约离纽约最近"的答案,

We'll operate on the square form of the results. First, to exclude "New York is closest to New York" answers,

numpy.fill_diagonal(distances, numpy.inf)

然后,它是沿着轴的简单argmin:

Then, it's a simple argmin along an axis:

closest_points = distances.argmin(axis=0)

这篇关于在pdist凝聚距离矩阵中找到最小值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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