5

Can someone explain what this code is doing?

   a = np.array([[1, 2], [3, 4]])
   a[..., [True, False]]

What is the [True, False] doing there?

JRR
  • 6,014
  • 6
  • 39
  • 59

1 Answers1

1

Ellipsis Notation and Booleans as Integers

From the numpy docs:

Ellipsis expand to the number of : objects needed to make a selection tuple of the same length as x.ndim. There may only be a single ellipsis present

True and False are just obfuscated 0 and 1. Taking the example from the docs:

x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
x[...,0]
# outputs: array([[1, 2, 3],
#       [4, 5, 6]])
x[..., False] # same thing

The boolean values are specifying an index, just like the numbers 0 or 1 would.


In response to your question in the comments

It first seems magical that

a = np.array([[1, 2], [3, 4]])
a[..., [True, True]]  # = [[2,2],[4,4]]

But when we consider it as

a[..., [1,1]] # = [[2,2],[4,4]]

It seems less impressive.

Similarly:

b = array([[1,2,3],[4,5,6]])
b[...,[2,2]] # = [[3,3],[5,5]]

After applying the ellipsis rules; the true and false grab column indices, just like 0, 1, or 17 would have


Boolean Arrays for Complex Indexing

There are some subtle differences (bool's have a different type than ints). A lot of the hairy details can be found here. These do not seem to have any roll in your code, but they are interesting in figuring out how numpy indexing works.

In particular, this line is probably what you're looking for:

In the future Boolean array-likes (such as lists of python bools) will always be treated as Boolean indexes

On this page, they talk about boolean arrays, which are quite complex as an indexing tool

Boolean arrays used as indices are treated in a different manner entirely than index arrays. Boolean arrays must be of the same shape as the initial dimensions of the array being indexed

Skipping down a bit

Unlike in the case of integer index arrays, in the boolean case, the result is a 1-D array containing all the elements in the indexed array corresponding to all the true elements in the boolean array. The elements in the indexed array are always iterated and returned in row-major (C-style) order. The result is also identical to y[np.nonzero(b)]. As with index arrays, what is returned is a copy of the data, not a view as one gets with slices.

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • If that's the case then why does `a[..., [True, True]]` returns `array([[2, 2], [4, 4]])` – JRR Jun 16 '16 at 03:55
  • @JRR sorry, didn't mean to cut that answer short submitted too soon – en_Knight Jun 16 '16 at 03:59
  • @JRR you can confirm that a[..., [True, True]] evaluates the same way as a[..., [1, 1]], correct? – en_Knight Jun 16 '16 at 04:08
  • @en_Knight, according to [this](http://stackoverflow.com/questions/24410469/boolean-indexing-but-turns-out-to-be-some-other-operation), when the index is a list, the boolean values are converted to corresponding integers, and the resulting values are used as actual indices in the matrix. And I also personally verified from my side, they are the same indeed. – MaThMaX Jun 16 '16 at 04:10
  • @MaThMaX we're on the same page, right? That bottom section isn't to confuse people, it's just to help clarify if some of the numpy terminology seems scary. If it's unclear I can clean it up or remove it... it's just fun to dive down the numpy rabbit hole :) – en_Knight Jun 16 '16 at 04:16
  • @en_Knight, yes, we are on the same page. I think it is fine to have the bottom section. – MaThMaX Jun 16 '16 at 04:20