

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return the mask of a masked array in Numpy
To return the mask of a masked array, use the ma.getmask() method in Python Numpy. Returns the mask of a as an ndarray if a is a MaskedArray and the mask is not nomask, else return nomask. To guarantee a full array of booleans of the same shape as a, use getmaskarray.
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
Creating a 4x4 array with int elements using the numpy.arange() method −
arr = np.arange(16).reshape((4,4)) print("Array...\n", arr) print("\nArray type...\n", arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim) print("\nOur Array type...\n", arr.dtype)
Get the shape of the Array −
print("\nOur Masked Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Masked Array...\n",arr.size)
Create a masked array −
arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked
Count the number of masked elements along specific axis −
print("\nThe number of masked elements...\n",ma.count_masked(arr, axis = 1))
To return the mask of a masked array, use the ma.getmask() method in Python Numpy −
print("\nResult (mask of a masked array)...\n",ma.getmask(arr))
Example
import numpy as np import numpy.ma as ma # Creating a 4x4 array with int elements using the numpy.arange() method arr = np.arange(16).reshape((4,4)) print("Array...\n", arr) print("\nArray type...\n", arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) print("\nOur Array type...\n", arr.dtype) # Get the shape of the Array print("\nOur Masked Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Masked Array...\n",arr.size) # Create a masked array arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked # Count the number of masked elements along specific axis print("\nThe number of masked elements...\n",ma.count_masked(arr, axis = 1)) # To return the mask of a masked array, use the ma.getmask() method in Python Numpy print("\nResult (mask of a masked array)...\n",ma.getmask(arr))
Output
Array... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Array type... int64 Array Dimensions... 2 Our Array type... int64 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 The number of masked elements... [1 1 2 3] Result (mask of a masked array)... [[False True False False] [False True False False] [False True True False] [ True False True True]]
- Related Questions & Answers
- Return the mask of a masked array or full boolean array of False in Numpy
- Return the addresses of the data and mask areas of a masked array in Numpy
- Return the mask of a masked array when mask is equal to nomask
- Return a copy of the masked array in NumPy
- Return the transpose of the masked array in NumPy
- Return the length of the masked array in Numpy
- Return the absolute value of a masked Array in NumPy
- Mask columns of a 2D array that contain masked values in Numpy
- Mask rows of a 2D array that contain masked values in Numpy
- Return the average of the masked array elements in Numpy
- Return the variance of the masked array elements in Numpy
- Return the pickle of the masked array as a string in NumPy
- Return each element of the masked array rounded in Numpy
- Return array of indices of the maximum values from a masked array in NumPy
- Return array of indices of the minimum values from a masked array in NumPy