***[Possible UCE]*** Re: [Numpy-discussion] Broadcasting rules (Ticket 76).

Travis Oliphant oliphant.travis at ieee.org
Mon Apr 24 11:25:06 CDT 2006

Zachary Pincus wrote:
>> It would be very difficult to change the other broadcasting behavior 
>> which was inherited from Numeric, however.  The only possibility I 
>> see is adding new useful functionality where Numeric used to raise an 
>> error.
> Well, there is one case that I run into all of the time where the 
> broadcasting rules seem a bit constraining:
> In [1]: import numpy
> In [2]: numpy.__version__
> ''
> In [3]: a = numpy.ones([50, 100])
> In [4]: means = a.mean(axis = 1)
> In [5]: print a.shape, means.shape
> (50, 100) (50,)
> In [5]: a / means
> ValueError: index objects are not broadcastable to a single shape
> In [6]: (a.transpose() / means).transpose()
> #this works
> It's obvious why this doesn't work due to the broadcasting rules, but 
> it also seems (to me, in this case at least) obvious what I am trying 
> to do. I don't think I'm suggesting that the broadcasting rules be 
> changed to allow matching-from-the-right in the general case, since 
> that seems likely to make the broadcasting rules even more difficult 
> to grok. But there do seem to be a lot of (....transpose() ... 
> ).transpose() bits in my code.
> Is there anything to be done here? I presume not, but I just wanted to 
> mention it.

Yes,  just be more explicit about which end to tack extra dimensions 
onto (the automatic extension always assumes pre-pending...)

a / means[:,newaxis]

is the suggested spelling...


More information about the Numpy-discussion mailing list