使用 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)

示例

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

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

输出

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

更新日期:03-Feb-2022

228 次浏览

启动你的 事业

完成课程并获得认证

开始
广告