在 NumPy 中返回掩码数组的副本
要返回掩码数组的副本,请使用ma. MaskedArray.copy()Python Numpy 中的方法。order 参数控制副本的内存布局。“C”表示 C 阶,“F”表示 F 阶,如果 a 是 Fortran 连续的,则“A”表示“F”,否则表示“C”。'K' 表示尽可能匹配 a 的布局。(请注意,此函数 和numpy.copy非常相似,但它们的 order = 参数具有不同的默认值,并且此函数始终传递子类。)
脚步
首先,导入所需的库 -
import numpy as npimportnumpy.maas ma
使用该方法创建一个包含 int 元素的数组-numpy.array()
arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])print("Array...\n", arr)
print("\nArray type...\n", arr.dtype)
获取数组的维度 -
print("Array Dimensions...\n",arr.ndim)
创建一个屏蔽数组并将其中一些屏蔽为无效 -
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
获取 Masked Array 的尺寸 -
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
获取蒙面阵列的形状 -
print("\nOur Masked Array Shape...\n",maskArr.shape)
获取 Masked Array 的元素数量 -
print("\nElements in the Masked Array...\n",maskArr.size)
要返回掩码数组的副本,请使用 ma. 方法 -MaskedArray.copy()
resArr = maskArr.copy()print("\nResult...\n",resArr)
示例
import numpy as np输出结果importnumpy.maas ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37],[56, 45, 99, 85]])
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, 1, 0],
[0, 0, 0, 1], [0, 1, 0, 0]])
print("\nOur Masked Array\n", maskArr)
print("\nOur Masked Array type...\n", maskArr.dtype)
# Get the dimensions of the Masked Array
print("\nOur Masked Array Dimensions...\n",maskArr.ndim)
# Get the shape of the Masked Array
print("\nOur Masked Array Shape...\n",maskArr.shape)
# Get the number of elements of the Masked Array
print("\nElements in the Masked Array...\n",maskArr.size)
# To return a copy of the masked array, use the ma.MaskedArray.copy() method
resArr = maskArr.copy()
print("\nResult...\n",resArr)
Array...[[55 85 68 84]
[67 33 39 53]
[29 88 51 37]
[56 45 99 85]]
Array type...
int64
Array Dimensions...
2
Our Masked Array
[[-- -- 68 84]
[67 33 -- 53]
[29 88 51 --]
[56 -- 99 85]]
Our Masked Array type...
int64
Our Masked Array Dimensions...
2
Our Masked Array Shape...
(4, 4)
Elements in the Masked Array...
16
Result...
[[-- -- 68 84]
[67 33 -- 53]
[29 88 51 --]
[56 -- 99 85]]
以上是 在 NumPy 中返回掩码数组的副本 的全部内容, 来源链接: utcz.com/z/297114.html