Challenge - 5 Problems
Masked Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of masked array sum with masked elements
What is the output of this code snippet using NumPy masked arrays?
NumPy
import numpy as np arr = np.ma.array([1, 2, 3, 4, 5], mask=[0, 1, 0, 1, 0]) result = arr.sum() print(result)
Attempts:
2 left
💡 Hint
Remember that masked elements are ignored in calculations like sum.
✗ Incorrect
The masked array has elements 2 and 4 masked. So sum adds 1 + 3 + 5 = 9.
❓ data_output
intermediate2:00remaining
Resulting masked array after applying mask condition
Given this code, what is the resulting masked array?
NumPy
import numpy as np arr = np.array([10, 20, 30, 40, 50]) masked_arr = np.ma.masked_greater(arr, 25) print(masked_arr)
Attempts:
2 left
💡 Hint
masked_greater masks elements greater than the given value.
✗ Incorrect
Elements greater than 25 (30, 40, 50) are masked, so they appear as '--'.
🔧 Debug
advanced2:00remaining
Identify the error in masked array creation
What error does this code raise?
NumPy
import numpy as np arr = np.ma.array([1, 2, 3], mask=[True, False])
Attempts:
2 left
💡 Hint
Check if the mask length matches the data length.
✗ Incorrect
The mask length is 2 but data length is 3, causing a ValueError.
🚀 Application
advanced2:00remaining
Using masked arrays to ignore invalid data in mean calculation
You have sensor data with invalid readings marked as -999. Which code correctly calculates the mean ignoring invalid data?
Attempts:
2 left
💡 Hint
Mask exactly the invalid value -999 to exclude it from calculations.
✗ Incorrect
Option D masks all -999 values and calculates mean ignoring them. Others do not mask correctly.
🧠 Conceptual
expert2:00remaining
Understanding behavior of masked array fill_value
What is the purpose of the fill_value attribute in a NumPy masked array?
Attempts:
2 left
💡 Hint
Think about how masked elements are represented when converting to normal arrays.
✗ Incorrect
fill_value is used to fill masked elements when converting masked arrays to normal arrays or printing.