[Numpy-discussion] DEP: Deprecate boolean array indices with non-matching shape #4353
josef.pktd at gmail.com
josef.pktd at gmail.com
Fri Jun 5 12:57:10 EDT 2015
On Fri, Jun 5, 2015 at 11:50 AM, Anne Archibald <archibald at astron.nl> wrote:
>
>
> On Fri, Jun 5, 2015 at 5:45 PM Sebastian Berg <sebastian at sipsolutions.net>
> wrote:
>
>> On Fr, 2015-06-05 at 08:36 -0400, josef.pktd at gmail.com wrote:
>> >
>> <snip>
>> >
>> > What is actually being deprecated?
>> > It looks like there are different examples.
>> >
>> >
>> > wrong length: Nathaniels first example above, where the mask is not
>> > broadcastable to original array because mask is longer or shorter than
>> > shape[axis].
>> > I also wouldn't have expected this to work, although I use np.nozero
>> > and boolean mask indexing interchangeably, I would assume we need the
>> > correct length for the mask.
>> >
>>
>> For the moment we are only talking about wrong length (along a given
>> dimension). Not about wrong number of dimensions or multiple boolean
>> indices.
>>
>
> I am pro-deprecation then, definitely. I don't see a use case for padding
> a wrong-shaped boolean array with Falses, and the padding has burned me in
> the past.
>
> It's not orthogonal to the wrong-number-of-dimensions issue, though,
> because if your Boolean array has a dimension of length 1, broadcasting
> says duplicate it along that axis to match the indexee, and wrong-length
> says pad it with Falses. This ambiguity/pitfall disappears if the padding
> never happens, and that kind of broadcasting is very useful.
>
Good argument, now I understand why we only get a single column
>>> x = np.arange(4*5).reshape(4,5)
>>> mask = np.array([1,0,1,0,1], bool)
padding with False, this would also be deprecated AFAIU, and Anna pointed
out
>>> x[mask[:4][:,None]]
array([ 0, 10])
>>> x[mask[None,:]]
array([0, 2, 4])
masks can only be combined with slices, so no "fancy masking" allowed nor
defined (yet)
>>> x[mask[:4][:,None], mask[None,:]]
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
x[mask[:4][:,None], mask[None,:]]
IndexError: too many indices for array
I'm using 1d masks quite often to select rows or columns, which seems to
work in more than two dimensions
(Benjamin's surprise)
>>> x[:, mask]
array([[ 0, 2, 4],
[ 5, 7, 9],
[10, 12, 14],
[15, 17, 19]])
>>> x[mask[:4][:,None] * mask[None,:]]
array([ 0, 2, 4, 10, 12, 14])
>>> x[:,:,None][mask[:4][:,None] * mask[None,:]]
array([[ 0],
[ 2],
[ 4],
[10],
[12],
[14]])
Josef
>
> Anne
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150605/beac0411/attachment.html>
More information about the NumPy-Discussion
mailing list