如何在 networkx 2.2 中使用社区模块“python-louvain”?

Dan*_*Man 4 networkx python-3.x

我曾经像这样使用这个模块:

import community

if __name__ == '__main__':

    G = nx.karate_club_graph()
    pos = nx.spring_layout(G)

    partition = community.best_partition(G)
Run Code Online (Sandbox Code Playgroud)

我安装了正确的模块:

sudo pip3 install python-louvain
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

AttributeError: module 'community' has no attribute 'best_partition'
Run Code Online (Sandbox Code Playgroud)

据我所知,它遵循此处提供的文档。

Joh*_*chs 6

似乎其他一些人之前遇到过这个问题,请参阅:https : //bitbucket.org/taynaud/python-louvain/issues/23/module-has-no-attribute-best_partition

如果您安装了另一个名为 community 的库,这可能会导致问题。这是我链接到的线程中提出的一种解决方案:

from community import community_louvain
partition = community_louvain.best_partition(G)
Run Code Online (Sandbox Code Playgroud)


小智 5

我也是使用 Networkx 的初学者,但我在 Jupyter Notebook 中使用了以下语法,它对我来说效果很好。

!pip install python-louvain
from community import community_louvain
communities =community_louvain.best_partition(G)
Run Code Online (Sandbox Code Playgroud)

亲切的问候,