將掩碼陣列資料作為包含陣列中原始位元組的字串返回,並在Numpy中填充無效的條目
要將陣列資料作為包含陣列中原始位元組的字串返回,請在Numpy中使用**ma.MaskedArray.tobytes()**方法。
fill_value引數是用於填充掩碼值的數值。預設為None,在這種情況下,將使用MaskedArray.fill_value。
order引數是副本中資料項的順序。預設為'C'。
‘C’ - C順序(行主序)。
‘F’ - Fortran順序(列主序)。
‘A’ - 任意,陣列的當前順序。
None - 與'A'相同。
步驟
首先,匯入所需的庫 -
import numpy as np import numpy.ma as ma
使用numpy.array()方法建立包含整數元素的陣列 -
arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)獲取陣列的維度 -
print("Array Dimensions...
",arr.ndim)
建立一個掩碼陣列,並將其中一些標記為無效 -
maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
print("
Our Masked Array
", maskArr)
print("
Our Masked Array type...
", maskArr.dtype)獲取掩碼陣列的維度 -
print("
Our Masked Array Dimensions...
",maskArr.ndim)
獲取掩碼陣列的形狀 -
print("
Our Masked Array Shape...
",maskArr.shape)獲取掩碼陣列的元素數量 -
print("
Elements in the Masked Array...
",maskArr.size)
將陣列資料作為包含陣列中原始位元組的字串返回,使用ma.MaskedArray.tobytes()。"fill_value"引數是用於填充掩碼值的數值。預設為None,在這種情況下,將使用MaskedArray.fill_value -
print("
Result...
",maskArr.tobytes(fill_value = 1111))示例
# Python ma.MaskedArray - Return the array data as a string containing the raw bytes in the array and
# fill the invalid entries
import numpy as np
import numpy.ma as ma
# Create an array with int elements using the numpy.array() method
arr = np.array([[49, 85, 45], [67, 33, 59]])
print("Array...
", arr)
print("
Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr.ndim)
# Create a masked array and mask some of them as invalid
maskArr = ma.masked_array(arr, mask =[[0, 0, 1], [ 0, 1, 0]])
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 shape of the Masked Array
print("
Our Masked Array Shape...
",maskArr.shape)
# Get the number of elements of the Masked Array
print("
Elements in the Masked Array...
",maskArr.size)
# To return the array data as a string containing the raw bytes in the array, use the ma.MaskedArray.tobytes() method in Numpy
# The "fill_value" parameter ios the value used to fill in the masked values.
# Default is None, in which case MaskedArray.fill_value is used.
print("
Result...
",maskArr.tobytes(fill_value = 1111))輸出
Array... [[49 85 45] [67 33 59]] Array type... int64 Array Dimensions... 2 Our Masked Array [[49 85 --] [67 -- 59]] Our Masked Array type... int64 Our Masked Array Dimensions... 2 Our Masked Array Shape... (2, 3) Elements in the Masked Array... 6 Result... b'1\x00\x00\x00\x00\x00\x00\x00U\x00\x00\x00\x00\x00\x00\x00W\x04\x00\x00\x00\x00\x00\x00C\x00\x00\x00\x00\x00\x00\x00W\x04\x00\x00\x00\x00\x00\x00;\x00\x00\x00\x00\x00\x00\x00'
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP