0
0
NumPydata~20 mins

Element-wise arithmetic in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Element-wise Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
[[1 2 3]
 [10 20 30]
 [11 22 33]]
B
[[10 20 30]
 [11 21 31]
 [12 22 32]]
C
[[11 12 13]
 [21 22 23]
 [31 32 33]]
D
[[11 22 33]
 [12 23 34]
 [13 24 35]]
Attempts:
2 left
💡 Hint
Remember how broadcasting works when adding a 1D array to a 2D array.
data_output
intermediate
2: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)
A
Result:
[[ 2  4  6]
 [ 8 10 12]]
Shape: (2, 3)
B
Result:
[[ 2  6 12]
 [ 8 15 24]]
Shape: (2, 3)
C
Result:
[[ 2  6 12  8 15 24]]
Shape: (1, 6)
D
Result:
[[ 2  6]
 [ 8 15]
 [12 24]]
Shape: (3, 2)
Attempts:
2 left
💡 Hint
Check how the 1D array y is broadcast across the rows of x.
🔧 Debug
advanced
2: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)
ARuntimeWarning: divide by zero encountered in true_divide and result: [inf 1. 0.]
BTypeError: unsupported operand type(s) for /: 'int' and 'int'
CValueError: operands could not be broadcast together
DZeroDivisionError
Attempts:
2 left
💡 Hint
Consider how NumPy handles division by zero in arrays.
visualization
advanced
2: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()
ABar heights: [3, 2, 3, 2]
BBar heights: [-3, -2, -3, -2]
CBar heights: [7, 18, 27, 38]
DBar heights: [2, 8, 12, 18]
Attempts:
2 left
💡 Hint
Subtract each element of y from the corresponding element of x.
🧠 Conceptual
expert
3: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)
A
Data: [10 40 -- 160]
Mask: [False False True False]
B
Data: [10 40 90 160]
Mask: [False False False False]
C
Data: [10 -- 90 160]
Mask: [False True True False]
D
Data: [10 -- -- 160]
Mask: [False True True False]
Attempts:
2 left
💡 Hint
Remember that the mask in the result is the logical OR of the input masks.