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

4
  • 1
    can you show us your code with sample data like for 10 columns? Commented Mar 5, 2022 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 Commented Mar 5, 2022 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 Commented Mar 6, 2022 at 4:05
  • @Riley I have updated with my version Commented Mar 6, 2022 at 4:06

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
Thanks for your comment. I editted my post. Again, thanks a lot.
9

Upgrade both scipy and networkx and it worked for me.

pip install --upgrade scipy networkx

Comments

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

2 Comments

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.
Indeed this does it.
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

Comments

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.

Comments

0

These versions of newtorkx and scipy solved the error for me

%pip install 'networkx<2.7'

%pip install 'scipy>=1.8'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.