0
0
SciPydata~20 mins

Performance tips and vectorization in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vectorization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
A[4, 6, 8, 10]
B[1, 3, 5, 7]
C[3, 5, 7, 9]
D[2, 4, 6, 8]
Attempts:
2 left
💡 Hint
Remember vectorized operations apply element-wise.
data_output
intermediate
2: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)
A(3, 2)
B(4, 4)
C(3, 4)
D(2, 3)
Attempts:
2 left
💡 Hint
Matrix multiplication shape rule: (m,n) dot (n,p) = (m,p).
🔧 Debug
advanced
2: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])
AIndexError: index out of bounds
BTypeError: unsupported operand type(s) for *: 'csr_matrix' and 'list'
CNo error, outputs a sparse matrix
DValueError: dimension mismatch
Attempts:
2 left
💡 Hint
Check the shapes of the sparse matrix and the array for multiplication compatibility.
🚀 Application
advanced
2: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?
AUse list comprehension with math.sqrt for each element.
BUse np.sqrt(array) to compute all square roots at once.
CUse a for loop to compute sqrt for each element individually.
DConvert array to list and use map with math.sqrt.
Attempts:
2 left
💡 Hint
Vectorized NumPy functions are optimized for array operations.
🧠 Conceptual
expert
2:00remaining
Why vectorization improves performance in SciPy and NumPy?
Which explanation best describes why vectorization improves performance in SciPy and NumPy?
AVectorization reduces Python-level loops by using optimized C/Fortran code that runs faster.
BVectorization increases memory usage but slows down computation due to overhead.
CVectorization replaces all functions with GPU code by default.
DVectorization allows parallel execution on multiple CPUs automatically without any code changes.
Attempts:
2 left
💡 Hint
Think about how vectorized operations avoid slow Python loops.