[NumPy-Tickets] [NumPy] #1934: Improve assignment error message when output can broadcast to the input
NumPy Trac
numpy-tickets@scipy....
Mon Aug 15 15:50:36 CDT 2011
#1934: Improve assignment error message when output can broadcast to the input
-------------------------+--------------------------------------------------
Reporter: mwiebe | Owner: somebody
Type: enhancement | Status: new
Priority: normal | Milestone: 2.0.0
Component: Other | Version: 1.6.0
Keywords: |
-------------------------+--------------------------------------------------
The code
{{{
a = numpy.zeros((5,5))
v = numpy.ones((5,1))
a[:,3] = v
}}}
raises the exception
{{{
ValueError?: output operand requires a reduction, but reduction is not
enabled
}}}
which is not very enlightening as to the nature of the error. This
illustrates why it errors:
{{{
>>> a = numpy.zeros((5,5))
>>> v = numpy.ones((5,1))
>>> a[:,3]
array([ 0., 0., 0., 0., 0.])
>>> v
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
>>> a[:,3].shape
(5,)
>>> v.shape
(5, 1)
}}}
The (5,) and (5,1) get broadcast to a (5,5) shape, which is what is really
then being assigned into the (5,) array. To fix it, write the assignment
like a[:,3] = v[:,0].
Example taken from ticket #1870 comments.
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1934>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list