AttributeError:模块“networkx.algorithms.community”没有属性“best_partition”

新手上路,请多包涵

好吧,我正在尝试在著名的 facebook 快照数据集上使用 networkx社区检测算法。这是我的代码:

 import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import community
from networkx.algorithms.community.centrality import girvan_newman

G_fb = nx.read_edgelist("./facebook_combined.txt",create_using = nx.Graph(), nodetype=int)

parts = community.best_partition(G_fb)
values = [parts.get(node) for node in G_fb.nodes()]

但是当我运行单元格时,我遇到了标题错误:

 AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'

有什么建议吗?

原文由 Mohammad Heydari 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.4k
2 个回答

我认为您将 networkx 中的 社区 模块与 使用 networkx 的 python-louvain 模块中的社区检测混淆了。

如果你安装 python-louvain,它的文档中的示例对我有用,并生成像这样的图像

样本图分区

请注意,您将导入 community ,而不是 networkx.algorithms.community 。那是,

 import community

[.. code ..]

partition = community.best_partition(G_fb)

原文由 DSM 发布,翻译遵循 CC BY-SA 4.0 许可协议

我在 CS224W 遇到过这个

AttributeError: module 'community' has no attribute 'best_partition' 在此处输入图像描述

请更改此文件 karate.py

将导入替换为 import community.community_louvain as community_louvain

然后它对我有用。

原文由 Fan Yang 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进