本文介绍了等同于MATLAB spy的scipy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在将isomap算法的代码从MATLAB移植到Python.我正在尝试使用间谍功能可视化稀疏模式.

I have been porting code for an isomap algorithm from MATLAB to Python. I am trying to visualize the sparsity pattern using the spy function.

MATLAB命令:

spy(sparse(A));
drawnow;

Python命令:

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

我无法使用上述命令在Python中重现MATLAB结果.仅对非稀疏格式的A使用命令会产生与MATLAB相当相似的结果.但这花费了相当长的时间(A是2000乘2000).稀疏函数对scipy的MATLAB等效性是什么?

I am not able to reproduce the MATLAB result in Python using the above command. Using the command with only A in non-sparse format gives quite similar result to MATLAB. But it's taking quite long (A being 2000-by-2000). What would be the MATLAB equivalent of a sparse function for scipy?

推荐答案

也许是您的matplotlib版本造成了麻烦,对我来说scipy.sparsematplotlib.pylab可以很好地协同工作.

Maybe it's your version of matplotlib that makes trouble, as for me scipy.sparse and matplotlib.pylab work well together.

请参见下面的示例代码,该代码会生成附加的间谍"图.

See sample code below that produces the 'spy' plot attached.

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__

这给出了以下图:

这篇关于等同于MATLAB spy的scipy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:58