Challenge - 5 Problems
Array Processing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of element-wise multiplication using NumPy arrays
What is the output of this code that multiplies two NumPy arrays element-wise?
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) result = arr1 * arr2 print(result)
Attempts:
2 left
💡 Hint
Remember that NumPy arrays multiply element by element, not like lists.
✗ Incorrect
NumPy arrays multiply element-wise, so each element in arr1 is multiplied by the corresponding element in arr2.
❓ data_output
intermediate1:30remaining
Shape of the resulting array after reshaping
What is the shape of the array after reshaping a 1D array of 12 elements into a 3x4 array?
NumPy
import numpy as np arr = np.arange(12) reshaped = arr.reshape(3, 4) print(reshaped.shape)
Attempts:
2 left
💡 Hint
Reshape changes the shape but keeps the total number of elements the same.
✗ Incorrect
The reshape(3,4) changes the array to 3 rows and 4 columns, so shape is (3, 4).
❓ visualization
advanced3:00remaining
Visualizing the speed difference between list and NumPy array operations
Which option shows the correct plot comparing execution time of adding 1 million elements using Python lists vs NumPy arrays?
NumPy
import time import numpy as np import matplotlib.pyplot as plt size = 10**6 # Timing list addition start_list = time.time() list_data = list(range(size)) list_result = [x + 1 for x in list_data] end_list = time.time() # Timing numpy addition start_np = time.time() np_data = np.arange(size) np_result = np_data + 1 end_np = time.time() plt.bar(['List', 'NumPy'], [end_list - start_list, end_np - start_np]) plt.ylabel('Time in seconds') plt.title('Execution time: List vs NumPy addition') plt.show()
Attempts:
2 left
💡 Hint
NumPy uses optimized C code and vectorized operations, so it is faster.
✗ Incorrect
NumPy operations are faster than Python list comprehensions, so the bar for NumPy time is shorter.
🧠 Conceptual
advanced1:30remaining
Why is array broadcasting important in NumPy?
Which statement best explains why broadcasting is important in NumPy array operations?
Attempts:
2 left
💡 Hint
Think about how NumPy handles operations on arrays with different shapes.
✗ Incorrect
Broadcasting lets NumPy perform operations on arrays with different shapes by 'stretching' smaller arrays without copying data.
🔧 Debug
expert2:00remaining
Identify the error in this NumPy array operation
What error will this code raise and why?
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[1, 2], [3, 4]]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Check if the shapes of the arrays can be broadcast together.
✗ Incorrect
The shapes (3,) and (2,2) cannot be broadcast together because their dimensions are incompatible.