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

Зміст курсу

Ultimate NumPy

Ultimate NumPy

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

book
Integer Array Indexing

Apart from basic indexing, where we use an integer for a single index, NumPy also allows us to use an entire 1D array of integers (a list of integers is also possible) for indexing.

Integer Array Indexing in 1D Arrays

Each element of the integer array used for indexing is treated as an index, so, for example, array[[0, 1, 3]] retrieves elements at indices 0, 1, and 3 in the form of a 1D array, given that array is a 1D array itself. You can also use NumPy arrays for indexing, but it makes the code more cumbersome.

12345678
import numpy as np array = np.array([23, 41, 7, 80, 3]) # Retrieving elements at indices 0, 1 and 3 print(array[[0, 1, 3]]) # Retrieving elements at indices 1, -1 and 2 in this order print(array[[1, -1, 2]]) # IndexError is thrown since index 5 is out of bounds print(array[[2, 5]])
copy

Integer Array Indexing in 1D 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.

123456789101112131415
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]]) # Retrieving the main diagonal elements print(array_2d[[0, 1, 2], [0, 1, 2]]) # 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

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.

Speaking of applications, such indexing is useful when you need to select specific elements that are not next to each other or don't follow a regular order. Unlike slicing, which works with continuous ranges, this method lets you choose exactly which elements to retrieve. It's helpful when you want to extract scattered data or rearrange values in an array.

1. You are analyzing the monthly sales data (in thousands) for five products. What is the output of the code?
2. The `temperatures` array represents the weekly temperatures (in °C) of three cities over Monday, Tuesday, and Wednesday. Select the correct option to retrieve the temperatures of Berlin on Monday and Tuesday, and Madrid on Tuesday.
You are analyzing the monthly sales data (in thousands) for five products. What is the output of the code?

You are analyzing the monthly sales data (in thousands) for five products. What is the output of the code?

Виберіть правильну відповідь

The `temperatures` array represents the weekly temperatures (in °C) of three cities over Monday, Tuesday, and Wednesday. Select the correct option to retrieve the temperatures of Berlin on Monday and Tuesday, and Madrid on Tuesday.

The temperatures array represents the weekly temperatures (in °C) of three cities over Monday, Tuesday, and Wednesday. Select the correct option to retrieve the temperatures of Berlin on Monday and Tuesday, and Madrid on Tuesday.

Виберіть правильну відповідь

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
We're sorry to hear that something went wrong. What happened?
some-alt