Зміст курсу
Ultimate NumPy
Ultimate NumPy
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. There's nothing complicated about it.
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.
Let's look at an example:
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]]) print('-' * 10) # 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]])
Here is an illustration to make things clear:
Завдання
You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list
) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures
(use a negative index only for the last element).
Дякуємо за ваш відгук!
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. There's nothing complicated about it.
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.
Let's look at an example:
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]]) print('-' * 10) # 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]])
Here is an illustration to make things clear:
Завдання
You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list
) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures
(use a negative index only for the last element).
Дякуємо за ваш відгук!
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. There's nothing complicated about it.
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.
Let's look at an example:
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]]) print('-' * 10) # 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]])
Here is an illustration to make things clear:
Завдання
You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list
) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures
(use a negative index only for the last element).
Дякуємо за ваш відгук!
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. There's nothing complicated about it.
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.
Let's look at an example:
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]]) print('-' * 10) # 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]])
Here is an illustration to make things clear:
Завдання
You are managing a list of daily temperatures recorded over a week, stored in a 1D NumPy array where each element represents the temperature of a specific day. Use integer array indexing (a Python list
) to retrieve the temperatures of the first day, the second day, and the last day of the week from weekly_temperatures
(use a negative index only for the last element).