[SciPy-User] overflow in .sum() of dtype bool sparse matrix

Ralf Gommers ralf.gommers@gmail....
Sat Nov 3 12:49:08 CDT 2012


On Tue, Oct 30, 2012 at 2:38 PM, Yaroslav Halchenko <lists@onerussian.com>wrote:

> I wonder if that is somehow considered a feature and manual casting is
> generally advised in such cases:  calling .sum on a bool matrix can easily
> lead
> to overflows causing bogus results (works fine on ndarrays):
>
> % git describe --tags
> v0.4.3-6232-g43c7982
>
> % PYTHONPATH=$PWD ../demo-scipy-sparse-negativesoverflow.py
> summing 128 booleans in <type 'numpy.ndarray'> leads to answer [128]
> summing 128 booleans in <class 'scipy.sparse.csc.csc_matrix'> leads to
> answer [[-128]]
>
> % cat ../demo-scipy-sparse-negativesoverflow.py
> #!/usr/bin/python
>
> import numpy as np
> import scipy.sparse as sp
> test = np.random.rand(128, 1)
> test_m= sp.csc_matrix(test)
>
> for t in test, test_m:
>     test_bool=t.astype('bool')
>     sum = test_bool.sum(axis=0)
>     print "summing %d booleans in %s leads to answer %s" \
>            % (t.shape[0], test_bool.__class__, sum)
>

Boolean sparse matrices are apparently not supported:

In [37]: sparse.sputils.supported_dtypes
Out[37]:
[numpy.int8,
 numpy.uint8,
 numpy.int16,
 numpy.uint16,
 numpy.int32,
 numpy.uint32,
 numpy.int64,
 numpy.uint64,
 numpy.float32,
 numpy.float64,
 numpy.float96,
 numpy.complex64,
 numpy.complex128,
 numpy.complex192]

In [38]: x = sp.csc_matrix(np.random.rand(3, 1)).astype(np.bool)

In [39]: x.tocsr()
Out[39]:
<3x1 sparse matrix of type '<type 'numpy.int8'>'
        with 3 stored elements in Compressed Sparse Row format>


Does anyone know why?

Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20121103/6a91c9bb/attachment.html 


More information about the SciPy-User mailing list