gpt4 book ai didi

python - 根据 NetworkX 中的某些边缘属性高效提取子图

转载 作者:太空狗 更新时间:2023-10-29 20:56:32 29 4
gpt4 key购买 nike

可以通过指定节点列表轻松地从 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))

关于python - 根据 NetworkX 中的某些边缘属性高效提取子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16910538/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com