Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

I am getting the below given error when am trying to use nx.connected_component_subgraphs(G).

In the dataset there are two coumns(movie and actor), and it's in the form bipartite graph.

I want to get connected components for the movie nodes.

---------------------------------------------------------------------------
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'
like image 720
Satyam Anand Avatar asked Apr 11 '20 09:04

Satyam Anand


4 Answers

This was deprecated with version 2.1, and finally removed with version 2.4.

See these instructions

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

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

like image 56
Joel Avatar answered Oct 25 '22 12:10

Joel


connected_component_subgraphs has been removed from the networkx library. You can use the alternative described in the deprecation notice.

For your example, refer to the code below:

A = (B.subgraph(c) for c in nx.connected_components(B))
A = list(A)[0]
like image 36
Sagar Baronia Avatar answered Oct 25 '22 10:10

Sagar Baronia


Use the following code for single line alternative

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

Or you can install the previous version of networkx

pip install networkx==2.3
like image 34
ABHISHEK D Avatar answered Oct 25 '22 12:10

ABHISHEK D


First I got

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

To fix the above error, I upgraded networkx using

pip install --upgrade --force-reinstall  network

It installed unetworkx-2.6.3, I got the error

AttributeError: module networkx has no attribute connected_component_subgraphs.

I used the below code as mentioned by ABHISHEK D, it resolved. Thanks.

A=list(B.subgraph(c) for c in nx.connected_components(B))[0]
like image 21
VISHNUKUMAR CHERUKU Avatar answered Oct 25 '22 12:10

VISHNUKUMAR CHERUKU