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

AttributeError: module 'networkx.algorithms.community' has no attribute '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'

有什么建议吗?

我认为您混淆了 使用 networkx 的 community module in networkx proper with the community detection in the python-louvain 模块。

如果您安装 python-louvain,其文档中的示例对我有用,并生成像

这样的图像

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

import community

[.. code ..]

partition = community.best_partition(G_fb)

我遇到了类似的问题。 就我而言,这是因为在另一台机器上库 networkx 已过时。

使用以下命令,问题已解决。

pip3 install --upgrade networkx

我遇到了同样的问题。就我而言,解决了以不同方式导入模块的问题:

import community.community_louvain

Source

我在 CS224W 遇到过这个问题

AttributeError: module 'community' has no attribute 'best_partition'

请更改此文件karate.py

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

那对我有用。

我在CS224W也遇到过这个问题 但更改 karate.py 或其他解决方案无效。

对我来说(在 colab 中)使用新的 PyG 安装代码有效。 此代码将安装最新版本:

!pip install -q torch-scatter -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q torch-sparse -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q git+https://github.com/rusty1s/pytorch_geometric.git

我天真地认为 pip install community 是我正在寻找的包,但我需要 pip install python-louvain 然后将其导入为 import community.