Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

numpy
communitycreator

What is the ma.masked_equal() function in NumPy?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In NumPy, the masked_equal() function is used to mask an array where the specified value is equal to the element of the given array.

Syntax

ma.masked_equal(x, value)
Syntax for the masked_equal() function

Parameters

This function takes the following parameter values:

  • x: This is the input array. This is a required parameter.
  • value: This is the value of the given array to be masked. This is a required parameter.

Return value

This function returns a masked array.

Example

# A code to illustrate the masked_equal() function
# importing the necessary libraries
import numpy as np
import numpy.ma as ma
# creating an input array
my_array = np.array([1, 2, 3, 4, 5])
# masking the array
mask_array =ma.masked_equal(my_array, 2)
print(mask_array)
Implementing the masked_equal() function

Explanation

  • Lines 4–5: We import the necessary library and module.
  • Line 8: We create an input array, my_array.
  • Line 11: We mask the value 2 of the input array using the masked_equal() function. The result is assigned to a variable, mask_array.
  • Line 13: We print the masked array, mask_array.

RELATED TAGS

numpy
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring