Bird
Raised Fist0
NumPydata~10 mins

np.unravel_index() for multi-dim positions in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the multi-dimensional index of the flat index 5 in a 3x3 array.

NumPy
import numpy as np
index = np.unravel_index([1], (3, 3))
print(index)
Drag options to blanks, or click blank then click option'
A1
B5
C9
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the shape as a single number instead of a tuple.
Confusing flat index with multi-dimensional index.
2fill in blank
medium

Complete the code to find the multi-dimensional index of flat index 14 in a 4x5 array.

NumPy
import numpy as np
pos = np.unravel_index([1], (4, 5))
print(pos)
Drag options to blanks, or click blank then click option'
A14
B10
C20
D9
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong flat index number.
Mixing up rows and columns in the shape.
3fill in blank
hard

Fix the error in the code to correctly get the multi-dimensional index of flat index 7 in a 2x4x3 array.

NumPy
import numpy as np
pos = np.unravel_index([1], (2, 4, 3))
print(pos)
Drag options to blanks, or click blank then click option'
A7
B[7]
C(7,)
D'7'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the flat index as a list or string.
Using incorrect shape tuple format.
4fill in blank
hard

Fill both blanks to create a list of multi-dimensional indices for flat indices 0 to 5 in a 2x3 array.

NumPy
import numpy as np
indices = [np.unravel_index(i, [1]) for i in [2]]
print(indices)
Drag options to blanks, or click blank then click option'
A(2, 3)
Brange(6)
Crange(5)
D[0, 1, 2, 3, 4, 5]
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(5) which excludes the last index.
Using a list instead of range for iteration.
5fill in blank
hard

Fill all three blanks to create a dictionary mapping flat indices to their multi-dimensional indices for a 3x2x2 array, for indices 0 to 11.

NumPy
import numpy as np
mapping = { [1]: [2] for i in [3] }
print(mapping)
Drag options to blanks, or click blank then click option'
Ai
Bnp.unravel_index(i, (3, 2, 2))
Crange(12)
Di**2
Attempts:
3 left
💡 Hint
Common Mistakes
Using i**2 instead of unravel_index for values.
Using incorrect range length.
Swapping keys and values.

Practice

(1/5)
1. What does np.unravel_index() do in NumPy?
easy
A. It returns the total number of elements in an array.
B. It converts a flat index into a tuple of coordinate indices for a given shape.
C. It reshapes an array into a new shape without changing data.
D. It flattens a multi-dimensional array into a one-dimensional array.

Solution

  1. Step 1: Understand the function purpose

    np.unravel_index() takes a flat index and a shape to find the multi-dimensional coordinates.
  2. Step 2: Compare options with function behavior

    Only It converts a flat index into a tuple of coordinate indices for a given shape. describes converting a flat index to multi-dimensional indices, which matches the function's purpose.
  3. Final Answer:

    It converts a flat index into a tuple of coordinate indices for a given shape. -> Option B
  4. Quick Check:

    Function purpose = convert flat index to coordinates [OK]
Hint: Remember: unravel means 'find coordinates' from flat index [OK]
Common Mistakes:
  • Confusing unravel_index with flatten or reshape functions
  • Thinking it returns the total number of elements
  • Assuming it changes the array data
2. Which of the following is the correct syntax to use np.unravel_index() for a flat index 5 in a shape (3, 3)?
easy
A. np.unravel_index((3, 3), 5)
B. np.unravel_index(5, 3, 3)
C. np.unravel_index(5, (3, 3))
D. np.unravel_index([5], (3, 3))

Solution

  1. Step 1: Recall the function signature

    The correct syntax is np.unravel_index(indices, shape), where indices is an integer or array and shape is a tuple.
  2. Step 2: Match options to syntax

    np.unravel_index(5, (3, 3)) matches the correct syntax with index 5 and shape (3, 3). np.unravel_index([5], (3, 3)) uses a list for index which is allowed but less common for single index.
  3. Final Answer:

    np.unravel_index(5, (3, 3)) -> Option C
  4. Quick Check:

    Syntax = np.unravel_index(index, shape) [OK]
Hint: Use np.unravel_index(index, shape) with shape as a tuple [OK]
Common Mistakes:
  • Swapping the order of arguments
  • Passing shape as separate numbers instead of a tuple
  • Using list instead of integer for single index without need
3. What is the output of the following code?
import numpy as np
index = 7
shape = (3, 3)
result = np.unravel_index(index, shape)
print(result)
medium
A. (1, 1)
B. (1, 2)
C. (2, 0)
D. (2, 1)

Solution

  1. Step 1: Understand the shape and flat index

    The shape is (3, 3), so the array has 3 rows and 3 columns. Flat indices go row-wise: 0 to 8.
  2. Step 2: Calculate multi-dimensional indices for flat index 7

    Index 7 corresponds to row = 7 // 3 = 2, column = 7 % 3 = 1, so position is (2, 1).
  3. Final Answer:

    (2, 1) -> Option D
  4. Quick Check:

    Row = 7//3=2, Col = 7%3=1 [OK]
Hint: Divide index by shape dims to find row, mod for column [OK]
Common Mistakes:
  • Mixing row and column order
  • Using zero-based index incorrectly
  • Confusing division and modulo operations
4. The code below raises an error. What is the cause?
import numpy as np
index = 10
shape = (3, 3)
result = np.unravel_index(index, shape)
print(result)
medium
A. Index 10 is out of bounds for shape (3, 3).
B. Shape must be a list, not a tuple.
C. np.unravel_index requires a 1D array, not a scalar index.
D. Missing import statement for numpy.

Solution

  1. Step 1: Check the shape size

    Shape (3, 3) means total elements = 3*3 = 9, so valid flat indices are 0 to 8.
  2. Step 2: Analyze the index value

    Index 10 is greater than 8, so it is out of bounds and causes an error.
  3. Final Answer:

    Index 10 is out of bounds for shape (3, 3). -> Option A
  4. Quick Check:

    Max index = 8, given index = 10 [OK]
Hint: Check if index < product of shape dims to avoid errors [OK]
Common Mistakes:
  • Assuming tuple shape is invalid
  • Thinking np.unravel_index needs array input only
  • Ignoring index bounds for given shape
5. You have a 3D array with shape (4, 3, 2). You want to find the multi-dimensional indices for flat indices [5, 10, 15]. Which code correctly returns these positions?
hard
A. np.unravel_index([5, 10, 15], (4, 3, 2))
B. np.unravel_index(5, 10, 15, (4, 3, 2))
C. np.unravel_index((5, 10, 15), (4, 3, 2))
D. np.unravel_index([5, 10, 15], 4, 3, 2)

Solution

  1. Step 1: Understand input for multiple indices

    To convert multiple flat indices, pass a list or array of indices as first argument and shape tuple as second.
  2. Step 2: Match options to correct syntax

    np.unravel_index([5, 10, 15], (4, 3, 2)) correctly passes a list of indices and the shape tuple. np.unravel_index((5, 10, 15), (4, 3, 2)) passes a tuple instead of list, which also works but less common. np.unravel_index(5, 10, 15, (4, 3, 2)) and D have wrong argument formats.
  3. Final Answer:

    np.unravel_index([5, 10, 15], (4, 3, 2)) -> Option A
  4. Quick Check:

    Multiple indices = list + shape tuple [OK]
Hint: Use list of indices and shape tuple for multiple positions [OK]
Common Mistakes:
  • Passing multiple indices as separate arguments
  • Using tuple instead of list for indices (less common but works)
  • Passing shape as separate numbers instead of tuple
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard