[英]scipy.interpolate.interp1d with kind=“previous” doesn't extrapolate as expected
import numpy as np
from scipy.interpolate import interp1d
a = np.arange(10)
b = a + 100
iterp = interp1d(a, b, kind="previous", bounds_error=False)
print(iterp([-1,-2,5,8,12,25]))
# [nan nan 105. 108. nan nan]
First two nan
values makes sense, but last two don't since a previous value is available. 前两个nan
值有意义,但后两个则无意义,因为先前的值可用。
Now if I use extrapolate: 现在,如果我使用推断:
iterp = interp1d(a, b, kind="previous", fill_value="extrapolate")
print(iterp([-1,-2,5,8,12,25]))
# [100. 100. 105. 108. 109. 109.]
The last two 109.
makes sense but the first two 100.
don't since there is no previous value. 后两个109.
有意义,但前两个100.
则没有意义,因为没有先前的值。
The output I'm looking for is the intuitive [nan nan 105. 108. 109. 109.]
. 我正在寻找的输出是直观的[nan nan 105. 108. 109. 109.]
。 Of course I can take either options above as a base and fiddle with it. 当然,我可以将以上任一选项作为基础并加以摆弄。 But is there a direct way to obtain the desired result? 但是,有直接的方法来获得所需的结果吗?
Ok, this workaround isn't too bad I guess: 好的,我猜这个解决方法还不错:
iterp = interp1d(a, b, kind="previous", fill_value=(np.nan, b.max()), bounds_error=False)
print(iterp([-1,-2,5,8,12,25]))
# [ nan nan 105. 108. 109. 109.]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.