- 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
Convert Masked Array elements to Float Type in Numpy
To convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy. 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 using the numpy.array() method −
arr = np.array([30]) print("Array...", arr) print("
Array type...", arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...",arr.ndim)
Create a masked array −
maskArr = ma.masked_array(arr, mask =[False]) 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)
Convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy −
print("
Result Converted to float type...
",maskArr.__float__())
Example
import numpy as np import numpy.ma as ma # Create an array using the numpy.array() method arr = np.array([30]) print("Array...", arr) print("
Array type...", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...",arr.ndim) # Create a masked array maskArr = ma.masked_array(arr, mask =[False]) 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) # To convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy print("
Result Converted to float type...
",maskArr.__float__())
Output
Array... [30] Array type... int64 Array Dimensions... 1 Our Masked Array [30] Our Masked Array type... int64 Our Masked Array Dimensions... 1 Result Converted to float type... 30.0
- Related Articles
- Convert masked array element to int Type in NumPy
- Convert the input to a masked array of the given data-type in Numpy
- Repeat elements of a masked array in Numpy
- Count the non-masked elements of the masked array in Numpy
- Convert the input to a masked array conserving subclasses in Numpy
- Return the average of the masked array elements in Numpy
- Compute the median of the masked array elements in Numpy
- Return the variance of the masked array elements in Numpy
- Return the default fill value for a masked array with float datatype in Numpy
- Count the non-masked elements of the masked array along axis 0 in Numpy
- Count the non-masked elements of the masked array along axis 1 in Numpy
- How to convert Float array list to float array in Java?
- Repeat elements of a masked array along given axis in NumPy
- Repeat elements of a masked array along axis 1 in NumPy
- Repeat elements of a masked array along axis 0 in NumPy

Advertisements