AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'

AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'

B = nx.Graph()
B.add_nodes_from(data['movie'].unique(), bipartite=0, label='movie')
B.add_nodes_from(data['actor'].unique(), bipartite=1, label='actor')
B.add_edges_from(edges, label='acted')

A = list(nx.connected_component_subgraphs(B))[0]

我在尝试使用 nx.connected_component_subgraphs(G).

时遇到以下错误

数据集中有两个列(电影和演员),并且是二分图的形式。

我想获取电影节点的连接组件。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-efff4e6fafc4> in <module>
----> 1 A = list(nx.connected_component_subgraphs(B))[0]

AttributeError: module 'networkx' has no attribute 'connected_component_subgraphs'

这在 2.1 版中已弃用,并最终在 2.4 版中删除。

these instructions

Use (G.subgraph(c) for c in connected_components(G))

Or (G.subgraph(c).copy() for c in connected_components(G))

connected_component_subgraphs 已从 networkx 库中删除。 您可以使用弃用通知中描述的替代方法。

对于您的示例,请参考以下代码:

A = (B.subgraph(c) for c in nx.connected_components(B))
A = list(A)[0]

单行替换使用以下代码

A=list(B.subgraph(c) for c in nx.connected_components(B))[0]

或者你可以安装之前版本的networkx

pip install networkx==2.3

首先我得到了

AttributeError: module 'matplotlib.cbook' has no attribute 'iterable'.

为了修复上述错误,我使用

升级了 networkx
pip install --upgrade --force-reinstall  network

安装了unetworkx-2.6.3,报错

AttributeError: module networkx has no attribute connected_component_subgraphs.

我使用了 ABHISHEK D 提到的以下代码,它解决了。谢谢

A=list(B.subgraph(c) for c in nx.connected_components(B))[0]