0
0
NumPydata~10 mins

np.ix_() for open mesh indexing 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 create open mesh indices using np.ix_.

NumPy
import numpy as np
rows = np.array([0, 2, 4])
cols = np.array([1, 3])
mesh = np.[1](rows, cols)
print(mesh)
Drag options to blanks, or click blank then click option'
Aix_
Bmeshgrid
Copen_mesh
Dindices
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.meshgrid instead of np.ix_
Trying to use np.indices which creates grid indices for shape, not from arrays
2fill in blank
medium

Complete the code to select elements from a 2D array using np.ix_.

NumPy
import numpy as np
arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])
rows = [0, 2]
cols = [1, 2]
selected = arr[[1](rows, cols)]
print(selected)
Drag options to blanks, or click blank then click option'
Ameshgrid
Bopen_mesh
Cindices
Dix_
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.meshgrid which returns coordinate matrices, not suitable for fancy indexing here
Trying to index directly with lists without np.ix_
3fill in blank
hard

Fix the error in the code to correctly use np.ix_ for indexing.

NumPy
import numpy as np
arr = np.arange(16).reshape(4,4)
rows = [1, 3]
cols = [0, 2]
subset = arr[[1](rows, cols)]
print(subset)
Drag options to blanks, or click blank then click option'
Ameshgrid
Bix_
Cindices
Dopen_mesh
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.meshgrid which returns coordinate matrices, not suitable for fancy indexing
Using np.indices which expects shape, not arrays
4fill in blank
hard

Fill both blanks to create a dictionary of squares for numbers greater than 3 using dictionary comprehension.

NumPy
numbers = [1, 2, 3, 4, 5]
squares = [1]: [2] for n in numbers if n > 3
Drag options to blanks, or click blank then click option'
An
Bn**2
Dn*2
Attempts:
3 left
💡 Hint
Common Mistakes
Using n*2 instead of n**2 for squares
Swapping key and value positions
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase keys and values greater than 10.

NumPy
data = {'a': 5, 'b': 15, 'c': 20}
result = [1]: [2] for k, v in data.items() if v [3] 10}}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using k instead of k.upper() for keys
Using < instead of > in the condition