到((行,列,距离)列表的距离的距离的数组 [英] Numpy array of distances to list of (row,col,distance)

查看:152
本文介绍了到((行,列,距离)列表的距离的距离的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来如下的nd数组:

I have an nd array that looks as follows:

[[ 0.          1.73205081  6.40312424  7.21110255  2.44948974]
 [ 1.73205081  0.          5.09901951  5.91607978  1.        ]
 [ 6.40312424  5.09901951  0.          1.          4.35889894]
 [ 7.21110255  5.91607978  1.          0.          5.09901951]
 [ 2.44948974  1.          4.35889894  5.09901951  0.        ]]

此数组中的每个元素都是一个距离,我需要将其转换为具有行,col,distance的列表,如下所示:

Each element in this array is a distance and I need to turn this into a list with the row,col,distance as follows:

l = [(0,0,0),(0,1, 1.73205081),(0,2, 6.40312424),...,(1,0, 1.73205081),(1,1,0),...,(4,4,0)] 

另外,删除对角线元素也很酷,因为(i,j)元素(j,i)已经存在.从本质上讲,是否可以仅取其顶部三角形矩阵?

Additionally, it would be cool to remove the diagonal elements and also the elements (j,i) as (i,j) are already there. Essentially, is it possible to take just the top triangular matrix of this?

这是否有可能有效地进行(没有很多循环)?我创建了带有正方形的数组,但是找不到任何文档来执行此操作.

Is this possible to do efficiently (without a lot of loops)? I had created this array with squareform, but couldn't find any docs to do this.

推荐答案

squareform完成所有这些操作.阅读文档并进行实验.它可以双向工作.如果给它一个矩阵,它将返回上三角值(压缩形式).如果为它们提供这些值,它将返回矩阵.

squareform does all this. Read the docs and experiment. It works in both directions. If you give it a matrix it returns the upper triangle values (condensed form). If you give it those values, it returns the matrix.

In [668]: M
Out[668]: 
array([[ 0. ,  0.1,  0.5,  0.2],
       [ 0.1,  0. ,  2. ,  0.3],
       [ 0.5,  2. ,  0. ,  0.2],
       [ 0.2,  0.3,  0.2,  0. ]])
In [669]: spatial.distance.squareform(M)
Out[669]: array([ 0.1,  0.5,  0.2,  2. ,  0.3,  0.2])
In [670]: v=spatial.distance.squareform(M)
In [671]: v
Out[671]: array([ 0.1,  0.5,  0.2,  2. ,  0.3,  0.2])
In [672]: spatial.distance.squareform(v)
Out[672]: 
array([[ 0. ,  0.1,  0.5,  0.2],
       [ 0.1,  0. ,  2. ,  0.3],
       [ 0.5,  2. ,  0. ,  0.2],
       [ 0.2,  0.3,  0.2,  0. ]])

您还可以指定一个forcechecks参数,但没有参数的话,它就随形状而变.

You can also specify a force and checks parameter, but without those it just goes by the shape.

指数可以来自triu

In [677]: np.triu_indices(4,1)
Out[677]: 
(array([0, 0, 0, 1, 1, 2], dtype=int32),
 array([1, 2, 3, 2, 3, 3], dtype=int32))

In [680]: np.vstack((np.triu_indices(4,1),v)).T
Out[680]: 
array([[ 0. ,  1. ,  0.1],
       [ 0. ,  2. ,  0.5],
       [ 0. ,  3. ,  0.2],
       [ 1. ,  2. ,  2. ],
       [ 1. ,  3. ,  0.3],
       [ 2. ,  3. ,  0.2]])

只需检查一下,我们就可以使用这些值填写4x4矩阵

Just to check, we can fill in a 4x4 matrix with these values

In [686]: A=np.vstack((np.triu_indices(4,1),v)).T
In [687]: MM = np.zeros((4,4))
In [688]: MM[A[:,0].astype(int),A[:,1].astype(int)]=A[:,2]
In [689]: MM
Out[689]: 
array([[ 0. ,  0.1,  0.5,  0.2],
       [ 0. ,  0. ,  2. ,  0.3],
       [ 0. ,  0. ,  0. ,  0.2],
       [ 0. ,  0. ,  0. ,  0. ]])

那些triu索引也可以从M中获取值:

Those triu indices can also fetch the values from M:

In [693]: I,J = np.triu_indices(4,1)
In [694]: M[I,J]
Out[694]: array([ 0.1,  0.5,  0.2,  2. ,  0.3,  0.2])

squareformspatial.distance._distance_wrap中使用编译后的代码,因此我希望它对大型数组会非常快.唯一的问题是它只返回压缩形式的值,而不返回索引.但是给定形状,就总是可以计算出指标.它们不需要与值一起存储.

squareform uses compiled code in spatial.distance._distance_wrap so I expect it will be quite fast for large arrays. Only problem it just returns the condensed form values, but not the indices. But given the shape,the indices can always be calculated. They don't need to be stored with the values.

这篇关于到((行,列,距离)列表的距离的距离的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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