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

Contenido del Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookBoolean Indexing in 2D Arrays

Boolean indexing in 2D and higher-dimensional arrays works similarly to 1D arrays. However, the boolean array must have the same number of dimensions as the initial array (e.g., 2D for two-dimensional arrays). The returned array, however, will be 1D.

Here is an example:

12345678
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving elements less than 3 or greater than OR greater than or equal to 8 print(array_2d[(array_2d < 3) | (array_2d >= 8)])
copy

Let’s visualize it:

The boolean array on the right is the resulting boolean array of these two combined conditions. Once again, purple elements match the conditions, and green elements don’t. So when applying array_2d[(array_2d < 3) | (array_2d >= 8)], we get a 1D array of purple elements.

Tarea
test

Swipe to show code editor

You are analyzing the temperatures recorded in two different cities over four days. The temperatures are stored in the city_temperatures array where each row represents a city and each column represents a day. Your task is to include only the temperatures that are less than or equal to 15 degrees OR greater than 30 degrees Celsius using boolean indexing.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 9
toggle bottom row

bookBoolean Indexing in 2D Arrays

Boolean indexing in 2D and higher-dimensional arrays works similarly to 1D arrays. However, the boolean array must have the same number of dimensions as the initial array (e.g., 2D for two-dimensional arrays). The returned array, however, will be 1D.

Here is an example:

12345678
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving elements less than 3 or greater than OR greater than or equal to 8 print(array_2d[(array_2d < 3) | (array_2d >= 8)])
copy

Let’s visualize it:

The boolean array on the right is the resulting boolean array of these two combined conditions. Once again, purple elements match the conditions, and green elements don’t. So when applying array_2d[(array_2d < 3) | (array_2d >= 8)], we get a 1D array of purple elements.

Tarea
test

Swipe to show code editor

You are analyzing the temperatures recorded in two different cities over four days. The temperatures are stored in the city_temperatures array where each row represents a city and each column represents a day. Your task is to include only the temperatures that are less than or equal to 15 degrees OR greater than 30 degrees Celsius using boolean indexing.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 9
toggle bottom row

bookBoolean Indexing in 2D Arrays

Boolean indexing in 2D and higher-dimensional arrays works similarly to 1D arrays. However, the boolean array must have the same number of dimensions as the initial array (e.g., 2D for two-dimensional arrays). The returned array, however, will be 1D.

Here is an example:

12345678
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving elements less than 3 or greater than OR greater than or equal to 8 print(array_2d[(array_2d < 3) | (array_2d >= 8)])
copy

Let’s visualize it:

The boolean array on the right is the resulting boolean array of these two combined conditions. Once again, purple elements match the conditions, and green elements don’t. So when applying array_2d[(array_2d < 3) | (array_2d >= 8)], we get a 1D array of purple elements.

Tarea
test

Swipe to show code editor

You are analyzing the temperatures recorded in two different cities over four days. The temperatures are stored in the city_temperatures array where each row represents a city and each column represents a day. Your task is to include only the temperatures that are less than or equal to 15 degrees OR greater than 30 degrees Celsius using boolean indexing.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Boolean indexing in 2D and higher-dimensional arrays works similarly to 1D arrays. However, the boolean array must have the same number of dimensions as the initial array (e.g., 2D for two-dimensional arrays). The returned array, however, will be 1D.

Here is an example:

12345678
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving elements less than 3 or greater than OR greater than or equal to 8 print(array_2d[(array_2d < 3) | (array_2d >= 8)])
copy

Let’s visualize it:

The boolean array on the right is the resulting boolean array of these two combined conditions. Once again, purple elements match the conditions, and green elements don’t. So when applying array_2d[(array_2d < 3) | (array_2d >= 8)], we get a 1D array of purple elements.

Tarea
test

Swipe to show code editor

You are analyzing the temperatures recorded in two different cities over four days. The temperatures are stored in the city_temperatures array where each row represents a city and each column represents a day. Your task is to include only the temperatures that are less than or equal to 15 degrees OR greater than 30 degrees Celsius using boolean indexing.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 9
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt