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

Alok Singhal alok at merfinllc.com
Tue Feb 8 17:54:35 EST 2011


Hi,

I have an NxM array, which I am indexing with a 1-d, length N boolean
array.  For example, with a 3x5 array:

In [1]: import numpy
In [2]: data = numpy.arange(15)
In [3]: data.shape = 3, 5

Now, I want to select rows 0 and 2, so I can do:

In [4]: mask = numpy.array([True, False, True])
In [5]: data[mask]
Out[5]:
array([[ 0,  1,  2,  3,  4],
      [10, 11, 12, 13, 14]])

But when the shape of 'data' is a 0xM, this indexing fails:

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.

Of course, I can check on "len(data)" and not use the above indexing
when it is zero, but I am hoping that I don't need to special case the
boundary condition and have numpy fancy indexing do the "right thing"
always.  Is this a bug in numpy?  Is there any other way to do what I
am doing?

Here is my numpy setup (numpy installed from the git repository):

In [1]: import numpy
In [2]: numpy.__version__
Out[2]: '1.6.0.dev-13c83fd'
In [3]: numpy.show_config()
blas_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib']
   language = f77
lapack_info:
   libraries = ['lapack']
   library_dirs = ['/usr/lib']
   language = f77
atlas_threads_info:
 NOT AVAILABLE
blas_opt_info:
   libraries = ['blas']
   library_dirs = ['/usr/lib']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]
atlas_blas_threads_info:
 NOT AVAILABLE
lapack_opt_info:
   libraries = ['lapack', 'blas']
   library_dirs = ['/usr/lib']
   language = f77
   define_macros = [('NO_ATLAS_INFO', 1)]
atlas_info:
 NOT AVAILABLE
lapack_mkl_info:
 NOT AVAILABLE
blas_mkl_info:
 NOT AVAILABLE
atlas_blas_info:
 NOT AVAILABLE
mkl_info:
 NOT AVAILABLE
In [4]: import sys
In [5]: print sys.version
2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3]

Thanks!




More information about the NumPy-Discussion mailing list