Challenge - 5 Problems
Vectorization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of vectorized operation with NumPy arrays
What is the output of this code snippet using NumPy vectorization?
SciPy
import numpy as np arr = np.array([1, 2, 3, 4]) result = arr * 2 + 1 print(result.tolist())
Attempts:
2 left
💡 Hint
Remember vectorized operations apply element-wise.
✗ Incorrect
Each element in the array is multiplied by 2, then 1 is added. So 1*2+1=3, 2*2+1=5, etc.
❓ data_output
intermediate2:00remaining
Resulting shape after vectorized matrix multiplication
Given two NumPy arrays A with shape (3, 4) and B with shape (4, 2), what is the shape of the result after np.dot(A, B)?
SciPy
import numpy as np A = np.ones((3, 4)) B = np.ones((4, 2)) result = np.dot(A, B) print(result.shape)
Attempts:
2 left
💡 Hint
Matrix multiplication shape rule: (m,n) dot (n,p) = (m,p).
✗ Incorrect
The dot product of (3,4) and (4,2) results in shape (3,2).
🔧 Debug
advanced2:00remaining
Identify the error in vectorized code using SciPy sparse matrix
What error will this code raise when executed?
SciPy
from scipy.sparse import csr_matrix import numpy as np sparse_mat = csr_matrix([[1, 0], [0, 2]]) result = sparse_mat * np.array([1, 2, 3])
Attempts:
2 left
💡 Hint
Check the shapes of the sparse matrix and the array for multiplication compatibility.
✗ Incorrect
The sparse matrix shape is (2,2) but the array has shape (3,), so multiplication fails due to dimension mismatch.
🚀 Application
advanced2:00remaining
Choosing vectorized approach for element-wise operation
You want to compute the square root of each element in a large NumPy array efficiently. Which approach is fastest and uses vectorization?
Attempts:
2 left
💡 Hint
Vectorized NumPy functions are optimized for array operations.
✗ Incorrect
np.sqrt is a vectorized function that applies sqrt element-wise efficiently without Python loops.
🧠 Conceptual
expert2:00remaining
Why vectorization improves performance in SciPy and NumPy?
Which explanation best describes why vectorization improves performance in SciPy and NumPy?
Attempts:
2 left
💡 Hint
Think about how vectorized operations avoid slow Python loops.
✗ Incorrect
Vectorization uses compiled code underneath to perform operations on whole arrays at once, avoiding slow Python loops.