From: Stefan v. d. W. <st...@su...> - 2006-09-22 08:22:26
|
On Fri, Sep 22, 2006 at 02:17:57AM -0500, Robert Kern wrote: > Stefan van der Walt wrote: > > Hi P., > >=20 > > On Thu, Sep 21, 2006 at 07:40:39PM -0400, PGM wrote: > >=20 > >> I'm running into the following problem with putmask on take. > >> > >>>>> import numpy > >>>>> x =3D N.arange(12.) > >>>>> m =3D [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1] > >>>>> i =3D N.nonzero(m)[0] > >>>>> w =3D N.array([-1, -2, -3, -4.]) > >>>>> x.putmask(w,m) > >>>>> x.take(i) > >>>>> N.allclose(x.take(i),w) > >=20 > > According to the putmask docstring: > >=20 > > a.putmask(values, mask) sets a.flat[n] =3D v[n] for each n where > > mask.flat[n] is true. v can be scalar. > >=20 > > This would mean that 'w' is not of the right length.=20 >=20 > There are 4 true values in m and 4 values in w. What's the wrong length? The way I read the docstring, you use putmask like this: In [4]: x =3D N.array([1,2,3,4]) In [5]: x.putmask([4,3,2,1],[1,0,0,1]) In [6]: x Out[6]: array([4, 2, 3, 1]) > For the sake of clarity: >=20 >=20 > In [1]: from numpy import * >=20 > In [3]: m =3D [1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1] >=20 > In [4]: i =3D nonzero(m)[0] >=20 > In [5]: i > Out[5]: array([ 0, 6, 9, 11]) >=20 > In [6]: w =3D array([-1, -2, -3, -4.]) >=20 > In [7]: x =3D arange(12.) >=20 > In [8]: x.putmask(w, m) >=20 > In [9]: x > Out[9]: > array([ -1., 1., 2., 3., 4., 5., -3., 7., 8., -2., 10.= , > -4.]) >=20 > In [17]: x[array(m, dtype=3Dbool)] =3D w >=20 > In [18]: x > Out[18]: > array([ -1., 1., 2., 3., 4., 5., -2., 7., 8., -3., 10.= , > -4.]) >=20 >=20 > Out[9] and Out[18] should have been the same, but elements 6 and 9 are = flipped.=20 > It's pretty clear that this is a bug in .putmask(). Based purely on what I read in the docstring, I would expect the above to= do x[0] =3D w[0] x[6] =3D w[6] x[9] =3D w[9] x[11] =3D w[11] Since w is of length 4, you'll probably get indices modulo 4: w[6] =3D=3D w[2] =3D=3D -3 w[9] =3D=3D w[1] =3D=3D -2 w[11] =3D=3D w[3] =3D=3D -4 Which seems to explain what you are seeing. Regards St=E9fan |