[Numpy-discussion] numpy.filled, again

Benjamin Root ben.root at ou.edu
Fri Jun 14 13:28:30 EDT 2013


On Fri, Jun 14, 2013 at 1:21 PM, Robert Kern <robert.kern at gmail.com> wrote:

> On Fri, Jun 14, 2013 at 6:18 PM, Eric Firing <efiring at hawaii.edu> wrote:
> > On 2013/06/14 5:15 AM, Alan G Isaac wrote:
> >> On 6/14/2013 9:27 AM, Aldcroft, Thomas wrote:
> >>> If I just saw np.values(..) in some code I would never guess what it
> is doing from the name
> >>
> >> That suggests np.fromvalues.
> >> But more important than the name I think
> >> is allowing broadcasting of the values,
> >> based on NumPy's broadcasting rules.
> >> Broadcasting a scalar is then a special case,
> >> even if it is the case that has dominated this thread.
> >
> > True, but this looks to me like mission creep.  All of this fuss is
> > about replacing two lines of user code with a single line.  If it can't
> > be kept simple, both in implementation and in documentation, it
> > shouldn't be done at all.  I'm not necessarily opposed to your
> > suggestion, but I'm skeptical.
>
> It's another two-liner:
>
> [~]
> |1> x = np.empty([3,4,5])
>
> [~]
> |2> x[...] = np.arange(5)
>
> [~]
> |3> x
> array([[[ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.]],
>
>        [[ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.]],
>
>        [[ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.],
>         [ 0.,  1.,  2.,  3.,  4.]]])
>
> It's wafer-thin!
>
>
True, but wouldn't we rather want to encourage the use of broadcasting in
the numerical operations rather than creating new arrays from broadcasted
arrays?

a = np.arange(5) + np.ones((3, 4, 5))
b = np.filled((3, 4, 5), np.arange(5)) + np.ones((3, 4, 5))

The first one is much easier to read, and is more efficient than the second
(theoretical) one because it needs to create two (3, 4, 5) arrays rather
than just one.  That being said, one could make a similar argument against
ones(), zeros(), etc.

Cheers!
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130614/677488b4/attachment.html>


More information about the NumPy-Discussion mailing list