[Numpy-discussion] weights parameter of np.average() doesn't work (BUG?)

Charles R Harris charlesr.harris@gmail....
Mon Aug 25 00:44:28 CDT 2008


On Sun, Aug 24, 2008 at 9:56 PM, Daniel Lenski <dlenski@gmail.com> wrote:

> On Sun, 24 Aug 2008 20:57:43 -0600, Charles R Harris wrote:
> On Sun, Aug 24, 2008 at 8:03 PM, Dan Lenski <dlenski@gmail.com> wrote:
> > This has been fixed in later versions:
> >
> > In [2]: a=arange(100).reshape(10,10)
> >
> > In [3]: average(a, axis=1, weights=ones(10)) Out[3]: array([  4.5,
> > 14.5,  24.5,  34.5,  44.5,  54.5,  64.5,  74.5, 84.5,  94.5])
>
> Ah, good to know!  I guess it's time to upgrade.
>
> > However, broadcasting in the axis=None case is not allowed. The thinking
> > is that the weights must be fully specified for the axis (or None) that
> > is being averaged over.
>
> Hmm, that's a pity :-(
>
> My real-world use case has a 2D array of weights, shape (a,b), that I'd
> like to broadcast to a 3D array, shape (n,a,b), which I average over the
> first dimension.  Currently, I just use repeat() to make it the right
> size... but this is less-than-ideal for huge arrays.
>

I expect you can do this explicitly, which is pretty much what average does
anyway. Average is pure python, so you won't be losing any efficiency. If
you were a bit clearer with your use case I could be more explicit.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080824/eebc4df6/attachment.html 


More information about the Numpy-discussion mailing list