NumPy Boolean Array Indexing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 4: Line 4:
=Overview=
=Overview=


Boolean indexing is when a boolean array is used to select element from another array with the same shape.
If the boolean array and the target array do not have the same shape, the operation produces an <code>IndexError</code>:
<font size=-2>
<font color=red>IndexError</font>: boolean index did not match indexed array along dimension 0; dimension is 6 but corresponding boolean dimension is 5
</font>
The boolean arrays used in boolean indexing can be generated with [[NumPy_ndarray#Vectorized_Comparison|vectorized comparison]].
The boolean arrays used in boolean indexing can be generated with [[NumPy_ndarray#Vectorized_Comparison|vectorized comparison]].

Revision as of 16:37, 21 May 2024

Internal

Overview

Boolean indexing is when a boolean array is used to select element from another array with the same shape.

If the boolean array and the target array do not have the same shape, the operation produces an IndexError:

IndexError: boolean index did not match indexed array along dimension 0; dimension is 6 but corresponding boolean dimension is 5

The boolean arrays used in boolean indexing can be generated with vectorized comparison.