- 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 underlying data as a view of the masked array in Numpy
To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data in Python Numpy.
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 −
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("
The number of masked elements...
",ma.count_masked(arr, axis = 1))
Return the mask of a masked array −
print("
The mask of a masked array)...
",ma.getmask(arr))
Return the data of a masked array as an ndarray −
print("
Data of a masked array as an ndarray...
",ma.getdata(arr))
Determine whether input is an instance of masked array −
print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr))
To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data −
print("
Result...
",arr.data)
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 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("
The number of masked elements...
",ma.count_masked(arr, axis = 1)) # Return the mask of a masked array print("
The mask of a masked array)...
",ma.getmask(arr)) # Return the data of a masked array as an ndarray print("
Data of a masked array as an ndarray...
",ma.getdata(arr)) # Determine whether input is an instance of masked array print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr)) # To return the underlying data, as a view of the masked array, use the ma.MaskedArray.data in Python Numpy print("
Result...
",arr.data)
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] The mask of a masked array)... [[False True False False] [False True False False] [False True True False] [ True False True True]] Data of a masked array as an ndarray... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Whether input is an instance of masked array? True Result... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]]
- Related Articles
- Return a view of the masked array with axes transposed in NumPy
- Return all the non-masked data as a 1-D array in Numpy
- Return the pickle of the masked array as a string in NumPy
- Return a view of the masked array with axis1 and axis2 interchanged in Numpy
- Return a view of the masked array with axes transposed along given axis in NumPy
- Return the data of a masked array as an ndarray
- Return a view of the MaskedArray data in Numpy
- Return a copy of the masked array in NumPy
- Return the mask of a masked array in Numpy
- Return the addresses of the data and mask areas of a masked array in Numpy
- Return the transpose of the masked array in NumPy
- Return the length of the masked array in Numpy
- Return an empty masked array of the given shape where all the data are masked in Numpy
- Return the absolute value of a masked Array in NumPy
- Return the masked array data as a string containing the raw bytes in the array and fill the invalid entries in Numpy
