在 Numpy 中將蒙版陣列元素轉換為浮點數型別
要將蒙版陣列轉換為 float 型別,請在 Numpy 中使用 ma.MaskedArray.__float__() 方法。掩碼可能是 nomask, 表示關聯陣列的任何值都是無效的,或一個布林型陣列,用於確定關聯陣列的每個元素的值是否有效。
步驟
首先,匯入所需的庫 −
import numpy as np import numpy.ma as ma
使用 numpy.array() 方法建立陣列 −
arr = np.array([30]) print("Array...", arr) print("
Array type...", arr.dtype)
獲取陣列的維度 −
print("
Array Dimensions...",arr.ndim)
建立蒙版陣列 −
maskArr = ma.masked_array(arr, mask =[False]) print("
Our Masked Array
", maskArr) print("
Our Masked Array type...
", maskArr.dtype)
獲取蒙版陣列的維度 −
print("
Our Masked Array Dimensions...
",maskArr.ndim)
將蒙版陣列轉換為浮點數型別,請在 Numpy 中使用 ma.MaskedArray.__float__() 方法 −
print("
Result Converted to float type...
",maskArr.__float__())
示例
import numpy as np import numpy.ma as ma # Create an array using the numpy.array() method arr = np.array([30]) print("Array...", arr) print("
Array type...", arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...",arr.ndim) # Create a masked array maskArr = ma.masked_array(arr, mask =[False]) 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 convert masked array to float type, use the ma.MaskedArray.__float__() method in Numpy print("
Result Converted to float type...
",maskArr.__float__())
輸出
Array... [30] Array type... int64 Array Dimensions... 1 Our Masked Array [30] Our Masked Array type... int64 Our Masked Array Dimensions... 1 Result Converted to float type... 30.0
廣告