我需要遍历坐标列表中的每个点。在它和每个最近的邻居之间建立一条边。
但我得到了一些优势 (100,100) , (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)