- 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
Transform a masked array into a flexibletype array in Numpy
To transform a masked array into a flexible-type array, use the ma.MaskedArray.toflex() method in Numpy. The flexible type array that is returned will have two fields: the _data field stores the _data part of the array.
The method returns a new flexible-type ndarray with two fields: the first element containing a value, the second element containing the corresponding mask boolean. The returned record shape matches self.shape.
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([[49, 85, 45], [67, 33, 59]]) print("Array...
", arr) print("
Array type...
", arr.dtype)
Get the dimensions of the Array −
print("Array Dimensions...
",arr.ndim)
Create a masked array and mask some of them as invalid −
maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
Get the dimensions of the Masked Array −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
Get the shape of the Masked Array −
print("
Our Masked Array Shape...
",maskArr.shape)
Get the number of elements of the Masked Array −
print("
Elements in the Masked Array...
",maskArr.size)
Transform a masked array into a flexible-type array, use the ma.MaskedArray.toflex() method in Numpy. The flexible type array that is returned will have two fields: the _data field stores the _data part of the array −
print("
Result of the transformation...
",maskArr.toflex())
Example
# Python ma.MaskedArray - Transform a masked array into a flexibletype array import numpy as np import numpy.ma as ma # Create an array with int elements using the numpy.array() method arr = np.array([[49, 85, 45], [67, 33, 59]]) print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the shape of the Masked Array print("
Our Masked Array Shape...
",maskArr.shape) # Get the number of elements of the Masked Array print("
Elements in the Masked Array...
",maskArr.size) # To transform a masked array into a flexible-type array, use the ma.MaskedArray.toflex() method in Numpy # The flexible type array that is returned will have two fields: the _data field stores the _data part of the array. #, the _mask field stores the _mask part of the array. print("
Result of the transformation...
",maskArr.toflex())
Output
Array... [[49 85 45] [67 33 59]] Array type... int64 Array Dimensions... 2 Our Masked Array [[49 85 --] [67 -- 59]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 3) Elements in the Masked Array... 6 Result of the transformation... [[(49, False) (85, False) (45, True)] [(67, False) (33, True) (59, False)]]
- Related Articles
- Transform a masked array into a flexibletype array with torecords() in Numpy
- Divide a scalar value into every element of a masked Array in NumPy
- Return a copy of the masked array collapsed into one dimension in Numpy
- Repeat elements of a masked array in Numpy
- Divide a scalar value into every element of a masked Array with __truediv__() in NumPy
- Divide every element of a masked Array into a scalar value with __rtruediv__() in NumPy
- Return specified diagonals from a masked array in NumPy
- Check the base of a masked array in NumPy
- Get the datatype of a masked array in NumPy
- Return a copy of the masked array in NumPy
- Dump a pickle of the masked array in NumPy
- Return the mask of a masked array in Numpy
- Find contiguous unmasked data in a masked array in Numpy
- Return the absolute value of a masked Array in NumPy
- Create a new array from the masked array and return a new reference in Numpy
