Challenge - 5 Problems
Broadcasting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Broadcasting with large arrays performance
What is the output of the following code snippet regarding the shape of the result array?
NumPy
import numpy as np large_array = np.ones((1000, 1000)) small_array = np.array([1, 2, 3, 4, 5]) result = large_array + small_array print(result.shape)
Attempts:
2 left
💡 Hint
Remember how broadcasting works when adding arrays of different shapes.
✗ Incorrect
The small array has shape (5,), which cannot broadcast with the large array of shape (1000, 1000) because the dimensions are incompatible. This results in a ValueError.
❓ data_output
intermediate2:00remaining
Memory usage difference in broadcasting
Given these two operations, which one uses less memory during execution?
NumPy
import numpy as np x = np.ones((1000, 1000)) y = np.ones((1, 1000)) result1 = x + y result2 = x + y.T
Attempts:
2 left
💡 Hint
Broadcasting tries to avoid copying data by creating views.
✗ Incorrect
Broadcasting creates views without copying data, so both operations use similar memory during execution.
❓ visualization
advanced3:00remaining
Visualizing broadcasting performance impact
Which option correctly plots the time taken to add arrays with and without broadcasting?
NumPy
import numpy as np import matplotlib.pyplot as plt import time sizes = [10, 100, 1000, 5000] time_broadcast = [] time_no_broadcast = [] for size in sizes: a = np.ones((size, size)) b = np.ones((size, 1)) start = time.time() c = a + b time_broadcast.append(time.time() - start) b2 = np.ones((size, size)) start = time.time() c2 = a + b2 time_no_broadcast.append(time.time() - start) plt.plot(sizes, time_broadcast, label='Broadcasting') plt.plot(sizes, time_no_broadcast, label='No Broadcasting') plt.xlabel('Array size (N x N)') plt.ylabel('Time (seconds)') plt.legend() plt.title('Performance: Broadcasting vs No Broadcasting') plt.show()
Attempts:
2 left
💡 Hint
Broadcasting avoids copying large arrays, improving speed.
✗ Incorrect
Broadcasting allows operations without copying data, so it is faster especially for large arrays.
🔧 Debug
advanced2:00remaining
Identify the cause of slow broadcasting operation
Why does this broadcasting operation run slower than expected?
NumPy
import numpy as np x = np.ones((1000, 1000)) y = np.ones((1000, 1)) result = x + y.T # Broadcasting with transpose
Attempts:
2 left
💡 Hint
Check if transpose creates a copy or a view.
✗ Incorrect
Transposing y changes its memory layout and may create a copy, which slows down broadcasting.
🚀 Application
expert3:00remaining
Optimizing broadcasting in a real-world scenario
You have a dataset with shape (10000, 50) and a vector of shape (50,). You want to add the vector to each row efficiently. Which approach is best for performance?
Attempts:
2 left
💡 Hint
Consider how broadcasting works with shapes and vectorization.
✗ Incorrect
Direct addition uses broadcasting efficiently without loops or reshaping, providing best performance.