Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Boolean Indexing | Indexing and Slicing
Ultimate NumPy

Boolean IndexingBoolean Indexing

Boolean indexing (also known as boolean array indexing) allows us to select elements in an array based on certain conditions. This type of indexing is extremely useful for efficiently filtering data in arrays, especially in large ones.

Boolean Arrays

To understand how boolean indexing works, we first need to understand what boolean arrays are.

A boolean array is an array consisting of elements, each of which can be either True or False.

Such an array can be created either by explicitly specifying its elements or based on a certain condition for the elements of a particular array.

Let's look at an example:

Here, array is an array of integers from 1 to 10 inclusive. We then create a boolean array named boolean_array based on the condition array > 5. This means that if a certain element of array is greater than 5 (condition is True), the element in boolean_array at this index is True; otherwise, it is False.

Here is an image to clarify this:

Boolean array

The upper array is our initial array where green elements don’t match the condition, and purple elements match the condition. The lower array is our created boolean array.

Boolean Array Indexing

Boolean indexing works quite straightforwardly: you simply specify the boolean array in square brackets. The resulting elements are those with the indices corresponding to the elements with True values in the boolean array.

Back to our previous example:

Boolean indexing

You can see that elements with True values have indices from 5 to 9, so the elements of array with these indices are returned as a result of boolean indexing:

Все було зрозуміло?

Секція 2. Розділ 7
course content

Зміст курсу

Ultimate NumPy

Boolean IndexingBoolean Indexing

Boolean indexing (also known as boolean array indexing) allows us to select elements in an array based on certain conditions. This type of indexing is extremely useful for efficiently filtering data in arrays, especially in large ones.

Boolean Arrays

To understand how boolean indexing works, we first need to understand what boolean arrays are.

A boolean array is an array consisting of elements, each of which can be either True or False.

Such an array can be created either by explicitly specifying its elements or based on a certain condition for the elements of a particular array.

Let's look at an example:

Here, array is an array of integers from 1 to 10 inclusive. We then create a boolean array named boolean_array based on the condition array > 5. This means that if a certain element of array is greater than 5 (condition is True), the element in boolean_array at this index is True; otherwise, it is False.

Here is an image to clarify this:

Boolean array

The upper array is our initial array where green elements don’t match the condition, and purple elements match the condition. The lower array is our created boolean array.

Boolean Array Indexing

Boolean indexing works quite straightforwardly: you simply specify the boolean array in square brackets. The resulting elements are those with the indices corresponding to the elements with True values in the boolean array.

Back to our previous example:

Boolean indexing

You can see that elements with True values have indices from 5 to 9, so the elements of array with these indices are returned as a result of boolean indexing:

Все було зрозуміло?

Секція 2. Розділ 7
some-alt