[Numpy-discussion] Broadcasting doesn't work with divide after tile

Bruce Southey bsouthey at gmail.com
Thu Feb 2 10:07:05 EST 2012


On 02/02/2012 02:52 AM, martin großhauser wrote:
> On Wed, Feb 1, 2012 at 10:57 PM, Pierre Haessig
> <pierre.haessig at crans.org>  wrote:
>> I've no idea what's going on, but here is my $0.02 contribution. I
>> reproduced the bug (numpy 1.5.1) with a rather minimal script. See attached.
> I reproduced the issue with Pierre's script also in numpy 1.6.1 and
> latest github (2.0.0.dev-b8bfcd0). In newer versions the error message
> is:
>
> Traceback (most recent call last):
>    File "ma_tiling_issue.py", line 18, in<module>
>      a/100. #raises ValueError: invalid return array shape
>    File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line
> 3654, in __div__
>      return divide(self, other)
>    File "/usr/local/lib/python2.7/dist-packages/numpy/ma/core.py", line
> 1078, in __call__
>      m |= ma
> ValueError: non-broadcastable output operand with shape (3,3,3)
> doesn't match the broadcast shape (1,3,3,3)
>
> I still don't know what's going on. Is the internal representation
> (shape) of the array changed by the tile instruction?
>
> I created a ticket: http://projects.scipy.org/numpy/ticket/2035
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
This is triggered when the array's mask is not 'False' and appears to be 
due to the call on line 827 of numpy/lib/shape_base.py:
c = _nx.array(A,copy=False,subok=True,ndmin=d)

where _nx is 'import numpy.core.numeric as _nx'. Also, setting 
'copy=True' in the call does not change anything.

You probably can get around it by passing a copy to np.tile():
sp4d = np.tile(a.copy(), (4, 1, 1, 1))

Bruce



More information about the NumPy-Discussion mailing list