0
0
NumPydata~10 mins

Masked arrays concept in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Masked arrays concept
Create normal array
Define mask (True=hide, False=show)
Apply mask to array
Use masked array for calculations
Masked elements ignored in output
Start with a normal array, define which elements to hide using a mask, apply it, then use the masked array where hidden elements are ignored.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = [False, True, False, True, False]
masked_arr = np.ma.masked_array(arr, mask=mask)
print(masked_arr)
Create an array, mask some elements, and print the masked array showing hidden elements.
Execution Table
StepActionArrayMaskMasked Array Output
1Create array[1 2 3 4 5][False False False False False][1 2 3 4 5]
2Define mask[1 2 3 4 5][False True False True False][1 -- 3 -- 5]
3Apply mask[1 2 3 4 5][False True False True False][1 -- 3 -- 5]
4Print masked array[1 2 3 4 5][False True False True False][1 -- 3 -- 5]
5Sum masked array[1 2 3 4 5][False True False True False]9 (2 and 4 ignored)
6Exit--Mask applied, masked elements hidden
💡 Mask applied, masked elements are hidden and ignored in calculations
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrNone[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
maskNone[False False False False False][False True False True False][False True False True False][False True False True False]
masked_arrNoneNoneNone[1 -- 3 -- 5][1 -- 3 -- 5]
Key Moments - 3 Insights
Why do masked elements show as '--' instead of their original values?
Masked elements are hidden by design to indicate they are ignored. The '--' shows these values are masked, as seen in execution_table rows 2-4.
Does the mask change the original array values?
No, the original array stays the same. The mask only hides elements in the masked array view, shown in variable_tracker where 'arr' stays unchanged.
Are masked elements included in calculations like sum?
No, masked elements are ignored in calculations. For example, in execution_table step 5, sum excludes masked values 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2. What is the mask applied to the array?
A[True False True False True]
B[False False False False False]
C[False True False True False]
D[True True True True True]
💡 Hint
Check the 'Mask' column at step 2 in the execution_table.
At which step does the masked array first show masked elements as '--'?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Masked Array Output' column in execution_table rows 1 and 2.
If the mask was all False, what would the masked array output be at step 3?
A[1 2 3 4 5]
B[1 -- 3 -- 5]
C[-- -- -- -- --]
D[0 0 0 0 0]
💡 Hint
Refer to variable_tracker for mask values and their effect on masked_arr.
Concept Snapshot
Masked arrays let you hide elements in data.
Use a mask of True/False to mark hidden elements.
Masked elements show as '--' and are ignored in calculations.
Original data stays unchanged.
Useful for ignoring invalid or missing data.
Full Transcript
Masked arrays in numpy allow you to hide certain elements in an array using a mask. The mask is a list or array of True or False values, where True means the element is hidden. When you apply the mask, the masked array shows hidden elements as '--'. These masked elements do not affect calculations like sum or mean. The original array remains unchanged. This is helpful when you want to ignore some data points without deleting them.