w3resource

Apply Mathematical function to Unmasked elements in NumPy Masked array

NumPy: Masked Arrays Exercise-19 with Solution

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

Sample Solution:

Python Code:

import numpy as np
import numpy.ma as ma

# Create a 2D NumPy array of shape (5, 5) with random integers
array_2d = np.random.randint(0, 100, size=(5, 5))

# Define a condition to mask elements less than 20
condition = array_2d < 20

# Create a masked array from the 2D array using the condition
masked_array = ma.masked_array(array_2d, mask=condition)

# Apply the sin function only to the unmasked elements
result_array = ma.sin(masked_array)

# Print the original array, the masked array, and the result array
print('Original 2D array:\n', array_2d)
print('Masked array (values < 20 are masked):\n', masked_array)
print('Result array after applying sin to unmasked elements:\n', result_array)

Output:

Original 2D array:
 [[65 60 56 96 90]
 [ 5  1 50 49  1]
 [12 95 84 46 94]
 [88 40 23 33 23]
 [48 13 72 97 32]]
Masked array (values < 20 are masked):
 [[65 60 56 96 90]
 [-- -- 50 49 --]
 [-- 95 84 46 94]
 [88 40 23 33 23]
 [48 -- 72 97 32]]
Result array after applying sin to unmasked elements:
 [[0.8268286794901034 -0.3048106211022167 -0.5215510020869119
  0.9835877454343449 0.8939966636005579]
 [-- -- -0.26237485370392877 -0.9537526527594719 --]
 [-- 0.683261714736121 0.7331903200732922 0.9017883476488092
  -0.24525198546765434]
 [0.03539830273366068 0.7451131604793488 -0.8462204041751706
  0.9999118601072672 -0.8462204041751706]
 [-0.7682546613236668 -- 0.25382336276203626 0.3796077390275217
  0.5514266812416906]]

Explanation:

  • Import Libraries:
    • Imported numpy as "np" for array creation and manipulation.
    • Imported numpy.ma as "ma" for creating and working with masked arrays.
  • Create 2D NumPy Array:
    • Create a 2D NumPy array named array_2d with random integers ranging from 0 to 99 and a shape of (5, 5).
  • Define Condition:
    • Define a condition to mask elements in the array that are less than 20.
  • Create Masked Array:
    • Create a masked array from the 2D array using ma.masked_array, applying the condition as the mask. Elements less than 20 are masked.
  • Apply Mathematical Function:
    • Apply the sin function to the masked array using ma.sin. This operation applies the function only to the unmasked elements, leaving the masked elements unchanged.
  • Print the original 2D array, the masked array, and the result array after applying the sin function to the unmasked elements.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Update Mask in NumPy Masked array to include additional elements.
Next: Check for Masked elements in NumPy Masked array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/numpy/apply-mathematical-function-to-unmasked-elements-in-numpy-masked-array.php