简体   繁体   English

Plot 带有 numpy 数组错误的 NetworkX 图形

[英]Plot NetworkX Graph with numpy array error

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'我将邻接矩阵作为 numpy 数组,并希望将它作为使用 NetworkX 的简单无向图 plot,但我一直遇到此错误: 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.我正在关注这个: Plot 来自邻接矩阵的 NetworkX Graph in CSV 文件特定的答案并且无法让它工作。 The only difference is that my adjacency matrix is rather huge with around 30000 columns唯一的区别是我的邻接矩阵相当大,大约有 30000 列

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我的scipy版本是1.8.0

downgrade networkx to 2.6.3 and it solved this problem for me.将networkx降级到2.6.3,它为我解决了这个问题。

Upgrade both scipy and networkx and it worked for me.升级scipynetworkx ,它对我有用。

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).我查看了生成该错误的 scipy 文件(“convert_matrix.py”)第 921 行:A = sp.sparse.coo_array((d, (r, c)), shape=(nlen, nlen), dtype=dtype )。

You need to switch "coo_array" to "coo_matrix".您需要将“coo_array”切换为“coo_matrix”。

So it should look like: A = sp.sparse.coo_matrix((d, (r, c)), shape=(nlen, nlen), dtype=dtype)所以它应该看起来像: 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.在 Windows 系统上,它工作。 I guess it may be because previous versions of 'scipy.sparse' don't have 'coo_array'.我想这可能是因为以前版本的“scipy.sparse”没有“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.我还尝试使用 conda 将 scipy 更新到 1.8.1 无济于事。

I am also using vs code in windows 10. I got the same problem.我也在 windows 10 中使用 vs 代码。我遇到了同样的问题。 then I tried to upgrade scipy and networks separately but they didn't work for me.然后我尝试分别升级 scipy 和网络,但它们对我不起作用。 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.在cmd中,这段代码应该是这样的。 Try to open as admin.尝试以管理员身份打开。

pip install --upgrade scipy networkx

These versions of newtorkx and scipy solved the error for me这些版本的 newtorkx 和 scipy 为我解决了错误

%pip install .networkx<2.7'

%pip install 'scipy>=1.8'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM