简体   繁体   English

Python Scipy和Networkx:如何计算欧几里得距离矩阵?

[英]Python Scipy and Networkx: How to compute a Euclidean distance matrix?

I am using Python Networkx to build a graph and I have the following node positions as an example (positions.txt): 我正在使用Python Networkx构建图形,并且以以下节点位置为例(positions.txt):

1 21.5 23
2 24.5 20
3 19.5 19
4 22.5 15
5 24.5 12  

Node ID, X, Y. I read the file using Pandas and set the positions as node attributes in Networkx. 节点ID,X,Y。我使用Pandas读取文件,并将位置设置为Networkx中的节点属性。 Nodes are added using add_edge(id1, id2, weight) method in a for loop (with no self edges). 使用add_edge(id1, id2, weight)方法在for循环中添加节点(无自add_edge(id1, id2, weight) )。 So far, I have assumed that all the nodes are connected to each other by default and have not taken parameters such as Radius or Distance of nodes into consideration. 到目前为止,我已经假定所有节点默认情况下都是彼此连接的,并且未考虑诸如半径或节点距离之类的参数。
Now, I want to compute a Euclidean radius or distance matrix between the nodes using these node positions, and with this matrix I want to be able to print the neighbours of a node that fall within a given radius n and save the file as a csv matrix file. 现在,我想使用这些节点位置来计算节点之间的欧几里德半径或距离矩阵,并希望使用该矩阵来打印属于给定半径n的节点的邻居并将文件另存为csv矩阵文件。 A friend suggested that I use scipy 's euclidean method but I have no clue how to use this to build the matrix, hence I have no starting point. 一位朋友建议我使用scipyeuclidean方法,但是我不知道如何使用它来构建矩阵,因此我没有起点。 I tried using ego_graph() but it did not give me the desired results. 我尝试使用ego_graph()但没有给我想要的结果。
Any help is appreciated in solving my problem. 感谢您为解决我的问题提供的任何帮助。
Thanks in advance. 提前致谢。 (Using Ubuntu 14.04 32-bit VM and Python 2.7) (使用Ubuntu 14.04 32位VM和Python 2.7)

You can use scipy.spatial.distance.cdist() to generate your distance matrix given a list of coordinates. 您可以使用scipy.spatial.distance.cdist()生成给定坐标列表的距离矩阵。

Obviously a numpy array is always 0-indexed and if your nodes have random numbers, you want to keep a list of them to know which row/column of your matrix corresponds to which pair. 显然,一个numpy数组始终为0索引,如果您的节点具有随机数,则希望保留它们的列表以了解矩阵的哪一行/哪一列对应于哪一对。

What you do after that should be trivial, but I'll give an example based on your description. 在那之后您应该做些琐碎的事,但是我将根据您的描述举一个例子。 For each node, print the id of said node and the ids of any neighbouring nodes within your threshold distance. 对于每个节点,请打印该节点的ID和您的阈值距离内的所有相邻节点的ID。

import numpy as np
from scipy.spatial import distance

def neighbour_nodes_of(node, dist_matrix, distance):
    row = dist_matrix[node]
    return np.where((row <= distance) * (row > 0))[0]

ids = [1, 2, 3, 4, 5]
coords = [(21.5, 23),
          (24.5, 20),
          (19.5, 19),
          (22.5, 15),
          (24.5, 12),
          ]
threshold = 8.

dist_matrix = distance.cdist(coords, coords)

# this is something you can write to a file instead
for i, node_id in enumerate(ids):
    neighbours = neighbour_nodes_of(i, dist_matrix, threshold)
    neighbour_ids = [ids[n] for n in neighbours]
    print([node_id] + neighbour_ids)

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

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