在 NumPy 中顯示當前掩碼
要顯示當前掩碼,請在 Python NumPy 中使用 **ma.MaskedArray.mask**。掩碼陣列是標準 numpy.ndarray 和掩碼的組合。掩碼要麼是 nomask,表示關聯陣列的任何值均有效,要麼是布林陣列,用於確定關聯陣列的每個元素的值是否有效。
NumPy 提供了全面的數學函式、隨機數生成器、線性代數例程、傅立葉變換等。它支援各種硬體和計算平臺,並且可以很好地與分散式、GPU 和稀疏陣列庫配合使用。
步驟
首先,匯入所需的庫:
import numpy as np import numpy.ma as ma
使用 numpy.arange() 方法建立一個包含 int 元素的 4x4 陣列:
arr = np.arange(16).reshape((4,4))
print("Array...
", arr)獲取陣列的維度:
print("
Array Dimensions...
",arr.ndim)
獲取陣列的形狀:
print("
Our Masked Array Shape...
",arr.shape)獲取陣列的元素數量:
print("
Elements in the Masked Array...
",arr.size)
建立一個掩碼陣列:
arr = ma.array(arr) arr[0, 1] = ma.masked arr[1, 1] = ma.masked arr[2, 1] = ma.masked arr[2, 2] = ma.masked arr[3, 0] = ma.masked arr[3, 2] = ma.masked arr[3, 3] = ma.masked
沿特定軸計算掩碼元素的數量:
print("
The number of masked elements...
",ma.count_masked(arr, axis = 1))
返回掩碼陣列的掩碼:
print("
The mask of a masked array...
",ma.getmask(arr))將掩碼陣列的資料作為 ndarray 返回:
print("
Data of a masked array as an ndarray...
",ma.getdata(arr))
確定輸入是否為掩碼陣列的例項:
print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr))要顯示當前掩碼,請在 Python NumPy 中使用 ma.MaskedArray.mask:
print("
Result...
",arr.mask)
示例
# Python ma.MaskedArray - Display the current mask
import numpy as np
import numpy.ma as ma
# Creating a 4x4 array with int elements using the numpy.arange() method
arr = np.arange(16).reshape((4,4))
print("Array...
", arr)
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
print("
Our Array type...
", arr.dtype)
# Get the shape of the Array
print("
Our Masked Array Shape...
",arr.shape)
# Get the number of elements of the Array
print("
Elements in the Masked Array...
",arr.size)
# Create a masked array
arr = ma.array(arr)
arr[0, 1] = ma.masked
arr[1, 1] = ma.masked
arr[2, 1] = ma.masked
arr[2, 2] = ma.masked
arr[3, 0] = ma.masked
arr[3, 2] = ma.masked
arr[3, 3] = ma.masked
# Count the number of masked elements along specific axis
print("
The number of masked elements...
",ma.count_masked(arr, axis = 1))
# Return the mask of a masked array
print("
The mask of a masked array...
",ma.getmask(arr))
# Return the data of a masked array as an ndarray
print("
Data of a masked array as an ndarray...
",ma.getdata(arr))
# Determine whether input is an instance of masked array
print("
Whether input is an instance of masked array?
",ma.isMaskedArray(arr))
# To display the current mask, use the ma.MaskedArray.mask in Python Numpy
print("
Result...
",arr.mask)輸出
Array... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Array type... int64 Array Dimensions... 2 Our Array type... int64 Our Masked Array Shape... (4, 4) Elements in the Masked Array... 16 The number of masked elements... [1 1 2 3] The mask of a masked array... [[False True False False] [False True False False] [False True True False] [ True False True True]] Data of a masked array as an ndarray... [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] Whether input is an instance of masked array? True Result... [[False True False False] [False True False False] [False True True False] [ True False True True]]
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP