gpt4 book ai didi

python - scipy 中的 interp1d 如何处理关系

转载 作者:行者123 更新时间:2023-12-01 03:52:18 29 4
gpt4 key购买 nike

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

这是由插值器的设计保证的吗?

最佳答案

interp1dscipy/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

call_linear_np 的作用是:

np.interp(x_new, self.x, self.y)

该函数调用编译后的代码。文档讨论了期望 xp 增加,但实际上并没有检查这一点。

def _call_linear(self, x_new):

好像都是Python,可以学习一下。

您所描述的是我对线性插值的期望。但请记住,此代码使用 float 进行操作,并且 float 不能保证“完全相等”。

关于python - scipy 中的 interp1d 如何处理关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033795/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com