返回带有无效数据的输入,并被 Numpy 中的填充值替换
要返回带有无效数据的输入并被填充值替换,请使用numpy. ma.fix_invalid()Python Numpy 中的方法。掩码数组是标准numpy.ndarray和掩码的组合。掩码要么是 nomask,表示关联数组的任何值都无效,要么是布尔数组,用于确定关联数组的每个元素的值是否有效。
脚步
首先,导入所需的库 -
import numpy as npimportnumpy.maas ma
使用该方法创建一个包含 int 元素的数组-numpy.array()
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
获取数组的维度 -
print("\nArray Dimensions...\n",arr.ndim)
创建一个屏蔽数组并将其中一些屏蔽为无效 -
maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]])print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
获取数组的维度 -
print("\nOur Masked Array Dimensions...\n",arr.ndim)
获取阵列的形状 -
print("\nOur Masked Array Shape...\n",arr.shape)
获取数组的元素数量 -
print("\nElements in the Masked Array...\n",arr.size)
要返回带有无效数据的输入并被填充值替换,请使用 numpy. Python Numpy 中的方法:ma.fix_invalid()
print("\nResult...\n",np.ma.fix_invalid(arr))
示例
import numpy as np输出结果importnumpy.maas ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[65, 68, 81], [93, 33, 39], [73, 88, 51], [62, 45, 67]])
print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[1, 1, 0], [ 0, 0, 0], [0, 1, 0], [0, 1, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
# Get the dimensions of the Array
print("\nOur Masked Array Dimensions...\n",arr.ndim)
# Get the shape of the Array
print("\nOur Masked Array Shape...\n",arr.shape)
# Get the number of elements of the Array
print("\nElements in the Masked Array...\n",arr.size)
# To return input with invalid data masked and replaced by a fill value, use the numpy.ma.fix_invalid() method in Python Numpy
print("\nResult...\n",np.ma.fix_invalid(arr))
Array...[[65 68 81]
[93 33 39]
[73 88 51]
[62 45 67]]
Array type...
int64
Array Dimensions...
2
Our Masked Array
[[-- -- 81]
[93 33 39]
[73 -- 51]
[62 -- 67]]
Our Masked Array type...
int64
Our Masked Array Dimensions...
2
Our Masked Array Shape...
(4, 3)
Elements in the Masked Array...
12
Result...
[[65 68 81]
[93 33 39]
[73 88 51]
[62 45 67]]
以上是 返回带有无效数据的输入,并被 Numpy 中的填充值替换 的全部内容, 来源链接: utcz.com/z/297107.html