[Numpy-discussion] New numpy functions: filled, filled_like

Eric Firing efiring at hawaii.edu
Sun Jan 13 18:02:01 EST 2013


On 2013/01/13 7:27 AM, Nathaniel Smith wrote:
> Hi all,
>
> PR 2875 adds two new functions, that generalize zeros(), ones(),
> zeros_like(), ones_like(), by simply taking an arbitrary fill value:
>    https://github.com/numpy/numpy/pull/2875
> So
>    np.ones((10, 10))
> is the same as
>    np.filled((10, 10), 1)
>
> The implementations are trivial, but the API seems useful because it
> provides an idiomatic way of efficiently creating an array full of
> inf, or nan, or None, whatever funny value you need. All the
> alternatives are either inefficient (np.ones(...) * np.inf) or
> cumbersome (a = np.empty(...); a.fill(...)). Or so it seems to me. But
> there's a question of taste here; one could argue instead that these
> just add more clutter to the numpy namespace. So, before we merge,
> anyone want to chime in?

I'm neutral to negative as to whether it is worth adding these to the 
namespace; I don't mind using the "cumbersome" alternative.

Note also that there is already a numpy.ma.filled() function for quite a 
different purpose, so putting a filled() in numpy breaks the pattern 
that ma has masked versions of most numpy functions.

This consideration actually tips me quite a bit toward the negative 
side.  I don't think I am unique in relying heavily on masked arrays.

>
> (Bonus, extra bike-sheddy survey: do people prefer
>    np.filled((10, 10), np.nan)
>    np.filled_like(my_arr, np.nan)

+1 for this form if you decide to do it despite the problem mentioned above.

> or
>    np.filled(np.nan, (10, 10))
>    np.filled_like(np.nan, my_arr)

This one is particularly bad for filled_like, therefore bad for both.

Eric

> ?)
>
> -n
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>




More information about the NumPy-Discussion mailing list