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
Let's first take a look at a 2D array example:
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:
As you can see, indexing along each of the axes is identical to indexing in 1D arrays.
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.
Let's take a look at an example:
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]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
Task
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.
Your task is the following:
- Retrieve all the stock prices of the first company over five days with a positive index.
- Retrieve the stock price of the third company on the second day with positive indices.
- Retrieve the stock price of the last company on the last day with negative indices.
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
Let's first take a look at a 2D array example:
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:
As you can see, indexing along each of the axes is identical to indexing in 1D arrays.
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.
Let's take a look at an example:
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]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
Task
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.
Your task is the following:
- Retrieve all the stock prices of the first company over five days with a positive index.
- Retrieve the stock price of the third company on the second day with positive indices.
- Retrieve the stock price of the last company on the last day with negative indices.
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
Let's first take a look at a 2D array example:
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:
As you can see, indexing along each of the axes is identical to indexing in 1D arrays.
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.
Let's take a look at an example:
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]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
Task
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.
Your task is the following:
- Retrieve all the stock prices of the first company over five days with a positive index.
- Retrieve the stock price of the third company on the second day with positive indices.
- Retrieve the stock price of the last company on the last day with negative indices.
Thanks for your feedback!
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
Let's first take a look at a 2D array example:
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:
As you can see, indexing along each of the axes is identical to indexing in 1D arrays.
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.
Let's take a look at an example:
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]) print('-' * 7) # Accessing the second element of the first 1D array with positive index print(array_2d[0, 1]) print('-' * 7) # Accessing the last element of the last 1D array with negative index print(array_2d[-1, -1])
Task
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.
Your task is the following:
- Retrieve all the stock prices of the first company over five days with a positive index.
- Retrieve the stock price of the third company on the second day with positive indices.
- Retrieve the stock price of the last company on the last day with negative indices.