np.take
is a function that takes the elements from an array along an axis. When there is an axis parameter specified it behaves exactly like "fancy" indexing (indexing using arrays), but without an axis parameter the array a is flattened, and then the indices taken from this array. However, the documentation gives nothing on boolean
indexing and it is unspecified what the behavior is with boolean arrays: https://numpy.org/doc/stable/reference/generated/numpy.take.html
The code in question is the following
Input:
a = np.array([2, 3])
b = np.array([[False, True],
[ True, False],
[False, True],
[ True, False]])
a.take(b)
Output:
array([[2, 3],
[3, 2],
[2, 3],
[3, 2]])
In this particular code how is the output being switched when we have a column of [True, False] and stays the same with a column of [False, True]? Now when I try this with boolean
indexing where I have the index as a boolean
array I get an error:
Input:
a = np.array([2, 3])
b = np.array([[False, True],
[ True, False],
[False, True],
[ True, False]])
a[b]
Output:
IndexError Traceback (most recent call last)
<ipython-input-6-8b64b196a893> in <cell line: 7>()
5 [ True, False]])
6
----> 7 a[b]
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
This error makes sense to me because the index array (b) has two dimensions while the array a has only one. So what is np.take
doing so that it just switches the columns in each row?