[Numpy-discussion] seting the dtype for where...
Travis Oliphant
oliphant.travis at ieee.org
Fri Dec 1 15:33:45 EST 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.
>
> How can I get a float32 array? where doesn't take a dtype argument --
> maybe it should?
>
You need to do
N.where(a >= 5, N.float32(5), N.float32(0))
The rules are the same as for ufuncs:
The returned array for mixed-type operations uses the "largest" type
unless one is a scalar and one is an array (then the scalar is ignored
unless the "kind" is different). In this case, you have two scalars (a
0-d array is considered a scalar in this context.
-Travis
More information about the NumPy-Discussion
mailing list