Challenge - 5 Problems
Element-wise Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of element-wise addition with broadcasting
What is the output of this code snippet using NumPy arrays?
NumPy
import numpy as np arr1 = np.array([1, 2, 3]) arr2 = np.array([[10], [20], [30]]) result = arr1 + arr2 print(result)
Attempts:
2 left
💡 Hint
Remember how broadcasting works when adding a 1D array to a 2D array.
✗ Incorrect
The 1D array arr1 is broadcast across each row of arr2, so each element of arr1 is added to each row element of arr2, resulting in a 3x3 array where each row is arr1 plus the corresponding element of arr2.
❓ data_output
intermediate2:00remaining
Result of element-wise multiplication with different shapes
Given these NumPy arrays, what is the shape and content of the result after element-wise multiplication?
NumPy
import numpy as np x = np.array([[1, 2, 3], [4, 5, 6]]) y = np.array([2, 3, 4]) result = x * y print(result) print(result.shape)
Attempts:
2 left
💡 Hint
Check how the 1D array y is broadcast across the rows of x.
✗ Incorrect
The array y with shape (3,) is broadcast to match the shape of x (2,3). Each element in y multiplies the corresponding column in x, resulting in element-wise multiplication with shape (2,3).
🔧 Debug
advanced2:00remaining
Identify the error in element-wise division
What error will this code raise when executed?
NumPy
import numpy as np arr1 = np.array([1, 2, 0]) arr2 = np.array([0, 2, 3]) result = arr1 / arr2 print(result)
Attempts:
2 left
💡 Hint
Consider how NumPy handles division by zero in arrays.
✗ Incorrect
NumPy does not raise a ZeroDivisionError for division by zero in arrays. Instead, it issues a RuntimeWarning and returns 'inf' or 'nan' where division by zero occurs.
❓ visualization
advanced2:00remaining
Visualizing element-wise subtraction result
Which plot correctly shows the element-wise subtraction of these two arrays?
NumPy
import numpy as np import matplotlib.pyplot as plt x = np.array([5, 10, 15, 20]) y = np.array([2, 8, 12, 18]) result = x - y plt.bar(range(len(result)), result) plt.xlabel('Index') plt.ylabel('Difference') plt.title('Element-wise subtraction result') plt.show()
Attempts:
2 left
💡 Hint
Subtract each element of y from the corresponding element of x.
✗ Incorrect
Element-wise subtraction subtracts each element in y from x, resulting in [3, 2, 3, 2]. The bar chart shows these values as bar heights.
🧠 Conceptual
expert3:00remaining
Understanding element-wise arithmetic with masked arrays
Given two masked arrays, what is the output of their element-wise multiplication?
NumPy
import numpy as np import numpy.ma as ma arr1 = ma.array([1, 2, 3, 4], mask=[False, True, False, False]) arr2 = ma.array([10, 20, 30, 40], mask=[False, False, True, False]) result = arr1 * arr2 print(result) print(result.mask)
Attempts:
2 left
💡 Hint
Remember that the mask in the result is the logical OR of the input masks.
✗ Incorrect
The multiplication masks elements where either input is masked. The mask is True where either arr1 or arr2 is masked, so positions 1 and 2 are masked. The data multiplies unmasked elements only.