Course Content
Ultimate NumPy
Ultimate NumPy
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.
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])
The picture below shows the structure of the stock_prices
array used in the task:
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.
-
Retrieve all the stock prices of the first company over five days using positive indexing.
-
Retrieve the stock price of the third company on the second day using positive indiexing.
-
Retrieve the stock price of the last company on the last day using negative indexing.
Thanks for your feedback!
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.
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])
The picture below shows the structure of the stock_prices
array used in the task:
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.
-
Retrieve all the stock prices of the first company over five days using positive indexing.
-
Retrieve the stock price of the third company on the second day using positive indiexing.
-
Retrieve the stock price of the last company on the last day using negative indexing.
Thanks for your feedback!