使用NumPy從陣列建立布林掩碼
要從陣列建立布林掩碼,請在Python NumPy中使用**ma.make_mask()**方法。該函式可以接受任何可轉換為整數的序列或nomask。不需要內容必須是0和1,值為0被解釋為False,其他所有值被解釋為True。
dtype是輸出掩碼的資料型別。預設情況下,輸出掩碼的dtype為MaskType(bool)。如果dtype是靈活的,則每個欄位都有一個布林dtype。當m為nomask時,此引數將被忽略,在這種情況下,始終返回nomask。
步驟
首先,匯入所需的庫:
import numpy as np import numpy.ma as ma
使用Python NumPy中的numpy.zeros()方法建立一個包含零的陣列:
arr = np.zeros(7)
print("Array...
", arr)要從陣列建立布林掩碼,請在Python NumPy中使用ma.make_mask()方法:
print("
Masked Array...
", ma.make_mask(arr))
陣列型別:
print("
Array type...
", arr.dtype)獲取陣列的維度:
print("
Array Dimensions...
",arr.ndim)
獲取陣列的形狀:
print("
Our Array Shape...
",arr.shape)獲取陣列的元素個數:
print("
Elements in the Array...
",arr.size)
示例
import numpy as np
import numpy.ma as ma
# Create an array with zeros using the numpy.zeros() method in Python Numpy
arr = np.zeros(7)
print("Array...
", arr)
# To Create a boolean mask from an array, use the ma.mask_mask() method in Python Numpy
print("
Masked Array...
", ma.make_mask(arr))
# Type of Array
print("
Array type...
", 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... [0. 0. 0. 0. 0. 0. 0.] Masked Array... False Array type... float64 Array Dimensions... 1 Our Array Shape... (7,) Elements in the Array... 7
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP