Numpy MaskedArray.filled() 方法 – Python
numpy.MaskedArray.filled()
函数返回一个 self 的副本,掩码值填充给定值。但是,如果没有要填充的掩码值,则 self 将作为 ndarray 返回。
Syntax : numpy.MaskedArray.filled(self, fill_value = None)
Parameters :
fill_value : [scalar, optional] The value to use for invalid entries, by default is None. If None, the fill_value attribute of the array is used instead.
Return :
filled_array : [ndarray] A copy of self with invalid entries replaced by fill_value or self itself as an ndarray if there are no invalid entries to be replaced.
代码#1:
# Python program explaining
# numpy.MaskedArray.filled() function
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
arr = geek.ma.array([2, 4, 6, 8, 10], mask =[0, 0, 1, 0, 1],
fill_value = -999)
gfg = arr.filled()
print(gfg)
输出 :
[ 2 4 -999 8 -999]
代码#2:
# Python program explaining
# numpy.MaskedArray.filled() 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], mask =[1, 0, 1, 0, 0],
fill_value = -999)
gfg = arr.filled()
print(gfg)
输出 :
[-999 2 -999 4 5]