Python scipy中的interp1d如何处理领带

Python scipy中的interp1d如何处理领带,python,scipy,Python,Scipy,scipy.interpolate.interp1d的文档没有说明当x参数中存在关联时会发生什么。我的实验表明,当请求精确的x值时,它返回最右边的值,并在插值时使用最接近的值: from scipy.interpolate import interp1d temp = interp1d([0, 1, 1, 2], [1, 2, 3, 4]) temp(0.5) # 1.5 temp(1) # 3.0 temp(1.5) # 3.5 插值器的设计是否保证了这一点?interp1d在scip

scipy.interpolate.interp1d
的文档没有说明当
x
参数中存在关联时会发生什么。我的实验表明,当请求精确的x值时,它返回最右边的值,并在插值时使用最接近的值:

from scipy.interpolate import interp1d
temp = interp1d([0, 1, 1, 2], [1, 2, 3, 4])
temp(0.5)  # 1.5
temp(1)  # 3.0
temp(1.5)  # 3.5

插值器的设计是否保证了这一点?

interp1d
scipy/interpolate/interpolate.py
中定义。对于默认的“线性”类型,它似乎有两种选择

            # Check if we can delegate to numpy.interp (2x-10x faster).
            if (not np.issubdtype(self.y.dtype, np.complexfloating) and
               self.y.ndim == 1 and
               not _do_extrapolate(fill_value)):
                self._call = self.__class__._call_linear_np
            else:
                self._call = self.__class__._call_linear
调用\u linear\u np
执行以下操作:

np.interp(x_new, self.x, self.y)
该函数调用编译后的代码。文档中提到希望
xp
增加,但实际上并没有检查这一点

def _call_linear(self, x_new):
看起来都是Python,可以学习


你所描述的是我对线性插值的期望。但是请记住,这段代码使用浮点运算,而浮点运算不能保证“完全相等”。

不,不能保证


x
值中存在关联时,插值器的行为未定义,并且可能在scipy版本之间更改。(还有插值类型和数组中的关系)。

您是否将问题限制为默认的“线性”类型?目前,我只对线性算法感兴趣,但了解备选方案的行为可能对未来有用。