Challenge - 5 Problems
Vectorization Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of vectorized vs loop sum
What is the output of the following code comparing vectorized sum and loop sum using numpy arrays?
NumPy
import numpy as np arr = np.array([1, 2, 3, 4, 5]) vectorized_sum = np.sum(arr) loop_sum = 0 for x in arr: loop_sum += x print(vectorized_sum, loop_sum)
Attempts:
2 left
💡 Hint
Think about how numpy's sum and a manual loop add numbers.
✗ Incorrect
Both numpy's sum and the manual loop add all elements, so both results are 15.
❓ data_output
intermediateResult shape after vectorized operation
Given the code below, what is the shape of the result array after the vectorized operation?
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4], [5, 6]]) result = arr * 2 print(result.shape)
Attempts:
2 left
💡 Hint
Multiplying by 2 does not change the shape of the array.
✗ Incorrect
Element-wise multiplication keeps the original shape (3 rows, 2 columns).
🔧 Debug
advancedIdentify the error in vectorized operation
What error does the following code raise?
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5]) result = arr1 + arr2
Attempts:
2 left
💡 Hint
Check if the arrays have compatible shapes for element-wise addition.
✗ Incorrect
Arrays with different shapes that cannot be broadcast cause a ValueError.
🚀 Application
advancedWhy vectorized operations are faster
Which reason best explains why vectorized operations in numpy are faster than Python loops?
Attempts:
2 left
💡 Hint
Think about how numpy is implemented under the hood.
✗ Incorrect
Numpy vectorized operations call fast compiled code, avoiding slow Python loops.
🧠 Conceptual
expertImpact of vectorization on large data processing
Which statement best describes the impact of vectorized operations on processing large datasets?
Attempts:
2 left
💡 Hint
Consider how vectorization affects loops and memory access.
✗ Incorrect
Vectorization reduces Python overhead and uses optimized memory access, speeding up large data processing.
