MATLAB-spy的scipy等价性

2024-05-13 11:36:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在把isomap算法的代码从MATLAB移植到Python。我试着用间谍功能来可视化稀疏模式。

MATLAB命令:

spy(sparse(A));
drawnow;

Python命令:

matplotlib.pyplot.spy(scipy.sparse.csr_matrix(A))
plt.show()

我无法使用上面的命令在Python中复制MATLAB结果。只使用非稀疏格式的命令,得到与MATLAB非常相似的结果。但这需要相当长的时间(2000年到2000年)。对于scipy,什么样的MATLAB等价于稀疏函数?


Tags: 代码命令功能算法matplotlib可视化模式scipy
3条回答

使用较小的标记:

import matplotlib.pylab as pl
import scipy.sparse as sps
import scipy.io
import sys
A=scipy.io.mmread(sys.argv[1])
pl.spy(A,precision=0.01, markersize=1)
pl.show()

也许是你的matplotlib版本惹了麻烦,对我来说scipy.sparsematplotlib.pylab工作得很好。

请参阅下面生成“间谍”图的示例代码。

import matplotlib.pylab as plt
import scipy.sparse as sps
A = sps.rand(10000,10000, density=0.00001)
M = sps.csr_matrix(A)
plt.spy(M)
plt.show()

# Returns here '1.3.0'
matplotlib.__version__

这给出了这个图:enter image description here

我刚刚发布了betterspy,可以说它在这里做得更好。安装时使用

pip3 install betterspy --user

一起跑

import betterspy
from scipy import sparse

A = sparse.rand(20, 20, density=0.1)
betterspy.show(A)
betterspy.write_png("out.png", A)

enter image description here

相关问题 更多 >