可以通过指定节点列表轻松地从NetworkX图中提取子图,但我找不到一种有效的方法来执行按边提取子图。例如,提取由权重超过用户定义阈值的边组成的子图。
目前我是这样做的:
## extracts all edges satisfy the weight threshold (my_network is directed):
eligible_edges = [(from_node,to_node,edge_attributes) for from_node,to_node,edge_attributes in my_network.edges(data=True) if edge_attributes['weight'] > threshold]
new_network = NetworkX.DiGraph()
new_network.add_edges_from(eligible_edges)
有更好的方法吗?
谢谢你友好的回答。
这看起来是最好的解决方案。
您可以通过使用graph.edges_iter()
而不是graph.edges()
来节省内存,例如
>>> G = nx.DiGraph(((source, target, attr) for source, target, attr in my_network.edges_iter(data=True) if attr['weight'] > threshold))