无法从 networkx 导入 louvain_partitions

Can't import louvain_partitions from networkx

Python 3.9 on Mac OSX 11.6.1 with networkx 2.7.1(最新版本)

我无法从 networkx 访问 louvain_partitions 工具来确定简单的非定向网络中的社区。虽然我在创建图形和执行其他操作时没有问题,但我无法导入此方法。这是一个带有错误消息的简单示例。我一定遗漏了一些明显的东西。

import numpy as np
import networkx as nx
A=np.array([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,0,0],[0,0,0,0,1],[0,0,0,1,0]])
G=nx.from_numpy_matrix(A)
clusters=nx.louvain_partitions(G)

Traceback (most recent call last):   File "<pyshell#9>", line 1, in <module>
    clusters=nx.louvain_partitions(G)   File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/networkx/__init__.py", line 51, in __getattr__
    raise AttributeError(f"module {__name__} has no attribute {name}") AttributeError: module networkx has no attribute louvain_partitions

它不直接在 nx 下,这对我有用。

clusters=nx.algorithms.community.louvain_partitions(G)