Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Integer Array 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

bookInteger Array Indexing in 2D Arrays

Speaking of 2D and higher-dimensional arrays, integer array indexing works the same as in 1D arrays along each axis. If we use only one integer array for indexing, we index along only one axis (axis 0). If we use two arrays separated by a comma, we index along both axes (axis 0 and axis 1).

Indexing only along axis 0 using an array of integers returns a 2D array. When we access elements via such indexing, we group them into a new array. This new array consists of 1D arrays, and grouping them increases the dimensionality by one, resulting in a 2D array.

Indexing along axis 0 and axis 1 using two arrays of integers returns a 1D array.

Note

All integer arrays used for each of the axes must have the same shape.

Now it’s time for an example:

1234567891011121314151617
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving first and the third row print(array_2d[[0, 2]]) print('-' * 10) # Retrieving the main diagonal elements print(array_2d[[0, 1, 2], [0, 1, 2]]) print('-' * 10) # Retrieving the first and third element of the second row print(array_2d[1, [0, 2]]) # IndexError is thrown, since index 3 along axis 0 is out of bounds print(array_2d[[0, 3], [0, 1]])
copy

Here is an illustration for clarification:

As you can see, we can also combine basic integer indexing and integer array indexing.

Note

Once again, if at least one of the indices is out of bounds, an IndexError is thrown.

Tarea
test

Swipe to show code editor

You are analyzing the performance metrics of three different products over two criteria, stored in a 2D NumPy array. Use integer array indexing (a Python list) to retrieve the first metric of the first product and the second metric of the third product from performance_metrics using only positive indices.

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 6
toggle bottom row

bookInteger Array Indexing in 2D Arrays

Speaking of 2D and higher-dimensional arrays, integer array indexing works the same as in 1D arrays along each axis. If we use only one integer array for indexing, we index along only one axis (axis 0). If we use two arrays separated by a comma, we index along both axes (axis 0 and axis 1).

Indexing only along axis 0 using an array of integers returns a 2D array. When we access elements via such indexing, we group them into a new array. This new array consists of 1D arrays, and grouping them increases the dimensionality by one, resulting in a 2D array.

Indexing along axis 0 and axis 1 using two arrays of integers returns a 1D array.

Note

All integer arrays used for each of the axes must have the same shape.

Now it’s time for an example:

1234567891011121314151617
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving first and the third row print(array_2d[[0, 2]]) print('-' * 10) # Retrieving the main diagonal elements print(array_2d[[0, 1, 2], [0, 1, 2]]) print('-' * 10) # Retrieving the first and third element of the second row print(array_2d[1, [0, 2]]) # IndexError is thrown, since index 3 along axis 0 is out of bounds print(array_2d[[0, 3], [0, 1]])
copy

Here is an illustration for clarification:

As you can see, we can also combine basic integer indexing and integer array indexing.

Note

Once again, if at least one of the indices is out of bounds, an IndexError is thrown.

Tarea
test

Swipe to show code editor

You are analyzing the performance metrics of three different products over two criteria, stored in a 2D NumPy array. Use integer array indexing (a Python list) to retrieve the first metric of the first product and the second metric of the third product from performance_metrics using only positive indices.

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 6
toggle bottom row

bookInteger Array Indexing in 2D Arrays

Speaking of 2D and higher-dimensional arrays, integer array indexing works the same as in 1D arrays along each axis. If we use only one integer array for indexing, we index along only one axis (axis 0). If we use two arrays separated by a comma, we index along both axes (axis 0 and axis 1).

Indexing only along axis 0 using an array of integers returns a 2D array. When we access elements via such indexing, we group them into a new array. This new array consists of 1D arrays, and grouping them increases the dimensionality by one, resulting in a 2D array.

Indexing along axis 0 and axis 1 using two arrays of integers returns a 1D array.

Note

All integer arrays used for each of the axes must have the same shape.

Now it’s time for an example:

1234567891011121314151617
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving first and the third row print(array_2d[[0, 2]]) print('-' * 10) # Retrieving the main diagonal elements print(array_2d[[0, 1, 2], [0, 1, 2]]) print('-' * 10) # Retrieving the first and third element of the second row print(array_2d[1, [0, 2]]) # IndexError is thrown, since index 3 along axis 0 is out of bounds print(array_2d[[0, 3], [0, 1]])
copy

Here is an illustration for clarification:

As you can see, we can also combine basic integer indexing and integer array indexing.

Note

Once again, if at least one of the indices is out of bounds, an IndexError is thrown.

Tarea
test

Swipe to show code editor

You are analyzing the performance metrics of three different products over two criteria, stored in a 2D NumPy array. Use integer array indexing (a Python list) to retrieve the first metric of the first product and the second metric of the third product from performance_metrics using only positive indices.

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!

Speaking of 2D and higher-dimensional arrays, integer array indexing works the same as in 1D arrays along each axis. If we use only one integer array for indexing, we index along only one axis (axis 0). If we use two arrays separated by a comma, we index along both axes (axis 0 and axis 1).

Indexing only along axis 0 using an array of integers returns a 2D array. When we access elements via such indexing, we group them into a new array. This new array consists of 1D arrays, and grouping them increases the dimensionality by one, resulting in a 2D array.

Indexing along axis 0 and axis 1 using two arrays of integers returns a 1D array.

Note

All integer arrays used for each of the axes must have the same shape.

Now it’s time for an example:

1234567891011121314151617
import numpy as np array_2d = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) # Retrieving first and the third row print(array_2d[[0, 2]]) print('-' * 10) # Retrieving the main diagonal elements print(array_2d[[0, 1, 2], [0, 1, 2]]) print('-' * 10) # Retrieving the first and third element of the second row print(array_2d[1, [0, 2]]) # IndexError is thrown, since index 3 along axis 0 is out of bounds print(array_2d[[0, 3], [0, 1]])
copy

Here is an illustration for clarification:

As you can see, we can also combine basic integer indexing and integer array indexing.

Note

Once again, if at least one of the indices is out of bounds, an IndexError is thrown.

Tarea
test

Swipe to show code editor

You are analyzing the performance metrics of three different products over two criteria, stored in a 2D NumPy array. Use integer array indexing (a Python list) to retrieve the first metric of the first product and the second metric of the third product from performance_metrics using only positive indices.

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 6
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