[Numpy-discussion] Indexing a 2-d array with a 1-d mask

Alok Singhal alok at merfinllc.com
Wed Feb 9 19:02:37 EST 2011


On Wed, Feb 9, 2011 at 1:24 AM, Friedrich Romstedt
<friedrichromstedt at gmail.com> wrote:
> 2011/2/8 Alok Singhal <alok at merfinllc.com>:
>> In [6]: data2 = numpy.zeros((0, 5), 'd')
>> In [7]: mask2 = numpy.zeros(0, 'bool')
>> In [8]: data2[mask2]
>> ------------------------------------------------------------
>> Traceback (most recent call last):
>>  File "<ipython console>", line 1, in <module>
>> IndexError: invalid index
>>
>> I would have expected the above to give me a 0x5 array.
>
>> Is there any other way to do what I
>> am doing?
>
> Like so (works only for ndim==2):
>
>>>> d1 = numpy.arange(0).reshape((0, 5))
>>>> d2 = numpy.arange(15).reshape((3, 5))
>>>> index1 = numpy.asarray([], dtype=numpy.bool)
>>>> index2 = numpy.asarray([True, False, True], dtype=numpy.bool)
>>>> x = numpy.arange(5)
>>>> (x1, y1) = numpy.meshgrid(x, index1.nonzero()[0])
>>>> (x2, y2) = numpy.meshgrid(x, index2.nonzero()[0])
>>>> (x1, y1)
> (array([], shape=(0, 5), dtype=int32), array([], shape=(0, 5), dtype=int32))
>>>> print x2, "\n", y2
> [[0 1 2 3 4]
>  [0 1 2 3 4]]
> [[0 0 0 0 0]
>  [2 2 2 2 2]]
>>>> d1[y1, x1]
> array([], shape=(0, 5), dtype=int32)
>>>> d2[y1, x1]
> array([], shape=(0, 5), dtype=int32)
>>>> d2[y2, x2]
> array([[ 0,  1,  2,  3,  4],
>       [10, 11, 12, 13, 14]])

Yeah, I can do it with creating the full index array, but I have huge
data sets, so I was hoping to avoid them.  For now, I can just check
for the borderline case and keep using the memory-efficient indexing
for the "regular" cases.

> I don't know if the other thing is a bug, but it looks like.  I could
> imagine that it has something to do with the implicit slicing on the
> array without data?  Rather an imperfection ...
>
> Consider this:
>
>>>> d1 = numpy.arange(0).reshape((0,))
>>>> d2 = numpy.arange(0).reshape((0, 5))
>>>> d3 = numpy.arange(0).reshape((5, 0))
>>>> d1[[]]
> array([], dtype=int32)
>>>> d2[[]]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> IndexError: invalid index
>>>> d2[[], 0]
> array([], dtype=int32)
>>>> d3[[]]
> array([], shape=(0, 0), dtype=int32)
>>>> d3[0, []]
> array([], dtype=int32)
>>>> d3[:, []]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> IndexError: invalid index
>
> Ticket?

I think so too.  Although, I don't know if this behavior is a feature
of advanced indexing (and not a bug).

Thanks,
Alok




More information about the NumPy-Discussion mailing list