AttributeError:模块“ networkx.algorithms.community”没有属性“ best_partition”

Moh*_*ari 4 graph-theory networkx python-3.x anaconda

好吧,我想利用社会检测算法通过networkx上著名的Facebook截取数据集。这是我的代码:

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()]
Run Code Online (Sandbox Code Playgroud)

但是当我运行单元格时,我会遇到标题错误:

AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

小智 11

我在 CS224W 中遇到了这个

AttributeError: module 'community' has no attribute 'best_partition' 在此输入图像描述

请更改此文件 karate.py

将导入替换为 import community.community_louvain as community_louvain

那么它对我有用。


DSM*_*DSM 6

我想你混淆了社区模块在networkx适当与在社区发现蟒蛇,鲁汶该模块使用 networkx。

如果您安装python-louvain,则其文档中的示例对我有用,并生成类似

样本图分区

请注意,您将要导入community,而不是networkx.algorithms.community。那是,

import community

[.. code ..]

partition = community.best_partition(G_fb)
Run Code Online (Sandbox Code Playgroud)

  • 您实际上安装了“ python-louvain”模块吗? (2认同)