Recall & Review
beginner
What does
np.unravel_index() do in NumPy?It converts a flat (1D) index into a tuple of coordinate indices for a given shape of a multi-dimensional array.
Click to reveal answer
beginner
How do you use
np.unravel_index() to find the 2D position of the flat index 5 in a shape (3, 4)?Use
np.unravel_index(5, (3, 4)). It returns (1, 1), meaning row 1, column 1 in the 3x4 array.Click to reveal answer
intermediate
What is the output of
np.unravel_index([3, 7], (3, 3))?It returns two arrays: (array([1, 2]), array([0, 1])) which correspond to the row and column indices for flat indices 3 and 7 in a 3x3 array.
Click to reveal answer
beginner
Why is
np.unravel_index() useful in real life?It helps find the position in multi-dimensional data when you only have a single flat index, like finding a seat in a theater when you know the seat number but not the row and column.
Click to reveal answer
intermediate
What happens if you provide a flat index that is out of bounds for the given shape in
np.unravel_index()?NumPy raises an error because the flat index does not fit inside the array shape. The index must be less than the total number of elements.
Click to reveal answer
What does
np.unravel_index(8, (3, 3)) return?✗ Incorrect
The flat index 8 corresponds to row 2, column 2 in a 3x3 array.
If you have a flat index array
[0, 4, 7] and shape (3, 3), what does np.unravel_index() return?✗ Incorrect
Flat indices 0, 4, 7 correspond to positions (0,0), (1,1), and (2,1) respectively.
What is the required input for
np.unravel_index()?✗ Incorrect
You need a flat index (or indices) and the shape of the array to convert to multi-dimensional indices.
What happens if you pass a flat index equal to the total number of elements in the array to
np.unravel_index()?✗ Incorrect
Flat indices must be less than the total number of elements; otherwise, an error is raised.
Which of these is a valid use case for
np.unravel_index()?✗ Incorrect
np.unravel_index() converts flat indices to multi-dimensional coordinates.Explain how
np.unravel_index() helps convert a flat index into multi-dimensional coordinates. Give a simple example.Think about how a single number can point to a position in a grid.
You got /4 concepts.
Describe what happens if you provide an out-of-range flat index to
np.unravel_index(). Why is this important?Consider what happens if you try to find a seat number that doesn't exist.
You got /3 concepts.