在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP