8

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

yxx
  • 135
  • 1
  • 1
  • 6
  • 1
    can you show us your code with sample data like for 10 columns? – noob Mar 05 '22 at 17:49
  • I think you'll find sparse arrays were added in scipy 1.8.0 so it's likely you are using an earlier version. If you've imported scipy as sp, then print(sp.__version__) will confirm – Riley Mar 05 '22 at 22:01
  • @RabeeQasem I have updated with my code, as for my data, it is a regular adjacency matrix, im not sure how to paste it here as its quite big – yxx Mar 06 '22 at 04:05
  • @Riley I have updated with my version – yxx Mar 06 '22 at 04:06

6 Answers6

10

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

hiroki
  • 101
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 21 '22 at 04:14
  • Welcome to StackOverflow! There is no version 1.6.3 so I think you meant 2.6.3. I encountered the same issue and downgraded to 2.63 (from 2.7.1) and now it works fine. There is clearly some inconsistency between packages with the latest release, even using Anaconda to resolve dependencies (which I had done). TLDR: downgrading to version 2.6.3 worked for me. – A. G. Mar 21 '22 at 10:35
  • Thanks for your comment. I editted my post. Again, thanks a lot. – hiroki Mar 22 '22 at 05:04
9

Upgrade both scipy and networkx and it worked for me.

pip install --upgrade scipy networkx
lyh458
  • 91
  • 1
  • 3
2

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

cocophelps
  • 21
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 12 '22 at 03:06
  • Indeed this does it. – ITA Mar 17 '22 at 14:22
0

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.

Hafez Ahmad
  • 175
  • 2
  • 7
Jayyu
  • 319
  • 2
  • 10
0

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
Hafez Ahmad
  • 175
  • 2
  • 7
0

These versions of newtorkx and scipy solved the error for me

%pip install 'networkx<2.7'

%pip install 'scipy>=1.8'

Carol
  • 21
  • 2