[Numpy-discussion] Upgrading from numpy to

Travis Oliphant oliphant.travis at ieee.org
Tue Jun 27 13:50:05 CDT 2006

Stefan van der Walt wrote:
> On Tue, Jun 27, 2006 at 09:45:57AM -0700, Keith Goodman wrote:
> With r2691 I see
> In [7]: x = N.asmatrix(N.zeros((3,2)),float)
> In [8]: y = N.asmatrix(N.rand(3,1))
> In [12]: x[:,1] = y > 0.5
> In [13]: x
> Out[13]:
> matrix([[ 0.,  1.],
>        [ 0.,  1.],
>        [ 0.,  1.]])

This was a bug, indirectly caused by the move to broadcasted copying and 
casting and the use of a matrix here.  Previously the shapes didn't 
matter as long as the total size was the same. 

Thus internally x[:,1] was creating a (1,3) matrix referencing the last 
column of x (call it xp) and y>0.5  was a (3,1) matrix (call it yp)  
Thus the resulting casting code was repeatedly filling in x with 
(y>0.5).  Thus, the last entry of (y>0.5) was the one that resulted.

Previously, this would have worked because the shape of the arrays 
didn't matter, but now they do.  The real culprit was not allowing the 
matrices "getitem" method to be called (which would have correctly 
obtained a (3,1) matrix from x[:,1] and thus avoided the strange result.

Thus, in SVN, now PyObject_GetItem is used instead of the default 
ndarray getitem.   The upshot, is that this should now work --- and 
there is now a unit-test to check for it.

Thanks to Keith for exposing this bug.


More information about the Numpy-discussion mailing list