Mohammad Heydari
Mohammad Heydari

Reputation: 4290

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

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 ?

Upvotes: 16

Views: 30636

Answers (7)

A. chahid
A. chahid

Reputation: 303

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)

Upvotes: 0

financial_physician
financial_physician

Reputation: 1988

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.

Upvotes: 4

Mahmood Kazemi
Mahmood Kazemi

Reputation: 11

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

Upvotes: 1

Terence Yang
Terence Yang

Reputation: 658

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.

Upvotes: 13

Alan Peraza
Alan Peraza

Reputation: 41

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

import community.community_louvain

Source

Upvotes: 4

tong
tong

Reputation: 731

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

Upvotes: 0

DSM
DSM

Reputation: 353419

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)

Upvotes: 13

Related Questions