0
0
NumPydata~10 mins

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

Choose your learning style9 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.