网络X.创建边缘

2024-03-28 19:23:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要检查一下坐标表中的每一点。在它和它的每一个最近的邻居之间做一个边缘。在

但我得到了一些边缘(100100),(40,40)。例如:

我的文本文件里面有以下内容

这是我的密码

import numpy as np
from sklearn.neighbors import NearestNeighbors


import networkx as nx



#Get coordinates
f_name = 'bunny'
#coord = np.genfromtxt(str(f_name)+'.txt',autostrip=True)
coord = np.random.randn(200, 6)

#Fit nearest neighbours to coordinates
neigh = NearestNeighbors(3) 
neigh.fit(coord[:,:3])


#Create a graph
G = nx.Graph() 

#Add all points and there neighbours to graph, make the weight equal to the distance between points
for i in range(0,len(coord)):


    d = neigh.kneighbors(coord[i,:3]) 

    k = 3
    for c in range(0,k):
        p1 = d[1][0][0]
        p2 = d[1][0][c]
        n1 = coord[d[1][0][0],3:6]
        n2 = coord[d[1][0][c],3:6]
        dot = np.dot(n1,n2)

        G.add_edge(p1,p2,weight =1-np.abs(dot))
print G.edges()

示例:如果我循环通过我的边缘,我将得到以下几行

(1881年,1881年) (1881年、1882年) (1882年,1882年)


Tags: tonameimportasnpdot边缘points