# Mask array elements equal to a given value in Numpy

To mask an array where equal to a given value, use the numpy.ma.masked_equal() method in Python Numpy. This function is a shortcut to masked_where, with condition = (x == value). For floating point arrays, consider using masked_values(x, value).

A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.

## Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[74, 55, 91], [93, 33, 39], [73, 93, 51], [93, 45, 67]])
print("Array...", arr)
print("Array type...", arr.dtype)

Get the dimensions of the Array −

print("Array Dimensions...",arr.ndim)


Get the shape of the Array −

print("Our Array Shape...",arr.shape)

Get the number of elements of the Array −

print("Elements in the Array...",arr.size)


To mask an array where equal to a given value, use the numpy.ma.masked_equal() method −

print("Result...",np.ma.masked_equal(arr, 93))

## Example

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[74, 55, 91], [93, 33, 39], [73, 93, 51], [93, 45, 67]])
print("Array...", arr)
print("Array type...", arr.dtype)

# Get the dimensions of the Array
print("Array Dimensions...",arr.ndim)

# Get the shape of the Array
print("Our Array Shape...",arr.shape)

# Get the number of elements of the Array
print("Elements in the Array...",arr.size)

# To mask an array where equal to a given value, use the numpy.ma.masked_equal() method in Python Numpy
print("Result...",np.ma.masked_equal(arr, 93))

## Output

Array...
[[74 55 91]
[93 33 39]
[73 93 51]
[93 45 67]]

Array type...
int64

Array Dimensions...
2

Our Array Shape...
(4, 3)

Elements in the Array...
12

Result...
[[74 55 91]
[-- 33 39]
[73 -- 51]
[-- 45 67]]