属性错误:模块'networkx'没有属性'connected_component_subgraphs'。

21
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'

你使用的是哪个版本?从 nx.__version__ 中获取了什么? - yatu
它是2.4版本。 - Satyam Anand
无法复现...我有2.3版本,可以毫无问题地调用nx.connected_component_subgraphs - yatu
4个回答

28

这个功能从版本2.1开始被弃用,并在版本2.4中被彻底删除。

请参阅此说明

使用(G.subgraph(c) for c in connected_components(G))

或者(G.subgraph(c).copy() for c in connected_components(G))


解决了这个问题。谢谢 Joel。 - Satyam Anand
networkx.exception.NetworkXNotImplemented: 对于有向类型未实现 - CS QGB
2
@CSQGB - 你有问题吗?如果有,请提出一个新的问题。从你提供的信息中,我无法确定问题所在(是有向图未实现某些命令,但具体是哪个命令?)。 - Joel

9

connected_component_subgraphs已经从networkx库中移除。 你可以使用在弃用通知中描述的替代方法。

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

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

子图 = (self.digraph.subgraph(c).copy() for c in nx.strongly_connected_components(self.digraph)) #<生成器对象 - CS QGB

3

使用以下代码来实现单行替代

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

或者您可以安装networkx的先前版本。

pip install networkx==2.3

1

首先我遇到了

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

为了解决上述错误,我使用以下命令升级了networkx:

pip install --upgrade --force-reinstall  network

它安装了unetworkx-2.6.3,我收到了错误信息
AttributeError: 模块networkx没有属性connected_component_subgraphs.
我使用ABHISHEK D提供的以下代码,问题得到解决。谢谢。
A=list(B.subgraph(c) for c in nx.connected_components(B))[0]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接