Challenge - 5 Problems
NumPy Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of NumPy broadcasting with different shapes
What is the output of the following code snippet?
NumPy
import numpy as np arr1 = np.array([[1, 2, 3], [4, 5, 6]]) arr2 = np.array([10, 20, 30]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Recall how NumPy broadcasts arrays with compatible shapes.
✗ Incorrect
NumPy broadcasts the 1D array arr2 across the rows of arr1, adding element-wise. The shapes (2,3) and (3,) are compatible for broadcasting.
❓ data_output
intermediate2:00remaining
Shape and size of a reshaped NumPy array
Given the code below, what are the shape and size of the array 'reshaped'?
NumPy
import numpy as np arr = np.arange(24) reshaped = arr.reshape(2, 3, 4) print(reshaped.shape, reshaped.size)
Attempts:
2 left
💡 Hint
The reshape method changes the shape but does not change the total number of elements.
✗ Incorrect
The array is reshaped into dimensions 2 by 3 by 4, keeping the total size 24 (2*3*4).
🔧 Debug
advanced2:00remaining
Identify the error in NumPy matrix multiplication
What error will the following code raise when executed?
NumPy
import numpy as np A = np.array([[1, 2], [3, 4]]) B = np.array([5, 6, 7]) C = np.dot(A, B) print(C)
Attempts:
2 left
💡 Hint
Check the shapes of the arrays before matrix multiplication.
✗ Incorrect
Matrix multiplication requires the inner dimensions to match. Here, A is (2,2) and B is (3,), so they cannot be multiplied.
❓ visualization
advanced3:00remaining
Plotting a 2D Gaussian distribution using NumPy and Matplotlib
Which option produces a correct 2D heatmap of a Gaussian distribution centered at (0,0)?
NumPy
import numpy as np import matplotlib.pyplot as plt x = np.linspace(-3, 3, 100) y = np.linspace(-3, 3, 100) X, Y = np.meshgrid(x, y) Z = np.exp(-(X**2 + Y**2)) plt.imshow(Z, extent=[-3,3,-3,3], origin='lower') plt.colorbar() plt.show()
Attempts:
2 left
💡 Hint
The Gaussian function decreases smoothly from the center.
✗ Incorrect
The code computes a 2D Gaussian and plots it as a heatmap with highest intensity at the center (0,0).
🧠 Conceptual
expert2:30remaining
Understanding memory layout in NumPy arrays
Which statement about NumPy array memory layout is TRUE?
Attempts:
2 left
💡 Hint
Think about how C and Fortran languages store multi-dimensional arrays.
✗ Incorrect
C-contiguous means row-major order (rows stored one after another). Fortran-contiguous means column-major order (columns stored one after another). This affects performance in some operations.