[Numpy-discussion] indexing bug in numpy r2694

Keith Goodman kwgoodman at gmail.com
Wed Jun 28 14:23:36 CDT 2006


On 6/28/06, Travis Oliphant <oliphant at ee.byu.edu> wrote:
> Keith Goodman wrote:
>
> >On 6/28/06, Pau Gargallo <pau.gargallo at gmail.com> wrote:
> >
> >
> >>i don't know why 'where' is returning matrices.
> >>if you use:
> >>
> >>
> >>
> >>>>>idx = where(y.A > 0.5)[0]
> >>>>>
> >>>>>
> >>everything will work fine (I guess)
> >>
> >>
> >
> >What about the second issue? Is this expected behavior?
> >
> >
> >
> >>>idx
> >>>
> >>>
> >array([0, 1, 2])
> >
> >
> >
> >>>y
> >>>
> >>>
> >
> >matrix([[ 0.63731308],
> >       [ 0.34282663],
> >       [ 0.53366791]])
> >
> >
> >
> >>>y[idx]
> >>>
> >>>
> >
> >matrix([[ 0.63731308],
> >       [ 0.34282663],
> >       [ 0.53366791]])
> >
> >
> >
> >>>y[idx,0]
> >>>
> >>>
> >matrix([[ 0.63731308,  0.34282663,  0.53366791]])
> >
> >I was expecting a column vector.
> >
> >
> >
> This should be better behaved now in SVN.  Thanks for the reports.

Now numpy can do

y[y > 0.5]

instead of

y[where(y.A > 0.5)[0]]

where, for example, y = asmatrix(rand(3,1)).

I know I'm pushing my luck here. But one more feature would make this perfect.

Currently y[y>0.5,:] returns the first column even if y has more than
one column. Returning all columns would make it perfect.

Example:

>> y

matrix([[ 0.38828902,  0.91649964],
       [ 0.41074001,  0.7105919 ],
       [ 0.15460833,  0.16746956]])

>> y[y[:,1]>0.5,:]

matrix([[ 0.38828902],
       [ 0.41074001]])

A better answer for matrix users would be:

>> y[(0,1),:]

matrix([[ 0.38828902,  0.91649964],
       [ 0.41074001,  0.7105919 ]])




More information about the Numpy-discussion mailing list