Scipys interp1d和无穷大

2024-09-29 01:18:49 发布

您现在位置:Python中文网/ 问答频道 /正文

使用scipy.interpolate.interp1d进行线性插值时,我遇到以下行为:

from scipy.interpolate import interp1d
import numpy as np
x = [0,1,2,3,4]
y = [np.inf, 10, 12, 11, 9]
interpolated_function = interp1d(x,y)

当然,插值在[0,1)中没有很好地定义,但是在1中,我希望它被定义。但是:

^{pr2}$

这是预期的行为吗?在1处的插值函数不应该求值为10,因为这是传递给interp1d的完全有效的数据点吗?在


Tags: 函数fromimportnumpy定义asnpfunction
1条回答
网友
1楼 · 发布于 2024-09-29 01:18:49

interp1d不是特殊情况nans或infs。对于kind="linear",它只是在包含输入值的时间间隔内使用错误消息中打印的公式。在

由于精确相等在浮点中不可靠,通过在1.0处求值,您依赖于将其分类为(0,1)或(1,2)的实现细节。在

例如

In [26]: np.interp(1, x, y)
Out[26]: 10.0

In [32]: interp1d(x, y, kind='slinear')(1.)
Out[32]: array(10.0)

在scipy的未来版本中,行为将取决于fill_value。(在当前的开发版本中,您可以使用fill_value="extrapolate"。)

对于非有限的用户来说,过滤掉有限的数字是最好的。在

^{pr2}$

相关问题 更多 >