w3resource

NumPy Masked Arrays: Exercises and Solutions


NumPy Masked Arrays [ 20 exercises with solution]


Following exercises will help you practice using masked arrays to handle missing or invalid data in NumPy, enhancing your data manipulation and analysis skills.

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

1. 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. 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. Write a NumPy program to compute the mean of a masked array, ignoring the masked values.
Click me to see the sample solution

4. 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. 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. Write a NumPy program to create a masked array and count the number of masked elements.
Click me to see the sample solution

7. Write a NumPy program to perform element-wise addition of two masked arrays, maintaining the masks.
Click me to see the sample solution

8. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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: