使用 Numpy 中现有数组的属性来清空掩码数组
要使用现有数组的属性来清空掩码数组,请在 Python Numpy 中使用 ma.masked_all_like() 方法。掩码数组是标准 numpy.ndarray 和掩码的组合。掩码可以为无掩码(表示相关联的数组中没有值无效)或布尔值数组,它决定了相关联的数组的每个元素是有效还是无效。
步骤
首先,导入必需的库 −
import numpy as np import numpy.ma as ma
使用 Python Numpy 中的 numpy.array() 方法创建一个新数组 −
arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]], dtype=np.float32)
显示我们的数组 −
print("Array...
",arr)获取数据类型 −
print("
Array datatype...
",arr.dtype)
要使用现有数组的属性来清空掩码数组,请使用 ma.masked_all_like() −
arr = ma.masked_all_like(arr)
显示我们的数组 −
print("
New Array...
",arr)
获取数据类型 −
print("
New Array datatype...
",arr.dtype)获取数组的维度 −
print("
Array Dimensions...
",arr.ndim)
获取数组的形状 −
print("
Our Array Shape...
",arr.shape)获取数组的元素数量 −
print("
Elements in the Array...
",arr.size)
示例
# Python ma.MaskedArray - Empty masked array with the properties of an existing array
import numpy as np
import numpy.ma as ma
# Create a new array using the numpy.array() method in Python Numpy
arr = np.array([[77, 51, 92], [56, 31, 69], [73, 88, 51], [62, 45, 67]], dtype=np.float32)
# Displaying our array
print("Array...
",arr)
# Get the datatype
print("
Array datatype...
",arr.dtype)
# To empty masked array with the properties of an existing array, use the ma.masked_all_like() method in Python Numpy
arr = ma.masked_all_like(arr)
# Displaying our array
print("
New Array...
",arr)
# Get the datatype
print("
New Array datatype...
",arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr.size)输出
Array... [[77. 51. 92.] [56. 31. 69.] [73. 88. 51.] [62. 45. 67.]] Array datatype... float32 New Array... [[-- -- --] [-- -- --] [-- -- --] [-- -- --]] New Array datatype... float32 Array Dimensions... 2 Our Array Shape... (4, 3) Elements in the Array... 12
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP