[Numpy-discussion] Can't mix np.newaxis with boolean indexing

Torgil Svensson torgil.svensson at gmail.com
Sun Aug 21 16:07:16 EDT 2011


Since the result is one-dimensional after using boolean indexing you
can always do:

a[b][:, np.newaxis]
array([[2],
         [3],
         [4]])

a[b][np.newaxis, :]
array([[2, 3, 4]])

//Torgil

On Sat, Aug 20, 2011 at 10:17 PM, Benjamin Root <ben.root at ou.edu> wrote:
> On Sat, Aug 20, 2011 at 2:47 AM, Olivier Verdier <zelbier at gmail.com> wrote:
>>
>> Your syntax is not as intuitive as you may think.
>>
>> Suppose I take a matrix instead
>>
>> a = np.array([1,2,3,4]).reshape(2,2)
>> b = (a>1) # np.array([[False,True],[True,True]])
>>
>> How would a[b,np.newaxis] be supposed to work?
>>
>> Note that other (simple) slices work perfectly with newaxis, such as
>> a[:1,np.newaxis]
>>
>> == Olivier
>>
>
> Personally, I would have expected it to flatten the results and added a
> dimension:
>
> a[b, np.newaxis]
> array([[2],
>          [3],
>          [4]])
>
> or
>
> a[np.newaxis, b]
> array([[2, 3, 4]])
>
> I mean, it flattens the results anyway when doing boolean indexing for
> multi-dimensional arrays, so someone doing that should expect that anyway.
>
> At the very least, I think maybe we could have a better error message than
> just saying that long() can't take a NoneType?
>
> Thanks,
> Ben Root
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>



More information about the NumPy-Discussion mailing list