返回 Masked 数组在 NumPy 中的副本
要返回 Masked 数组的副本,请在 Python Numpy 中使用 ma.MaskedArray.copy() 方法。order 参数控制副本的内存布局。‘C’表示 C 顺序,‘F’表示 F 顺序,‘A’表示如果 a 是 Fortran 连续的则为‘F’,否则为‘C’。‘K’表示尽可能与 a 的布局相匹配。(请注意,此函数和 numpy.copy 非常相似,但 for order = 的默认值不同,并且此函数总是传递子类。)
步骤
首先,导入所需的库 −
import numpy as np import numpy.ma as ma
使用 numpy.array() 方法创建包含 int 元素的数组 −
arr = np.array([[55, 85, 68, 84], [67, 33, 39, 53], [29, 88, 51, 37], [56, 45, 99, 85]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)获取数组的维度 −
print("Array Dimensions...
",arr.ndim)
创建一个 Masked 数组并将其中一些作为无效项进行掩码 −
maskArr = ma.masked_array(arr, mask =[[1, 1, 0, 0], [ 0, 0, 1, 0], [0, 0, 0, 1], [0, 1, 0, 0]])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)获取 Masked 数组的维度 −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
获取 Masked 数组的形状 −
print("
Our Masked Array Shape...
",maskArr.shape)获取 Masked 数组中的元素数量 −
print("
Elements in the Masked Array...
",maskArr.size)
要返回 Masked 数组的副本,请使用 ma.MaskedArray.copy() 方法 −
resArr = maskArr.copy()
print("
Result...
",resArr)示例
import numpy as np
import numpy.ma as 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...
", 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 =[[1, 1, 0, 0], [ 0, 0, 1, 0],
[0, 0, 0, 1], [0, 1, 0, 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 the masked array, use the ma.MaskedArray.copy() method
resArr = maskArr.copy()
print("
Result...
",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]]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP