Python numpy.ma.MaskedArray.toflex()函数
numpy.ma.MaskedArray.toflex()函数将一个掩码数组转换为一个灵活类型的数组。返回的灵活类型数组将有两个字段:_data字段和_mask字段。_data字段存储数组的_data部分,_mask字段存储数组的_mask部分。
语法: numpy.ma.MaskedArray.toflex(self)
返回: [ndarray] 一个新的灵活型ndarray,有两个字段:第一个元素包含一个值,第二个元素包含相应的掩码布尔值。返回的记录形状与self.shape匹配。
代码#1:
# Python program explaining
# numpy.ma.MaskedArray.toflex() function
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
arr = geek.ma.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
mask =[0] + [1, 0]*4)
gfg = arr.toflex()
print (gfg)
输出 :
[[(1, False) (2, True) (3, False)]
[(4, True) (5, False) (6, True)]
[(7, False) (8, True) (9, False)]]
代码#2:
# Python program explaining
# numpy.ma.MaskedArray.toflex() function
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
arr = geek.ma.array([[11, 12, 13], [14, 15, 16], [17, 18, 19]],
mask =[0] + [1, 1]*4)
gfg = arr.toflex()
print (gfg)
输出 :
[[(11, False) (12, True) (13, True)]
[(14, True) (15, True) (16, True)]
[(17, True) (18, True) (19, True)]]