在 NumPy 中返回一個給定形狀和資料型別的空掩碼陣列,其中所有資料都被掩碼。
要返回一個給定形狀和資料型別的空掩碼陣列,其中所有資料都被掩碼,可以使用 Python NumPy 中的 **ma.masked_all()** 方法。第一個引數設定所需 MaskedArray 的形狀。**dtype** 引數設定陣列所需的輸出資料型別。
掩碼陣列是標準 numpy.ndarray 和掩碼的組合。掩碼要麼是 nomask,表示關聯陣列的任何值均無效,要麼是布林陣列,用於確定關聯陣列的每個元素的值是否有效。
步驟
首先,匯入所需的庫:
import numpy as np import numpy.ma as ma
使用 ma.masked_all() 返回一個給定形狀和資料型別的空掩碼陣列,其中所有資料都被掩碼。dtype 引數設定陣列所需的輸出資料型別:
arr = ma.masked_all((5, 5),dtype = np.int32)
顯示我們的陣列:
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 and dtype 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
# The dtype parameter sets the desired output data-type for the array
arr = ma.masked_all((5, 5),dtype = np.int32)
# 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... int32 Array Dimensions... 2 Our Array Shape... (5, 5) Elements in the Array... 25
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP