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

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

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

book
Multidimensional Indexing

Now that you are able to access elements in 1D arrays, it's time to learn about indexing in higher-dimensional arrays.

2D Arrays Indexing

This is a 2x3 array, which means it consists of 2 1D arrays along axis 0, and each of these 1D arrays has 3 elements along axis 1.

The images below will clarify positive and negative indexing in 2D arrays (array values are shown in black, and indices are shown in green for positive indices and red for negative indices):

Accessing Elements in 2D Arrays

In 1D arrays, we accessed elements by specifying the index of the element in square brackets. If we do the same in 2D arrays, we retrieve a 1D array at the specified index, which may be exactly what we need.

However, if we want to retrieve a particular element of an inner 1D array, we should specify the index of the 1D array (along axis 0) and the index of its element (along axis 1), e.g., array[0, 1]. We could also write array[0][1] as we do with Python list, but this is less efficient since it performs the search twice for each index instead of once.

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

12345678
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy

The picture below shows the structure of the stock_prices array used in the task:

Tarefa
test

Swipe to show code editor

stock_prices contains simulated stock prices over five days for five different companies. Each row corresponds to a particular company, and each column corresponds to a particular day. Consequently, each element in the matrix represents the closing price of a certain company's stock on a given day.

  1. Retrieve all the stock prices of the first company over five days using positive indexing.

  2. Retrieve the stock price of the third company on the second day using positive indiexing.

  3. Retrieve the stock price of the last company on the last day using negative indexing.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
toggle bottom row

book
Multidimensional Indexing

Now that you are able to access elements in 1D arrays, it's time to learn about indexing in higher-dimensional arrays.

2D Arrays Indexing

This is a 2x3 array, which means it consists of 2 1D arrays along axis 0, and each of these 1D arrays has 3 elements along axis 1.

The images below will clarify positive and negative indexing in 2D arrays (array values are shown in black, and indices are shown in green for positive indices and red for negative indices):

Accessing Elements in 2D Arrays

In 1D arrays, we accessed elements by specifying the index of the element in square brackets. If we do the same in 2D arrays, we retrieve a 1D array at the specified index, which may be exactly what we need.

However, if we want to retrieve a particular element of an inner 1D array, we should specify the index of the 1D array (along axis 0) and the index of its element (along axis 1), e.g., array[0, 1]. We could also write array[0][1] as we do with Python list, but this is less efficient since it performs the search twice for each index instead of once.

Note

If a specified index is out of bounds, an IndexError is thrown, so be cautious of that.

12345678
import numpy as np array_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Accessing the first element (1D array) with positive index print(array_2d[0]) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
copy

The picture below shows the structure of the stock_prices array used in the task:

Tarefa
test

Swipe to show code editor

stock_prices contains simulated stock prices over five days for five different companies. Each row corresponds to a particular company, and each column corresponds to a particular day. Consequently, each element in the matrix represents the closing price of a certain company's stock on a given day.

  1. Retrieve all the stock prices of the first company over five days using positive indexing.

  2. Retrieve the stock price of the third company on the second day using positive indiexing.

  3. Retrieve the stock price of the last company on the last day using negative indexing.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt