Plot NetworkX Graph with numpy array error

Viewed 6536

I have my adjacency matrix as a numpy array and would like to plot it as a simple undirected graph using NetworkX but I keep running into this error: AttributeError: module 'scipy.sparse' has no attribute 'coo_array'

I'm following this: Plot NetworkX Graph from Adjacency Matrix in CSV file particular answer and could not get it to work. The only difference is that my adjacency matrix is rather huge with around 30000 columns

This is my graph drawing code:

G = nx.from_numpy_matrix(np.matrix(adj_mtx_np), create_using=nx.DiGraph)
nx.draw(G)
plt.show()

My scipy version is 1.8.0

5 Answers

downgrade networkx to 2.6.3 and it solved this problem for me.

Upgrade both scipy and networkx and it worked for me.

pip install --upgrade scipy networkx

I looked into the scipy file that was generating that error ("convert_matrix.py") line 921: A = sp.sparse.coo_array((d, (r, c)), shape=(nlen, nlen), dtype=dtype).

You need to switch "coo_array" to "coo_matrix".

So it should look like: A = sp.sparse.coo_matrix((d, (r, c)), shape=(nlen, nlen), dtype=dtype)

This worked well enough for me for my project

I encountered the same problem and I used

pip install scipy==1.8.1

on a Windows system and it worked! I guess it may be because previous versions of 'scipy.sparse' don't have 'coo_array'.

I tried the methods in the first answer, but they didn't work for. I also tried to use conda to update scipy to 1.8.1 to no avail.

I am also using vs code in windows 10. I got the same problem. then I tried to upgrade scipy and networks separately but they didn't work for me. Then I tried to upgrade both using the following code. then that works for me.

!pip install --upgrade scipy networkx

In cmd , this code should be like that . Try to open as admin.

pip install --upgrade scipy networkx
Related