[Numpy-discussion] question about subtraction and shape

Tony Yu tsyu80 at gmail.com
Thu Sep 1 17:44:49 EDT 2011


On Thu, Sep 1, 2011 at 5:33 PM, Jonas Wallin <jonas.wallin81 at gmail.com>wrote:

> Hello,
>
> I implemented the following line of code:
>
> Gami[~index0].shape ----> (100,)
> sigma.shape             ----> (1,1)
> Gami[~index0]  = Gam[~index0] - sigma**2
>
> I get the error message:
> *** ValueError: array is not broadcastable to correct shape
>
> apparently
> *temp* = Gam[~index0] - sigma**2
> *temp*.shape -->  (1,100)
>
> Which seems strange to me is there any reason why *temp* gets this shape ?
>

Jonas,

Numpy changes the shape of the output array based on certain broadcasting
rules (See http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html).
Broadcasting is really useful when you need it, but it can be confusing when
you don't.

Best,
-Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110901/3f66d898/attachment.html>


More information about the NumPy-Discussion mailing list