[SciPy-dev] Feature request : Comparison of sparse matrices is not implemented.

Robert Kern rkern at ucsd.edu
Thu Dec 2 05:19:56 CST 2004


Nils Wagner wrote:
> Hi all,
> 
> I tried to visualize the structure of large and sparse matrices using
> 
> from matplotlib.colors import LinearSegmentedColormap
> from matplotlib.matlab import *
> from scipy import *
> import IPython
> 
> def spy2(Z):
>    """
>    SPY(Z) plots the sparsity pattern of the matrix S as an image
>    """
> 
>    #binary colormap min white, max black
>    cmapdata = {
>         'red'  :  ((0., 1., 1.), (1., 0., 0.)),
>         'green':  ((0., 1., 1.), (1., 0., 0.)),
>         'blue' :  ((0., 1., 1.), (1., 0., 0.))
>         }
>    binary =  LinearSegmentedColormap('binary',  cmapdata, 2)
> 
>    Z = where(Z>0,1.,0.)
>    imshow(transpose(Z), interpolation='nearest', cmap=binary)

Since imshow() doesn't deal with sparse matrices (to my knowledge), but 
only dense matrices, you need to use Z.todense() regardless.

   Z = Z.transp().todense() > 0
   imshow(Z, ...)

If you really need the array to be Float, you can explicitly cast it. 
Otherwise, where(<condition>, 1.0, 0.0) is extraneous.

I'm sure sparse matrix comparisons are already "on the list," as it were.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Scipy-dev mailing list