[Numpy-discussion] Logical indexing and higher-dimensional arrays.

Travis Oliphant travis@continuum...
Tue Feb 7 16:51:37 CST 2012


This comes up from time to time.    This is an example of what is described at the top of page 84 of "Guide to NumPy".  Also read Chapter 17 to get the explanation of how fancy indexing is implemented if you really want to understand the issues. 

When you mix fancy-indexing with "simple indexing", the rules are not "special-cased" to handle this case of integer and masked indexing.  They really should be, since it is non-ambiguous.   There are other cases (say using two masked arrays in dimensions 1 and 3) where it's ambiguous what the dimension of the output should be and so the "punt" of moving the selected subspace (the one without the ':') to the front is used.

So, I would consider this a bug.     We will likely be re-visiting the situation of this kind of fancy indexing for NumPy 2.0.

Thanks,

-Travis



On Feb 7, 2012, at 11:11 AM, Jordi Gutiérrez Hermoso wrote:

> Consider the following. Is this a bug?
> 
> Thanks,
> - Jordi G. H.
> 
> -----------------------------------------------
> #!/usr/bin/python
> 
> import numpy as np
> 
> x = np.reshape(np.random.uniform(size=2*3*4), [2,3,4])
> 
> idx = np.array([False, True, False, True])
> y = x[0,:,:];
> 
> ## Why is this transposed?
> print x[0, :, idx].T == y[:, idx]
> 
> ## This doesn't happen with non-boolean indexing
> print x[0, :, 1:3] == y[:, 1:3]
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list