In this article, we will learn how to mask an array using another array in Python. When working with data arrays or data-frames masking can be extremely useful. Masks are an array that contains the list of boolean values for the given condition. The masked array is the arrays that have invalid or missing entries.
Using Masking of arrays we can easily handle the missing, invalid, or unwanted entries in our array or dataset/dataframe. Masking is essential works with the list of Boolean values i.e, True or False which when applied to an original array to return the element of interest, here True refers to the value that satisfies the given condition whereas False refers to values that fail to satisfy the condition.
We can mask the array using another by using the following functions:-
numpy.ma.masked_where(condition, arr)
numpy.ma.getmask(arr)
numpy.ma.masked_array(arr, mask=)
where,
condition: condition for masking
arr: arr to be masked
mask: result of masked array
Steps Required
- Import the library.
- Create a function for masking.
- Masking can be done by following two approaches:-
- Using masked_where() function: Pass the two array in the function as a parameter then use numpy.ma.masked_where() function in which pass the condition for masking and array to be masked. In this we are giving the condition for masking by using one array and masking the another array for that condition.
- Using masked_where(), getmask() and masked_array() function: Pass the two array in the function as a parameter then use numpy.ma.masked_where() function in which pass the condition for masking and array to be masked in this we are using the same array for which we are giving condition for making and the array to be masked and store the result in the variable, then use numpy.ma.getmask() function in which pass the result of marked_where function and store it in the variable named as ‘res_mask’. Now mask another array using the created mask, for this, we are using numpy.ma.masked_array() function in which pass the array to be made and the parameter mask=’res_mask’ for making the array using another array and store it in a variable let be named as ‘masked’.
- Then return the masked from the function.
- Now create the main function
- Create two arrays one for masking another.
- Then call the function as we have created above and pass both the arrays in the function as a parameter and store the result in a variable let named ‘masked’.
- Now for getting the array as a 1-d array we are using numpy.ma.compressed() which passes the masked as a parameter.
- Then print the Masked array.
Example 1: Masking the first array using the second array
In the above example, we are masking the first array using the second array on the basis of the condition that each element of the first array mod 7 is true, those elements which satisfy the condition at that index elements are masked in the first array.
Since we have the array1 = [1,2,4,5,7,8,9] and array2 = [10,12,14,5,7,0,13], we have given the condition array2%7 so in array2 element 14, 7 and 0 satisfies the condition, and they are present at index 2,4 and 5 so at the same index in array1 elements are masked so the resultant array we have [4 7 8].
Python
import numpy as np
def masking(ar1, ar2):
mask = np.ma.masked_where(ar2 % 7 ,ar1)
return mask
if __name__ = = '__main__' :
x = np.array([ 1 , 2 , 4 , 5 , 7 , 8 , 9 ])
y = np.array([ 10 , 12 , 14 , 5 , 7 , 0 , 13 ])
masked = masking(x,y)
masked_array = np.ma.compressed(mask)
print (f 'Masked Array is:{masked_array}' )
|
Output:

Example 2: Masking the second array using the first array
In the above example, we are masking the second array using the first array, giving the condition array1<5 means the elements of array1 which are less than 5 are satisfying the condition and the index of that element will be masked in the second array.
Since we have array1 = [1,2,4,5,7,8,9] and array2 = [10,12,14,5,7,0,13], so in array1 elements 1,2 and 4 are less than 5 these are present at index 0,1 and 2, so this element satisfies the condition so in array2 the elements present at the same index are masked, and we are using the function numpy.ma.compressed() so this function returns the non mask values. So that we are having [5 7 0 10] after masking.
Python
import numpy as np
def masking(ar1, ar2):
mask = np.ma.masked_where(ar1 < 5 , ar2)
return mask
if __name__ = = '__main__' :
x = np.array([ 1 , 2 , 4 , 5 , 7 , 8 , 9 ])
y = np.array([ 10 , 12 , 14 , 5 , 7 , 0 , 13 ])
masked = masking(x, y)
masked_array = np.ma.compressed(mask)
print (f 'Masked Array is:{masked_array}' )
|
Output:

Example 3: Masking the first array using the second array though getmask() function
In the above example, for making the mask of the first array using the second array, firstly we are creating the mask of the second array by giving the condition ar2%3 for ar2. Then we are using numpy.ma.getmask() function in which we are passing the result of the created mask, then we are creating the mask of the first array by using numpy.ma.masked_array() in which pass ar1 and pass mask=res_mask which is the mask of array2.
In this way, we can do the masking of one array using another array.
Python
import numpy as np
def masking(ar1, ar2):
mask = np.ma.masked_where(ar2 % 3 , ar2)
res_mask = np.ma.getmask(mask)
masked = np.ma.masked_array(ar1, mask = res_mask)
return masked
if __name__ = = '__main__' :
x = np.array([ 1 , 2 , 4 , 5 , 7 , 8 , 9 ])
y = np.array([ 10 , 12 , 14 , 5 , 7 , 0 , 12 ])
masked = masking(x, y)
masked_array = np.ma.compressed(masked)
print (f 'Masked Array is:{masked_array}' )
|
Output:

Example 4: Masking the second array using the first array though getmask() function
In the above example, for making the mask of the second array using the first array, firstly we are creating the mask of the first array by giving the condition ar1<4 for ar1. Then we are using numpy.ma.getmask() function in which we are passing the result of the created mask, then we are creating the mask of the second array by using numpy.ma.masked_array() in which pass ar2 and pass mask=res_mask which is the mask of array1.
In this way, we can do the masking of one array using another array.
Python
import numpy as np
def masking(ar1, ar2):
mask = np.ma.masked_where(ar2 % 3 , ar2)
res_mask = np.ma.getmask(mask)
masked = np.ma.masked_array(ar1, mask = res_mask)
return masked
if __name__ = = '__main__' :
x = np.array([ 1 , 2 , 4 , 5 , 7 , 8 , 9 ])
y = np.array([ 10 , 12 , 14 , 5 , 7 , 0 , 12 ])
masked = masking(x, y)
masked_array = np.ma.compressed(masked)
print (f 'Masked Array is:{masked_array}' )
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Apr, 2021
Like Article
Save Article