matshow与稀疏矩阵

时间:2013-09-11 18:44:47

标签: python matplotlib scipy

如何可视化大型稀疏矩阵的稀疏模式?

矩阵太大,无法作为密集阵列存储在内存中,所以我用csr_matrix格式。当我用它尝试pylab的matshow时,我收到以下错误:

ValueError:需要超过0个值才能解压缩

思想?

e.g:

import pylab as pl
import scipy.sparse as sp
from random import randint

mat = sp.lil_matrix( (4000,3000), dtype='uint8' )
for i in range(1000):
    mat[randint(0,4000),randint(0,3000)] = randint(0,10)

pl.figure()
pl.matshow(mat)

1 个答案:

答案 0 :(得分:9)

matshow适用于密集阵列。对于稀疏数组,您可以使用spy

import scipy.sparse as sps
import matplotlib.pyplot as plt

a = sps.rand(1000, 1000, density=0.001, format='csr')

plt.spy(a)
plt.show()

enter image description here

相关问题