[SciPy-User] indexing array without changing the ndims

Gabriel Gellner ggellner@uoguelph...
Tue Jan 26 00:39:55 CST 2010


On Mon, Jan 25, 2010 at 10:08 PM, Warren Weckesser
<warren.weckesser@enthought.com> wrote:
> Gabriel Gellner wrote:
>> I really want an easy way to index an array but not have numpy
>> simplify the shape (if you know R I want their drop=FALSE behavior).
>>
>
> For those of us who aren't familiar with R, could you give a concrete
> example of what you want to do?
>
It would be the similar to what the numpy.matrix class does, namely
when you use an index like
`mat[0, :]` you still have ndims == 2 (a column matrix in this case).
So I want this behavior for an ndarray so I could be certain that if I
do any indexing the ndims of the returned array is the same as the
original array.

In R any array can be indexed with an extra keyword argument
drop=FALSE to give this behavior so for the above I would have
`mat[0, :, drop=False]` (in pretend python notation, in R we would
write mat[1,, drop=F]) and it would do the right thing. An even more
extreme example would be to do something like

`zeros((3, 3, 3))[0, 0, 0, drop=False]` (In R `array(0, c(3, 3, 3))[1,
1, 1, drop=F]`) which would return an array with shape == (1, 1, 1)
instead of ().
Now this drop notation is just for explanation, I know it is not
possible in python, but I was hoping their is some equivalent way of
getting this nice behavior. Looking at the matrix source code suggest
this is not the case and it needs to be coded by hand, I was hoping
this is not the case!

thanks,
Gabriel


More information about the SciPy-User mailing list