獲取NumPy中掩碼陣列的記憶體佈局資訊


要獲取掩碼陣列的記憶體佈局資訊,請使用NumPy中的**ma.MaskedArray.flags**。掩碼陣列是可以包含缺失或無效條目的陣列。 numpy.ma 模組提供了幾乎與NumPy相同的替代方案,支援帶有掩碼的資料陣列。

掩碼陣列是標準numpy.ndarray和掩碼的組合。掩碼要麼是nomask,表示相關陣列中沒有無效值;要麼是一個布林型陣列,用於確定相關陣列的每個元素是否有效。

步驟

首先,匯入所需的庫:

import numpy as np
import numpy.ma as ma

使用numpy.array()方法建立一個NumPy陣列:

arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr) print("
Array type...
", arr.dtype)

獲取陣列的維度:

print("Array Dimensions...",arr.ndim)

獲取陣列的記憶體佈局資訊:

print("
Array flags...
",arr.flags)

建立一個掩碼陣列,並將其中一些標記為無效:

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)

要獲取掩碼陣列的記憶體佈局資訊,請使用NumPy中的ma.MaskedArray.flags:

print("
Our Masked Array flags...
",maskArr.flags)

示例

import numpy as np
import numpy.ma as ma

# Create a numpy array using the numpy.array() method
arr = np.array([[35, 85], [67, 33]])
print("Array...
", arr) print("
Array type...
", arr.dtype) # Get the dimensions of the Array print("Array Dimensions...",arr.ndim) # Get the information about the memory layout of the array print("
Array flags...
",arr.flags) # 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) # To get the information about the memory layout of the masked array, use the ma.MaskedArray.flags in Numpy print("
Our Masked Array flags...
",maskArr.flags)

輸出

Array...
[[35 85]
[67 33]]

Array type...
int64
Array Dimensions... 2

Array flags...
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False


Our Masked Array
[[35 85]
[67 --]]

Our Masked Array type...
int64
Our Masked Array Dimensions... 2

Our Masked Array flags...
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False

更新於:2022年2月21日

瀏覽量:137

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告