【发布时间】:2019-08-07 20:53:59
【问题描述】:
我有一个包含 orgin_nodes 和 Distination_nodes 的数据框,如下所示:
我需要使用networkx 库通过应用下一个函数来计算这些节点之间的short_path_length:
def short_path_length (node1,node2):
return nx.shortest_path_length(G, node1, nod2,weight='length')
df['short_path_length']=np.vectorize(short_length_nodes)(df['Orgin_nodes'],df['Destination_nodes'])
其中G 是从osmnx 库派生的网络图:
我将此代码应用于数据帧示例,结果如下:
当我将它应用于大约 3000000 行的原始数据帧时,它需要更多时间吗?
有没有办法让跑得更快?
更新1:
我关注了@gboeing 的回答并将networkx graph 转换为igraph 如下(https://github.com/gboeing/osmnx-examples/blob/master/notebooks/18-osmnx-to-igraph.ipynb):
ox.config(use_cache=True, log_console=True)
weight = 'length'
G_nx = nx.relabel.convert_node_labels_to_integers(G)
# convert networkx graph to igraph
G_ig = ig.Graph(directed=True)
G_ig.add_vertices(list(G_nx.nodes()))
G_ig.add_edges(list(G_nx.edges()))
G_ig.vs['osmid'] = list(nx.get_node_attributes(G_nx, 'osmid').values())
G_ig.es[weight] = list(nx.get_edge_attributes(G_nx, weight).values())
def short_path_length(node1,node2):
return G_ig.shortest_paths(source=node1,target=node2, weights=weight)[0][0]
df['short_path_length'] = df.apply(short_path_length(df['Orgin_nodes'],df['Destination_nodes']), axis=1)
我得到了这个错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<timed exec> in <module>()
<timed exec> in short_path_length(node1, node2)
ValueError: vertex IDs must be positive, got: -1
此错误的原因是df['Orgin_nodes'],df['Destination_nodes'] 中的节点编号与G_ig 顶点名称不匹配。
我该怎么解决?
更新2
我通过创建包含G_nx.nodes 及其对应的OSMid 值并将Orgin_nodes 和Destination_nodes 替换为Destination_nodes 解决了上述问题,如下所示:
df_indices_osmid_Orgin=pd.DataFrame.from_dict({'Orgin_nodes':list(nx.get_node_attributes(G_nx, 'osmid').values()),'Indecise_Nodes_Orgin':list(G_nx.nodes())})
df=pd.merge(df,df_indices_osmid_Orgin,how='inner',on='Orgin_nodes')
df_indices_osmid_Dest=pd.DataFrame.from_dict({'Destination_nodes':list(nx.get_node_attributes(G_nx, 'osmid').values()),'Indecise_Nodes_Dest':list(G_nx.nodes())})
df=pd.merge(df,df_indices_osmid_Dest,how='inner',on='Destination_nodes')
并应用df的以下函数样本来测量最短距离:
sampl_df=df.head()
def short_path_length(row):
return G_ig.shortest_paths(source=row['Indecise_Nodes_Orgin'], target=row['Indecise_Nodes_Dest'], weights=weight)[0][0]
sampl_df['short_path_length_1'] = sampl_df.apply(short_path_length, axis=1)
虽然运行没有错误,但与之前的试用相比,耗时更长:
sampl_df=df.head()
%%time
def short_path_length(row):
return G_ig.shortest_paths(source=row['Indecise_Nodes_Orgin'], target=row['Indecise_Nodes_Dest'], weights=weight)[0][0]
sampl_df['short_path_length_1'] = sampl_df.apply(short_path_length, axis=1)
持续时间:2.89 秒
每个循环 2.88 秒 ± 66.3 毫秒(平均值 ± 标准偏差,7 次运行,每次 1 个循环)
%%time
def short_path_length(row):
return nx.shortest_path_length(G, row['Orgin_nodes'], row['Destination_nodes'], weight='length')
sampl_df['short_path_length_2'] = sampl_df.apply(short_path_length, axis=1)
持续时间:1.24 秒
每个循环 1.2 秒 ± 15.7 毫秒(7 次运行的平均值 ± 标准偏差,每次 1 个循环)
%%time
def short_path_length (node1,node2):
return nx.shortest_path_length(G, node1, node2,weight='length')
sampl_df['short_path_length_intr3']=np.vectorize(short_path_length)(sampl_df['Orgin_nodes'],sampl_df['Destination_nodes'])
持续时间:1.2 秒
每个循环 1.21 秒 ± 12 毫秒(7 次运行的平均值 ± 标准偏差,每次 1 个循环)
因此可以指出,第三个是最好的,或者这不是衡量它们中哪个运行更快的标准。
【问题讨论】:
-
您可能会从
df['short_path_length'] = list(map(short_path_length, df['Orgin_nodes'], df['Destination_nodes']))获得稍微好一点的性能,但只要它不能被矢量化,这并不重要。如果真的很重要,我认为你需要自己动手。
标签: pandas performance networkx igraph shortest-path