numpy magic: cast scalar returns auto to python types float & int ?

Tim Hochberg tim.hochberg at ieee.org
Fri Nov 17 08:21:49 EST 2006


robert wrote:
> Turning algs for old NumPy modules into numpy code I suffer from this:
> Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy types.
> First there is some inefficiency in calculations. And then you get data inflation and questionable dependencies - e.g. with pickle,ZODB,mpi's ... :
> 
> 
>>>> l=array((1.,0))
>>>> l.prod()
> 0.0
>>>> cPickle.dumps(_)
> "cnumpy.core.multiarray\nscalar\np1\n(cnumpy\ndtype\np2\n(S'f8'\nI0\nI1\ntRp3\n(I2\nS'<'\nNNNI-1\nI-1\ntbS'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\ntRp4\n."
>>>> cPickle.dumps(0.0)
> 'F0\n.'
>>>> l=array((1,0))
>>>> l.prod()
> 0
>>>> cPickle.dumps(_)
> "cnumpy.core.multiarray\nscalar\np1\n(cnumpy\ndtype\np2\n(S'i4'\nI0\nI1\ntRp3\n(I2\nS'<'\nNNNI-1\nI-1\ntbS'\\x00\\x00\\x00\\x00'\ntRp4\n."
>>>> cPickle.dumps(0)
> 'I0\n.'
>>>> type(l.prod())
> <type 'numpy.int32'>
> 
> 
> To avoid this you'd need a type cast in Python code everywhere you get scalars from numpy into a python variable. Error prone task. Or check/re-render your whole object tree.
> Wouldn't it be much better if numpy would return Python scalars for float64 (maybe even for float32) and int32, int64 ... where possible? (as numarray and Numeric did)
> I suppose numpy knows internally very quickly how to cast. 

The short answer is no, it would not be better. There are some trade 
offs involved here, but overall, always returning numpy scalars is a 
significant improvement over returning Python scalars some of the time. 
Which is why numpy does it that way now; it was a conscious choice, it 
didn't just happen.  Please search the archives of numpy-discussion for 
previous discussions of this and if that is not enlightening enough 
please ask at on the numpy-discussion list (the address of which just 
changed and I don't have it handy, but I'm sure you can find it).

For your particular issue, you might try tweaking pickle to convert 
int64 objects to int objects. Assuming of course that you have enough of 
these to matter, otherwise, I suggest just leaving things alone.

-tim




More information about the Python-list mailing list