Re: [Numpy-discussion] Indexing a 2-d array with a 1-d mask
Hi, 2011/2/8 Alok Singhal a...@merfinllc.com: 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. 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]]) 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? Friedrich ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] Indexing a 2-d array with a 1-d mask
On Wed, Feb 9, 2011 at 1:24 AM, Friedrich Romstedt friedrichromst...@gmail.com wrote: 2011/2/8 Alok Singhal a...@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 ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
[Numpy-discussion] Indexing a 2-d array with a 1-d mask
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! ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion