w3resource

NumPy Masked Arrays: Exercises and Solutions


This resource offers a total of 100 NumPy Masked Arrays problems for practice. It includes 20 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

The following exercises focus on NumPy masked arrays, covering creation, modification, arithmetic operations, and data extraction while handling missing or excluded values efficiently. They help in performing computations while ignoring masked elements, making them essential for data analysis and preprocessing.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Masked Array Creation

Write a NumPy program that creates a masked array from a regular NumPy array with some specified values masked.

Click me to see the sample solution


2. Masked Array Fill

Write a NumPy program that creates a masked array and fills the masked values with a specified number.

Click me to see the sample solution


3. Mean of Masked Array

Write a NumPy program to compute the mean of a masked array, ignoring the masked values.

Click me to see the sample solution


4. Mask Elements Greater Than Specified Value

Write a NumPy program that creates a masked array where all elements greater than a specified value are masked.

Click me to see the sample solution


5. Replace Masked Values with Mean

Write a NumPy program to replace all masked values in a masked array with the mean of the unmasked elements.

Click me to see the sample solution


6. Count Masked Elements

Write a NumPy program to create a masked array and count the number of masked elements.

Click me to see the sample solution


7. Element-wise Addition of Masked Arrays

Write a NumPy program to perform element-wise addition of two masked arrays, maintaining the masks.

Click me to see the sample solution


8. Mask a Specified Row in a 2D Array

Write a NumPy program to create a masked array from a 2D NumPy array and mask all values in a specified row.

Click me to see the sample solution


9. Logical Condition Masking

Write a NumPy program that creates a masked array and uses logical operations to mask elements based on a condition.

Click me to see the sample solution


10. Maximum Value in Masked Array

Write a NumPy program to create a masked array and find the maximum value, ignoring the masked elements.

Click me to see the sample solution


11. Indices of Unmasked Elements

Write a NumPy program to create a masked array and find the indices of all unmasked elements.

Click me to see the sample solution


12. Convert Masked Array to Regular Array

Write a NumPy program to create a masked array and convert it back to a regular NumPy array, replacing the masked values with NaN.

Click me to see the sample solution


13. Masked Operation: Multiplication

Write a NumPy program that creates a masked array and performs a masked operation, such as masked multiplication.

Click me to see the sample solution


14. Apply Custom Function to Unmasked Elements

Write a NumPy program that creates a masked array and applies a custom function only to the unmasked elements.

Click me to see the sample solution


15. Extract Unmasked Data

Write a NumPy program that creates a masked array and extracts the unmasked data as a regular NumPy array.

Click me to see the sample solution


16. Masked Sum Operation

Write a NumPy program to create a masked array and perform a masked sum operation, ignoring the masked elements.

Click me to see the sample solution


17. Masked Array with Complex Condition

Write a NumPy program that creates a masked array from a regular array and mask values based on a complex condition.

Click me to see the sample solution


18. Modify Existing Mask

Write a NumPy program that creates a masked array and changes the mask to include additional elements.

Click me to see the sample solution


19. Apply Math Function on Unmasked Elements

Write a NumPy program that creates a masked array and applies a mathematical function (e.g., sin, cos) only to the unmasked elements.

Click me to see the sample solution


20. Check for Presence of Masked Elements

Write a NumPy program that creates a masked array and checks if any masked elements are present.

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.