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

Rox*_*lia 5 python graph networkx

我尝试运行这段代码:

from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.louvain(G, resolution=1., randomize=False)
Run Code Online (Sandbox Code Playgroud)

但错误仍然相同。我已尝试 AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'给出的所有选项

但它不起作用。

另外,我在 Google Colab 工作,并且安装了 cdlib。

jyl*_*lls 3

由此看来好像有一个communitypython包与该python-louvain包冲突。这两个软件包恰好都预装在 google colab 内核中。为了避免这种冲突,我只是卸载了networkxpython-louvain然后community重新安装了networkxpython-louvain。最后我安装了cdlib。之后我运行了你的代码,一切正常。所以总体来说代码是:

!pip uninstall networkx
!pip uninstall python-louvain
!pip uninstall community
!pip install python-louvain
!pip install networkx

!pip install cdlib

from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.louvain(G, resolution=1., randomize=False)
print(coms)
Run Code Online (Sandbox Code Playgroud)

输出给出:

在此输入图像描述