返回 NumPy 中填充给定值的掩码数组的副本


要返回自我的副本,其中掩码值填充了给定的值,请使用 ma.MaskedArray.filled() 方法。**“fill_value”** 参数是要用于无效条目的值。可以是标量或非标量。

fill_value 是用于无效条目的值。可以是标量或非标量。如果是非标量,则结果 ndarray 必须能够广播到输入数组上。默认为 None,在这种情况下,将改为使用数组的 fill_value 属性。

该方法返回自我的副本,其中无效条目替换为 *fill_value*(无论是函数参数还是自我的属性),或者如果没有任何无效条目需要替换,则返回自我本身作为 ndarray。

步骤

首先,导入所需的库 -

import numpy as np
import numpy.ma as ma

使用 numpy.array() 方法创建具有整数元素的数组 -

arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...
", arr) print("
Array type...
", arr.dtype)

获取数组的维度 -

print("Array Dimensions...
",arr.ndim)

创建一个掩码数组并将其中一些标记为无效 -

maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)

获取掩码数组的维度 -

print("
Our Masked Array Dimensions...
",maskArr.ndim)

获取掩码数组的形状 -

print("
Our Masked Array Shape...
",maskArr.shape)

获取掩码数组的元素数量 -

print("
Elements in the Masked Array...
",maskArr.size)

返回自我的副本,其中掩码值填充了给定的值,使用 ma.MaskedArray.filled() 方法。 "fill_value" 参数是要用于无效条目的值。可以是标量或非标量 -

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

示例

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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

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

Return Value...
[[ 49 85 111]
[ 67 111 59]]

更新于: 2022年2月2日

97 次浏览

启动您的 职业生涯

通过完成课程获得认证

开始
广告