[Numpy-discussion] Logical indexing and higher-dimensional arrays.
Benjamin Root
ben.root at ou.edu
Tue Feb 7 13:17:02 EST 2012
On Tue, Feb 7, 2012 at 11:11 AM, Jordi Gutiérrez Hermoso <jordigh at octave.org
> 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]
Funny things do happen when you mix boolean indexing with slicing/fancy
indexing. This does seem strange to me:
>>> print x.shape
(2, 3, 4)
>>> print x[0, :, :].shape
(3, 4)
>>> print x[0, :, idx].shape
(2, 3)
# 1-D slices using regular indexing and slicing
>>> print x[0, :, 1]
[ 0.06500275 0.46899149 0.50125757]
>>> print x[0, :, 3]
[ 0.06500275 0.46899149 0.50125757]
# 2-D view using regular indexing, slicing and boolean indexing
>>> print x[0, :, idx]
[[ 0.06500275 0.46899149 0.50125757]
[ 0.68811907 0.94795054 0.86839934]]
# 2-D view using indexing and slicing
>>> print x[0, :, 1:4]
[[ 0.06500275 0.95042819 0.68811907]
[ 0.46899149 0.49388795 0.94795054]
[ 0.50125757 0.04363919 0.86839934]]
The 1-D views makes sense for them to be of shape (3,), but the 2-D view is
inconsistent with the last result. Could this be a bug related to how
boolean indexing tends to flatten the results? Stacking flattened arrays
would yield the second result.
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120207/975efac6/attachment.html>
More information about the NumPy-Discussion
mailing list