argpartsort upcasts 32-bit to 64-bit

11 views
Skip to first unread message

dbv

unread,
Jul 11, 2014, 3:50:02 PM7/11/14
to bottl...@googlegroups.com
In a 64-bit (linux) OS, if the numpy/scipy arrays are defined as 32-bit objects then the outputs are 32-bit.  However, argpartsort returns a data object of 64-bit indices.

In a 32-bit (windows) OS, argpartsort returns a data object of 32-bit indices which is correct.

Keith Goodman

unread,
Jul 11, 2014, 4:15:59 PM7/11/14
to bottl...@googlegroups.com
On Fri, Jul 11, 2014 at 12:50 PM, dbv <dinesh...@hotmail.com> wrote:
In a 64-bit (linux) OS, if the numpy/scipy arrays are defined as 32-bit objects then the outputs are 32-bit.  However, argpartsort returns a data object of 64-bit indices.

In a 32-bit (windows) OS, argpartsort returns a data object of 32-bit indices which is correct.

Yes,

In [1]: a = np.arange(3, dtype=np.int32)
In [2]: bn.argpartsort(a, 2)
Out[2]: array([0, 1, 2])  # 64 bit

but numpy does the same

In [3]: np.argsort(a)
Out[3]: array([0, 1, 2])  # 64 bit

and the goal is to match numpy. BTW the examples above are on 64-bit linux.



dbv

unread,
Jul 11, 2014, 5:45:26 PM7/11/14
to bottl...@googlegroups.com
See output below from matrix multiplication on 64-bit OS:
 
>>> import numpy
>>> from scipy import sparse
>>> import bottleneck
 
>>> row = numpy.array([0,0,1,2,2,2], dtype=numpy.int32)
>>> col = numpy.array([0,2,2,0,1,2], dtype=numpy.int32)
>>> data = numpy.array([1,2,3,4,5,6], dtype=numpy.int32)
 
>>> A = sparse.csr_matrix((data,(row,col)), shape=(3,3), dtype=numpy.float32)
>>> x = numpy.array([8.0, 4.0, 2.0], dtype=numpy.float32)
>>> b = A * x.T
 
>>> b
array([ 12.,   6.,  64.], dtype=float32)
 
>>> c = bottleneck.argpartsort(b, 2)
>>> c
array([1, 0, 2])
>>> type(c[0])
<type 'numpy.int64'>
 
>>> d = numpy.argsort(b)
>>> d
array([1, 0, 2])
>>> type(d[0])
<type 'numpy.int64'>

The output vector 'b' is a float32 as it should be and an argsort should return an int32 otherwise it is a mismatch of data types.  But, argpartsort (and numpy argsort) return int64 which doesn't seem right.  Yes/no?

Keith Goodman

unread,
Jul 11, 2014, 5:52:51 PM7/11/14
to bottl...@googlegroups.com
The default numpy int type for your system (np.intp) is returned. I don't see why the type of b should change anything. If you disagree ask on the numpy list.

Dougal Sutherland

unread,
Jul 11, 2014, 5:59:14 PM7/11/14
to bottl...@googlegroups.com
argsort returns indices; it doesn't really make sense (IMO) for the type of the index to depend on the type of the data.

It would be nice to support shorter return types in some cases (since you know the max value beforehand based on the type of the array), but see discussion of that here: https://github.com/numpy/numpy/issues/2989.


--
You received this message because you are subscribed to the Google Groups "bottle-neck" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bottle-neck...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dbv

unread,
Jul 11, 2014, 6:44:22 PM7/11/14
to bottl...@googlegroups.com
Thanks for the pointer to the interesting discussion.
Reply all
Reply to author
Forward
0 new messages