Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the communities module "python-louvain" in networkx 2.2?

I used to use this module like this:

import community

if __name__ == '__main__':

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

    partition = community.best_partition(G)

I installed the correct module:

sudo pip3 install python-louvain

I get this error:

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

As far as I know it follows the documentation presented here.

like image 517
DanielTheRocketMan Avatar asked Oct 31 '18 15:10

DanielTheRocketMan


Video Answer


2 Answers

It seems some others have run into this problem before, see: https://bitbucket.org/taynaud/python-louvain/issues/23/module-has-no-attribute-best_partition

If you have another library called community installed that may be causing a problem. Here is one solution proposed in the thread I linked to:

from community import community_louvain
partition = community_louvain.best_partition(G)
like image 100
Johannes Wachs Avatar answered Jun 17 '23 09:06

Johannes Wachs


I am a beginner in using Networkx as well but I used following syntax in Jupyter notebook and it worked fine for me.

!pip install python-louvain
from community import community_louvain
communities =community_louvain.best_partition(G)

Kind Regards,

like image 29
user3665906 Avatar answered Jun 17 '23 09:06

user3665906