如何解决NetworkX错误NoPath:无法从Y

问题描述

在进行常规DBSCAN之后,我得到了包含集群的地图

enter image description here

Im将最近的节点附加到OSMNX绘制的每个公司,然后创建基于网络的距离矩阵,以便从此TUTORIAL

复制基于网络的空间聚类

加快距离矩阵的计算速度,而不是计算每个公司到每个公司的距离,而是找到每个至少附着一个公司的节点,然后计算每个这样的节点到每个这样的节点距离。一旦有了节点到节点的距离,就可以重新索引它,以利用这些距离来确定企业。

这是代码:

# attach nearest network node to each firm --APPLY SOLUTION B HERE
firms['nn'] = ox.get_nearest_nodes(G,X=firms['x'],Y=firms['y'],method='balltree')
print(len(firms['nn']))

# we'll get distances for each pair of nodes that have firms attached to them
nodes_unique = pd.Series(firms['nn'].unique())
nodes_unique.index = nodes_unique.values
print(len(nodes_unique))

# convert MultiDiGraph to DiGraph for simpler faster distance matrix computation
G_dm = nx.DiGraph(G)

输出:

269
230
time: 2.74 s

之后

# calculate network-based distance between each node --APPLY SOLUTION A HERE

def network_distance_matrix(u,G,vs=nodes_unique):
    
    dists = [nx.dijkstra_path_length(G,source=u,target=v,weight='length') for v in vs]
    return pd.Series(dists,index=vs)

最后

%%time
from tqdm._tqdm_notebook import tqdm_notebook
tqdm_notebook.pandas()
# create node-based distance matrix called node_dm
node_dm = nodes_unique.progress_apply(network_distance_matrix,G=G_dm)
node_dm = node_dm.astype(int)
print(node_dm.size)

解决方案A和B特别要感谢gboeing:

# OPTION A: recursively remove unsolvable origin/destination nodes and re-try
def network_distance_matrix(u,vs=nodes_unique):
G2 = G.copy()
solved = False
while not solved:
    try:
    dists = [nx.dijkstra_path_length(G,index=vs)
        solved = True
    except nx.exception.NetworkXNoPath:
        G2.remove_nodes_from([dist])

# OPTION B: Use a strongly (instead of weakly) connected graph 
Gs = ox.utils_graph.get_largest_component(G,strongly=True) 

# attach nearest network node to each firm
firms['nn'] = ox.get_nearest_nodes(Gs,method='balltree') 
print(len(firms['nn'])) 

# we'll get distances for each pair of nodes that have firms attached to them
nodes_unique = pd.Series(firms['nn'].unique()) 
nodes_unique.index = nodes_unique.values 
print(len(nodes_unique)) 

# convert MultiDiGraph to DiGraph for simpler faster distance matrix computation 
G_dm = nx.DiGraph(Gs)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...