[Numpy-discussion] Tests for empty arrays

Timothy Hochberg tim.hochberg@ieee....
Tue May 6 12:03:38 CDT 2008


On Tue, May 6, 2008 at 9:53 AM, Keith Goodman <kwgoodman@gmail.com> wrote:

> On Tue, May 6, 2008 at 9:45 AM, Anne Archibald
> <peridot.faceted@gmail.com> wrote:
> >  In fact, if you want to use empty() down the road, it may
> >  make sense to initialize your array to zeros()/0., so that if you ever
> >  use the values, the NaNs will propagate and become obvious.
>
> Numpy has ones and zeros. Could we add a nans?
>
> I often initialize using x = nan * ones((n ,m)). But if it's in a
> loop, I'll avoid one copy by doing
>
> x = np.ones((n, m))
> x *= np.nan
>
> To many on the list using nans for missing values is like chewing gum
> you found on the sidewalk. But I use it all the time so I'd use a
> nans.


Why don't you just roll your own?

>>> def nans(shape, dtype=float):
...     a = np.empty(shape, dtype)
...     a.fill(np.nan)
...     return a
...
>>> nans([3,4])
array([[ NaN,  NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN,  NaN]])


-- 
. __
. |-\
.
. tim.hochberg@ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080506/19b6138d/attachment.html 


More information about the Numpy-discussion mailing list