[Numpy-discussion] seting the dtype for where...
Robert Kern
robert.kern at gmail.com
Fri Dec 1 15:04:34 CST 2006
Chris Barker wrote:
> Hi all,
>
> I'd like to set the data type for what numpy.where creates. For example:
>
> import numpy as N
>
> N.where(a >= 5, 5, 0)
>
> creates an integer array, which makes sense.
>
> N.where(a >= 5, 5.0, 0)
>
> creates a float64 array, which also makes sense, but I'd like a float32
> array, so I tried:
>
> N.where(a >= 5, array(5.0, dtype=N.float32), 0)
>
> but I got a float64 array again.
Well, it's consistent with all of the other coercion rules:
In [6]: (array(5.0, dtype=float32) + 0).dtype
Out[6]: dtype('float64')
float64 is the lowest floating point dtype that can hold the full range of int32
values (much less int64) without losing precision. Since both operands
("coercands"?) are scalars, they both get a say in the final dtype (unlike a
full array being coerced together with a scalar; only the array gets a say).
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list