13

well i am trying to use community detection algorithms by networkx on famous facebook snap data set. here are my codes :

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()]

but when i'm run the cell i face with the title error which is :

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

any advice ?

7 Answers 7

12

I think you're confusing the community module in networkx proper with the community detection in the python-louvain module which uses networkx.

If you install python-louvain, the example in its docs works for me, and generates images like

sample graph partition

Note that you'll be importing community, not networkx.algorithms.community. That is,

import community

[.. code ..]

partition = community.best_partition(G_fb)
4
  • when i tried import community i faced with this error : No module named 'community' Oct 27, 2018 at 8:56
  • 4
    Did you actually install the python-louvain module?
    – DSM
    Oct 27, 2018 at 15:35
  • save my day. Thanks
    – Adiii
    Jan 16, 2019 at 14:30
  • 1
    I'm using the exact same code as yours but still it gives the same error. Could you help? May 5, 2021 at 2:58
8

I faced this in CS224W

AttributeError: module 'community' has no attribute 'best_partition' enter image description here

Pls change this file karate.py

replace import to import community.community_louvain as community_louvain

then it works for me.

0
4

I had the same problem. In my case, it was solved importing the module in a different manner:

import community.community_louvain

Source

1
  • So thanks! | import community.community_louvain as louvain | partitions = louvain.best_partition(G) Sep 24, 2022 at 15:33
1

I also faced this in CS224W but changing the karate.py or other solutions didn't work.

For me (in colab) using the new PyG installation code worked. this code, will install the last version:

!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
1

I naively thought that pip install community was the package I was looking for but rather I needed pip install python-louvain which is then imported as import community.

0

I had a similar issue. In my case, it was because on the other machine the library networkx was obsolete.

With the following command, the issues was solved.

pip3 install --upgrade networkx
0

This has helped me to run the code without errors:

pip uninstall community
import community.community_louvain as cl
partition = cl.best_partition(G_fb)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.