# Return a copy of self, with masked values filled with a given value in Numpy

To return a copy of self, with masked values filled with a given value, use the ma.MaskedArray.filled() method. The "fill_value" parameter is the value to use for invalid entries. Can be scalar or non-scalar.

The fill_value is the value to use for invalid entries. Can be scalar or non-scalar. If non-scalar, the resulting ndarray must be broadcastable over input array. Default is None, in which case, the fill_value attribute of the array is used instead.

The method returns a copy of self with invalid entries replaced by fill_value (be it the function argument or the attribute of self), or self itself as an ndarray if there are no invalid entries to be replaced.

## 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 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)


Return a copy of self, with masked values filled with a given value, use the ma.MaskedArray.filled() method. The "fill_value" parameter is the value to use for invalid entries. Can be scalar or nonscalar −

print("Return Value...",maskArr.filled(fill_value = 111))

## Example

# Python ma.MaskedArray - Return a copy of self, with masked values filled with a given value

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

# Get the dimensions of the Masked Array

# Get the shape of the Masked Array

# Get the number of elements of the Masked Array

# To return a copy of self, with masked values filled with a given value, use the ma.MaskedArray.filled() method

# The "fill_value" parameter is the value to use for invalid entries. Can be scalar or non-scalar.

# If non-scalar, the resulting ndarray must be broadcastable over input array
print("Return Value...",maskArr.filled(fill_value = 111))

## Output

Array...
[[49 85 45]
[67 33 59]]

Array type...
int64

Array Dimensions...
2

[[49 85 --]
[67 -- 59]]

int64

[ 67 111 59]]