在 Numpy 中返回给定形状的空掩码数组,其中所有数据都被掩码


要返回给定形状和数据类型的空掩码数组,其中所有数据都被掩码,请在 Python Numpy 中使用 **ma.masked_all()** 方法。第一个参数设置所需的 MaskedArray 的形状。

掩码数组是标准 numpy.ndarray 和掩码的组合。掩码要么是 nomask,表示关联数组的任何值都不无效,要么是布尔值的数组,用于确定关联数组的每个元素的值是否有效。

步骤

首先,导入所需的库 -

import numpy as np
import numpy.ma as ma

使用 ma.masked_all() 方法返回给定形状和数据类型的空掩码数组,其中所有数据都被掩码 -

arr = ma.masked_all((5, 5))

显示我们的数组 -

print("Array...
",arr)

获取数据类型 -

print("
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 - Return an empty masked array of the given shape where all the data are masked

import numpy as np
import numpy.ma as ma

# To return an empty masked array of the given shape and dtype where all the data are masked, use the ma.masked_all() method in Python Numpy
# The 1st parameter sets the shape of the required MaskedArray
arr = ma.masked_all((5, 5))

# Displaying our array
print("Array...
",arr) # Get the datatype print("
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...
[[-- -- -- -- --]
[-- -- -- -- --]
[-- -- -- -- --]
[-- -- -- -- --]
[-- -- -- -- --]]

Array datatype...
float64

Array Dimensions...
2

Our Array Shape...
(5, 5)

Elements in the Array...
25

更新于: 2022年2月3日

93 次浏览

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.