Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 535
Mohammad Heydari Avatar asked Oct 26 '18 22:10

Mohammad Heydari


2 Answers

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)
like image 182
DSM Avatar answered Oct 14 '22 09:10

DSM


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.

like image 23
Fan Yang Avatar answered Oct 14 '22 11:10

Fan Yang