matshow与稀疏矩阵

Ale*_*mes 6 python matplotlib scipy

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

矩阵太大而不能作为密集阵列放在内存中,所以我用csr_matrix格式.当我尝试使用pylab的matshow时,我收到以下错误:

ValueError:需要多于0个值才能解压缩

思考?

例如:

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)
Run Code Online (Sandbox Code Playgroud)

Jai*_*ime 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述