获取NumPy中掩码数组的当前形状
要获取掩码数组的形状,请使用NumPy中的ma.MaskedArray.shape属性。shape属性通常用于获取数组的当前形状,但也可以通过为其赋值一个数组维度的元组来就地重塑数组。
与numpy.reshape一样,新的形状维度之一可以是-1,在这种情况下,它的值将根据数组的大小和其余维度推断得出。如果需要复制,则就地重塑数组将失败。
步骤
首先,导入所需的库:
import numpy as np import numpy.ma as ma
使用numpy.array()方法创建一个数组:
arr = np.array([[35, 85], [67, 33]]) print("Array...
", arr) print("
Array type...
", arr.dtype)
获取数组的维度:
print("Array Dimensions...
",arr.ndim)
获取消耗的总字节数:
print("Array nbytes...
",arr.nbytes)
创建一个掩码数组,并将其中一些标记为无效:
maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
获取掩码数组的itemsize:
print("
Our Masked Array itemsize...
", maskArr.itemsize)
获取掩码数组的维度:
print("
Our Masked Array Dimensions...
",maskArr.ndim)
获取掩码数组的形状,使用NumPy中的ma.MaskedArray.shape属性:
print("
Our Masked Array Shape...
",maskArr.shape)
示例
import numpy as np import numpy.ma as ma arr = np.array([[35, 85], [67, 33]]) print("Array...
", arr) print("
Array type...
", arr.dtype) print("
Array itemsize...
", arr.itemsize) # Get the dimensions of the Array print("Array Dimensions...
",arr.ndim) # Get the total bytes consumed print("Array nbytes...
",arr.nbytes) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[0, 0], [ 0, 1]]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype) # Get the itemsize of the Masked Array print("
Our Masked Array itemsize...
", maskArr.itemsize) # Get the dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # To get the shape of the Masked Array, use the ma.MaskedArray.shape attribute in Numpy print("
Our Masked Array Shape...
",maskArr.shape)
输出
Array... [[35 85] [67 33]] Array type... int64 Array itemsize... 8 Array Dimensions... 2 Array nbytes... 32 Our Masked Array [[35 85] [67 --]] Our Masked Array type... int64 Our Masked Array itemsize... 8 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 2)
广告