[SciPy-user] Random sparse matrices

Anne Archibald peridot.faceted at gmail.com
Fri Apr 25 16:41:17 EDT 2008


On 25/04/2008, Nils Wagner <nwagner at iam.uni-stuttgart.de> wrote:

>  I run your script several times. Sometimes I get
>  >>> sprandn(5,5,3).todense()
>  matrix([[ 0.        ,  0.        ,  0.        ,  0.
>        ,  0.        ],
>          [ 0.        ,  0.36548958,  0.        ,  0.
>        ,  0.        ],
>          [ 1.51125878,  0.        ,  0.        ,  0.
>        , -0.20285678],
>          [ 0.        ,  0.        ,  0.        ,  0.
>        ,  0.        ],
>          [ 0.        ,  0.        ,  0.        ,  0.
>        ,  0.        ]])
>  >>> sprandn(5,5,3).todense()
>  Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>    File "sprand.py", line 23, in sprandn
>      return
>  scipy.sparse.coo_matrix((np.random.randn(nnz),ij),(m,n))
>    File
>  "/usr/lib/python2.4/site-packages/scipy/sparse/coo.py",
>  line 180, in __init__
>      self._check()
>    File
>  "/usr/lib/python2.4/site-packages/scipy/sparse/coo.py",
>  line 213, in _check
>      raise ValueError, "row index exceedes matrix
>  dimensions"
>  ValueError: row index exceedes matrix dimensions

Strange. Works for me, even in a while loop, and with various values
of m,n, and nnz. Did numpy.random.random_integer change to including
the endpoint or something? A quick assert() ought to turn up whether
the random distinct integers really are all less than m*n.
Alternatively, perhaps there is some weirdness to do with the "//"
operator?

Serves me right for not writing proper unit tests.

Anne



More information about the SciPy-User mailing list