获取 NumPy 中元素所占用的总字节数
要获取掩码数组所占用的总字节数,请在 NumPy 中使用 **ma.MaskedArray.nbytes** 属性。不包括数组对象非元素属性所占用的内存。
掩码可以是 nomask,表示关联数组的任何值均有效,也可以是布尔值的数组,用于确定关联数组的每个元素的值是否有效。
步骤
首先,导入所需的库 -
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 itemsize...
", arr.itemsize)
获取数组的维度 -
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)
获取掩码数组的维度 -
print("
Our Masked Array Dimensions...
",maskArr.ndim)
获取掩码数组的 itemsize -
print("
Our Masked Array itemsize...
", maskArr.itemsize)
获取掩码数组所占用的总字节数,在 NumPy 中使用 ma.MaskedArray.nbytes 属性 -
print("
Our Masked Array nbytes...
",maskArr.nbytes)
示例
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 dimensions of the Masked Array print("
Our Masked Array Dimensions...
",maskArr.ndim) # Get the itemsize of the Masked Array print("
Our Masked Array itemsize...
", maskArr.itemsize) # To get the total bytes consumed by the masked array, use the ma.MaskedArray.nbytes attribute in Numpy print("
Our Masked Array nbytes...
",maskArr.nbytes)
输出
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 Dimensions... 2 Our Masked Array itemsize... 8 Our Masked Array nbytes... 32
广告