- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 when mask is equal to nomask
To return the mask of a masked array, use the ma.getmaskarray() method in Python Numpy. Returns the mask of arr as an ndarray if arr is a MaskedArray and the mask is not nomask, else return a full boolean array of False of the same shape as arr.
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...
", 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 Masked Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Masked Array...
",arr.size)
Create a masked array. Here, mask == nomask −
arr = ma.array(arr)
To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() method −
print("
The number of masked elements...
",ma.count_masked(arr))
To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy −
print("
Result (mask of a masked array)...
",ma.getmaskarray(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...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) print("
Our Array type...
", arr.dtype) # Get the shape of the Array print("
Our Masked Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Masked Array...
",arr.size) # Create a masked array # Here, mask == nomask arr = ma.array(arr) # To count the number of masked elements along specific axis, use the ma.MaskedArray.count_masked() print("
The number of masked elements...
",ma.count_masked(arr)) # To return the mask of a masked array, or full boolean array of False, use the ma.getmaskarray() method in Python Numpy print("
Result (mask of a masked array)...
",ma.getmaskarray(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... 0 Result (mask of a masked array)... [[False False False False] [False False False False] [False False False False] [False False False False]]
- Related Articles
- Return the mask of a masked array in Numpy
- Reduce a mask to nomask when possible in Numpy
- 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
- Mask array elements equal to a given value 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
- Mask array elements not equal to a given value in Numpy
- Mask an array where the data is exactly equal to value in Numpy
- Mask array elements greater than or equal to a given value in Numpy
- Mask rows and/or columns of a 2D array that contain masked values in Numpy
- Mask an array where less than or equal to a given value in Numpy
- Mask rows and/or columns of a 2D Numpy array that contain masked values along negative axis
- Python Pandas - Return a new Index of the values set with the mask
- Mask an array where a condition is met in Numpy
