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

Charles R Harris charlesr.harris@gmail....
Sun Aug 24 21:57:43 CDT 2008


On Sun, Aug 24, 2008 at 8:03 PM, Dan Lenski <dlenski@gmail.com> wrote:

> Hi all,
> Is there a good reason why the weights parameter of np.average() doesn't
> broadcast properly?  This is with the Ubuntu Hardy x86_64 numpy package,
> version 1.0.4.
>
>
> In [293]: a=arange(100).reshape(10,10)
>
> # Things work fine when weights have the exact same shape as a
>
> In [297]: average(a, axis=1, weights=ones((10,10)))
> Out[297]: array([  4.5,  14.5,  24.5,  34.5,  44.5,  54.5,  64.5,  74.5,
> 84.5,  94.5])
>
> # Bizarre and incorrect result with length-10 weight array
>
> In [298]: average(a, axis=1, weights=ones(10))
> Out[298]:
> array([[[[[[[[[  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.],
>              [ 10.,  11.,  12.,  13.,  14.,  15.,  16.,  17.,  18.,  19.],
>              [ 20.,  21.,  22.,  23.,  24.,  25.,  26.,  27.,  28.,  29.],
>              [ 30.,  31.,  32.,  33.,  34.,  35.,  36.,  37.,  38.,  39.],
>              [ 40.,  41.,  42.,  43.,  44.,  45.,  46.,  47.,  48.,  49.],
>              [ 50.,  51.,  52.,  53.,  54.,  55.,  56.,  57.,  58.,  59.],
>              [ 60.,  61.,  62.,  63.,  64.,  65.,  66.,  67.,  68.,  69.],
>              [ 70.,  71.,  72.,  73.,  74.,  75.,  76.,  77.,  78.,  79.],
>              [ 80.,  81.,  82.,  83.,  84.,  85.,  86.,  87.,  88.,  89.],
>              [ 90.,  91.,  92.,  93.,  94.,  95.,  96.,  97.,  98.,  99.]
>       ]]]]]]]])
>

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])

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.

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


More information about the Numpy-discussion mailing list