Search code examples
python-3.xanacondanetworkxgraph-theory

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 ?


Solution

  • 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)