Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a masked array in numpy?
A masked array is like a regular array but with a mask that hides or ignores certain elements during computations. It helps handle missing or invalid data easily.
Click to reveal answer
beginner
How do you create a masked array in numpy?
You can create a masked array using numpy.ma.array(data, mask=mask_array), where mask_array is a boolean array marking elements to hide.
Click to reveal answer
intermediate
What happens when you perform operations on masked arrays?
Operations ignore the masked elements, so calculations like sums or means only consider visible data, avoiding errors from missing or invalid values.
Click to reveal answer
beginner
How can masked arrays help with real-world data?
They help handle missing or corrupted data points without removing entire rows or columns, making analysis more accurate and easier.
Click to reveal answer
beginner
What is the difference between a masked array and a regular numpy array?
A masked array has an extra mask that marks elements to ignore, while a regular array treats all elements equally without hiding any.
Click to reveal answer
What does the mask in a numpy masked array do?
ADuplicates the array
BChanges the data type of elements
CSorts the array elements
DMarks elements to ignore in calculations
✗ Incorrect
The mask is a boolean array that marks which elements to ignore during computations.
How do masked arrays handle missing data?
AThey hide missing data using a mask
BThey replace missing data with zeros
CThey convert missing data to strings
DThey remove missing data permanently
✗ Incorrect
Masked arrays hide missing or invalid data using a mask, so calculations ignore those elements.
Which numpy module provides masked array functionality?
Anumpy.ma
Bnumpy.linalg
Cnumpy.random
Dnumpy.fft
✗ Incorrect
The masked array functions are in the numpy.ma module.
What is the output of sum on a masked array with some masked elements?
ASum of all elements including masked
BAlways zero
CSum ignoring masked elements
DError due to masked elements
✗ Incorrect
Sum ignores masked elements and adds only visible data.
Which of these is a valid way to create a masked array?
The correct way is to use numpy.ma.array with a mask argument.
Explain what a masked array is and why it is useful in data analysis.
Think about how you handle missing data in a spreadsheet.
You got /4 concepts.
Describe how you would create a masked array in numpy and perform a sum ignoring certain values.
Remember the mask is a boolean array matching the data.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using masked arrays in NumPy?
easy
A. To speed up array computations by using GPU
B. To sort arrays in ascending order
C. To convert arrays into lists
D. To mark certain data points as invalid without removing them
Solution
Step 1: Understand masked arrays concept
Masked arrays allow marking some elements as invalid or missing without deleting them.
Step 2: Compare options with concept
Only To mark certain data points as invalid without removing them correctly describes this purpose; others describe unrelated operations.
Final Answer:
To mark certain data points as invalid without removing them -> Option D
Quick Check:
Masked arrays = mark invalid data [OK]
Hint: Masked arrays hide invalid data without deleting it [OK]
Common Mistakes:
Thinking masked arrays delete invalid data
Confusing masked arrays with sorting or conversion
Assuming masked arrays speed up GPU computations
2. Which of the following is the correct way to create a masked array from a NumPy array arr where values equal to 0 are masked?
easy
A. np.ma.masked_array(arr, mask=arr == 0)
B. np.ma.masked_where(arr, arr == 0)
C. np.ma.masked_array(arr == 0)
D. np.ma.mask(arr, arr == 0)
Solution
Step 1: Recall masked_array syntax
The correct syntax is np.ma.masked_array(data, mask=condition).
Step 2: Match options with syntax
np.ma.masked_array(arr, mask=arr == 0) uses np.ma.masked_array with mask=arr == 0, which is correct. np.ma.masked_where(arr, arr == 0) reverses arguments, C misses data argument, D uses invalid function.
Final Answer:
np.ma.masked_array(arr, mask=arr == 0) -> Option A
A. np.ma.masked_array requires a list, not a NumPy array
B. Mask array shape does not match data array shape
C. Boolean mask must be integers, not booleans
D. Masked arrays cannot be created from 1D arrays
Solution
Step 1: Check shapes of data and mask
Data array shape is (3,), mask shape is (2,), which do not match.
Step 2: Understand mask shape requirement
Mask must have the same shape as data to apply element-wise masking.
Final Answer:
Mask array shape does not match data array shape -> Option B
Quick Check:
Mask shape must match data shape [OK]
Hint: Mask shape must match data shape exactly [OK]
Common Mistakes:
Using mask with different shape than data
Thinking mask must be integer array
Believing masked_array needs list input
5. You have a dataset with some invalid temperature readings marked as -999. How would you create a masked array to ignore these invalid values and then calculate the average temperature ignoring them?
hard
A. Use np.ma.masked_where(data != -999, data) and then call .mean()
B. Replace -999 with 0 and then calculate mean normally
C. Use np.ma.masked_array(data, mask=(data == -999)) and then call .mean()
D. Filter out -999 values manually and use np.array.mean()
Solution
Step 1: Mask invalid values correctly
Mask where data equals -999 to mark invalid readings.
Step 2: Calculate mean ignoring masked values
Calling .mean() on masked array ignores masked elements automatically.
Final Answer:
Use np.ma.masked_array(data, mask=(data == -999)) and then call .mean() -> Option C
Quick Check:
Mask invalids, then mean() ignores them [OK]
Hint: Mask invalids with condition, then use .mean() [OK]