# Convert Masked Array elements to Float Type in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

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()
print("Array...", arr)
print("\nArray type...", arr.dtype)

Get the dimensions of the Array −

print("\nArray Dimensions...",arr.ndim)

Create a masked array −

maskArr = ma.masked_array(arr, mask =[False])
print("\nOur Masked Array type...\n", maskArr.dtype)

Get the dimensions of the Masked Array −

print("\nOur Masked Array Dimensions...\n",maskArr.ndim)

Convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy −

print("\nResult Converted to float type...\n",maskArr.__float__())

## Example

import numpy as np
import numpy.ma as ma

# Create an array using the numpy.array() method
arr = np.array()
print("Array...", arr)
print("\nArray type...", arr.dtype)

# Get the dimensions of the Array
print("\nArray Dimensions...",arr.ndim)

# Create a masked array

# Get the dimensions of the Masked Array

# To convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy
print("\nResult Converted to float type...\n",maskArr.__float__())

## Output

Array... 

Array type... int64

Array Dimensions... 1

30.0