fir*_*fly 5 python arrays numpy masking
使用它们旁边最接近的未屏蔽值的平均值替换数组中屏蔽值的最有效的numpy方法是什么?
例如:
a = np.array([2,6,4,8])
b = np.ma.masked_where(a>5,a)
print b
masked_array(data = [2 -- 4 --],
mask = [False True False True],
fill_value = 999999)
Run Code Online (Sandbox Code Playgroud)
我希望b中的掩码值替换为它们旁边的值的平均值.边界可以重复最接近的未屏蔽值.所以在这个例子中,b将如下:
b = [2,3,4,4]
Run Code Online (Sandbox Code Playgroud)
这个问题的主要原因是看看这是否可以在不使用迭代器的情况下有效地完成.
tmd*_*son -1
import numpy as np
a = np.array([2,6,4,8])
mymask = a>5
b = np.ma.masked_where(mymask,a)
print b
# [2 -- 4 --]
c = np.interp(np.where(mymask)[0],np.where(~mymask)[0],b[np.where(~mymask)[0]])
b[np.where(mymask)[0]] = c
print b
# [2 3 4 4]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
824 次 |
| 最近记录: |