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

Doing the weighted-sum explicitly works fine for me:

In [311]: sum(a*ones(10), axis=-1)/sum(ones(10))
Out[311]: array([  4.5,  14.5,  24.5,  34.5,  44.5,  54.5,  64.5,  74.5,  
84.5,  94.5])

This seems like a bug, especially since average.__doc__ states that:
    If weights are given, result is:
        sum(a * weights,axis) / sum(weights,axis),
    where the weights must have a's shape or be 1D with length the
    size of a in the given axis. Integer weights are converted to
    Float.  Not specifying weights is equivalent to specifying
    weights that are all 1.

Frankly, I don't even see why weights is constrained to be 1D or the same 
shape as a... why not anything that's broadcastable to the same shape as a?

Dan

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to