0
0
NumPydata~20 mins

np.ix_() for open mesh indexing in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.ix_ Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of np.ix_() for 2D mesh indexing
What is the output of the following code snippet using np.ix_() to create an open mesh grid?
NumPy
import numpy as np
rows = np.array([1, 3, 5])
cols = np.array([2, 4])
mesh = np.ix_(rows, cols)
result = mesh[0] + mesh[1]
print(result)
A
[[3 5]
 [5 7]
 [7 9]]
B
[[3 5 7]
 [5 7 9]]
C
[[3 5]
 [4 6]
 [5 7]]
D
[[3 5]
 [4 6]]
Attempts:
2 left
💡 Hint
Remember that np.ix_ creates open mesh grids that broadcast arrays for indexing.
data_output
intermediate
1:30remaining
Shape of arrays from np.ix_()
Given a = np.array([0, 1, 2]) and b = np.array([10, 20, 30, 40]), what are the shapes of the arrays returned by np.ix_(a, b)?
NumPy
import numpy as np
a = np.array([0, 1, 2])
b = np.array([10, 20, 30, 40])
mesh = np.ix_(a, b)
shapes = (mesh[0].shape, mesh[1].shape)
print(shapes)
A((4, 1), (3, 1))
B((3, 1), (1, 4))
C((1, 3), (4, 1))
D((3, 4), (3, 4))
Attempts:
2 left
💡 Hint
np.ix_ returns arrays shaped to broadcast for indexing: one column vector and one row vector.
🔧 Debug
advanced
2:30remaining
Identify the error in using np.ix_() for 3D indexing
What error will the following code raise when using np.ix_() for 3D indexing?
NumPy
import numpy as np
x = np.array([0, 1])
y = np.array([2, 3])
z = np.array([4, 5])
mesh = np.ix_(x, y, z)
arr = np.arange(60).reshape(3,4,5)
result = arr[mesh]
print(result.shape)
AValueError: cannot reshape array of size 60 into shape (3,4,5)
BTypeError: unhashable type: 'numpy.ndarray'
CIndexError: shape mismatch: indexing arrays could not be broadcast together
DNo error, prints (2, 2, 2)
Attempts:
2 left
💡 Hint
Check how np.ix_ arrays broadcast with the target array shape.
🚀 Application
advanced
1:30remaining
Using np.ix_() to select submatrix
You have a 5x5 matrix mat. Which option correctly uses np.ix_() to select rows 1, 3 and columns 0, 4 to create a 3x2 submatrix?
NumPy
import numpy as np
mat = np.arange(25).reshape(5,5)
Amat[[0,4]][[1,3]]
Bmat[[1,3], [0,4]]
Cmat[np.ix_([0,4], [1,3])]
Dmat[np.ix_([1,3], [0,4])]
Attempts:
2 left
💡 Hint
np.ix_ creates open mesh indexing for selecting multiple rows and columns.
🧠 Conceptual
expert
2:00remaining
Why use np.ix_() instead of broadcasting arrays directly?
Why is np.ix_() preferred for open mesh indexing over directly broadcasting arrays for indexing in NumPy?
Anp.ix_ ensures the indexing arrays have correct shapes to broadcast without ambiguity, preventing shape mismatch errors.
Bnp.ix_ compresses the indexing arrays to reduce memory usage.
Cnp.ix_ converts arrays to lists for compatibility with older NumPy versions.
Dnp.ix_ automatically sorts the indices to optimize indexing speed.
Attempts:
2 left
💡 Hint
Think about how np.ix_ shapes arrays for broadcasting.