[Numpy-discussion] DEP: Deprecate boolean array indices with non-matching shape #4353
Nathaniel Smith
njs at pobox.com
Thu Jun 4 21:04:35 EDT 2015
On Thu, Jun 4, 2015 at 5:57 PM, Nathaniel Smith <njs at pobox.com> wrote:
> So specifically the question is -- if you have an array with five items,
and
> a Boolean array with three items, then currently you can use the later to
> index the former:
>
> arr = np.arange(5)
> mask = np.asarray([True, False, True])
> arr[mask] # returns array([0, 2])
>
> This is justified by the rule that indexing with a Boolean array should be
> the same as indexing with the same array that's been passed to
np.nonzero().
> Empirically, though, this causes constant confusion and does not seen very
> useful, so the question is whether we should deprecate it.
One place where the current behavior is particularly baffling and annoying
is when you have multiple boolean masks in the same indexing operation. I
think everyone would expect this to index separately on each axis ("outer
product indexing" style, like slices do), and that's really the only useful
interpretation, but that's not what it does...:
In [3]: a = np.arange(9).reshape((3, 3))
In [4]: a
Out[4]:
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
In [6]: a[np.asarray([True, False, True]), np.asarray([False, True, True])]
Out[6]: array([1, 8])
In [7]: a[np.asarray([True, False, True]), np.asarray([False, False, True])]
Out[7]: array([2, 8])
In [8]: a[np.asarray([True, False, True]), np.asarray([True, True, True])]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-30b3427bec2a> in <module>()
----> 1 a[np.asarray([True, False, True]), np.asarray([True, True, True])]
IndexError: shape mismatch: indexing arrays could not be broadcast together
with shapes (2,) (3,)
-n
--
Nathaniel J. Smith -- http://vorpus.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150604/67727f3b/attachment.html>
More information about the NumPy-Discussion
mailing list