0
0
NumPydata~20 mins

Masked arrays concept in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Masked Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
Amasked
B15
C9
DTypeError
Attempts:
2 left
💡 Hint
Remember that masked elements are ignored in calculations like sum.
data_output
intermediate
2: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)
A[10 20 -- -- --]
B[-- -- -- -- --]
C[10 20 30 40 50]
D[-- -- 30 40 50]
Attempts:
2 left
💡 Hint
masked_greater masks elements greater than the given value.
🔧 Debug
advanced
2: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])
ATypeError: mask must be boolean
BValueError: mask and data must have the same shape
CIndexError: mask index out of range
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check if the mask length matches the data length.
🚀 Application
advanced
2: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?
Adata = np.array([1, 2, -999, 4]); mean = data.mean()
Bdata = np.array([1, 2, -999, 4]); masked = np.ma.masked_greater(data, 100); mean = masked.mean()
Cdata = np.array([1, 2, -999, 4]); masked = np.ma.masked_less(data, 0); mean = masked.mean()
Ddata = np.array([1, 2, -999, 4]); masked = np.ma.masked_equal(data, -999); mean = masked.mean()
Attempts:
2 left
💡 Hint
Mask exactly the invalid value -999 to exclude it from calculations.
🧠 Conceptual
expert
2:00remaining
Understanding behavior of masked array fill_value
What is the purpose of the fill_value attribute in a NumPy masked array?
AIt defines the value used when filling masked elements during output or conversion.
BIt sets the default value for all elements in the array.
CIt specifies the value to replace unmasked elements during calculations.
DIt controls the data type of the masked array elements.
Attempts:
2 left
💡 Hint
Think about how masked elements are represented when converting to normal arrays.