scipy.interpolate.interp1d在以十进制值开头x值时是否有问题? [英] Does scipy.interpolate.interp1d have problems with decimal values leading the x values?
问题描述
我试图通过scipy使用 interp1d()来插值一些数据,但是我一直遇到超限或范围错误.经过数小时的谷歌搜索,我现在知道x值未按升序排列会导致出现相同的错误,但我已经确定这不是问题.据我所知,看起来 interp1d()不喜欢第一个值中的小数.我想念什么吗?
I'm trying to use interp1d() from scipy to interpolate some data, but I keep hitting an out or range error. After hours of Googling, I now know that x values not in increasing order will cause the same error I'm getting but I've already made sure that's not the problem. As far as I can tell, it looks like interp1d() doesn't like decimals in the first value. Am I missing something?
我的问题的简化版本:
以下运行正常.
import numpy as np
from scipy.interpolate import interp1d
interp1d(np.array([1, 2, 3, 4, 5, 6]),
np.array([2, 4, 6, 8, 10, 12]), kind='cubic')(np.linspace(1, 6, num=40))
但是,这:
interp1d(np.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6]),
np.array([2, 4, 6, 8, 10, 12]), kind='cubic')(np.linspace(1, 6, num=40))
返回:
ValueError:x_new中的值低于插值范围.
ValueError: A value in x_new is below the interpolation range.
但是,这很好.
interp1d(np.array([1.0, 2.2, 3.3, 4.4, 5.5, 6.6]),
np.array([2, 4, 6, 8, 10, 12]), kind='cubic')(np.linspace(1, 6, num=40))
推荐答案
inter1d
返回一个函数,该函数可让您在数据域内内插 .当您使用
inter1d
returns a function which allows you to interpolate within the domain of the data. When you use
interp1d(np.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6]),
np.array([2, 4, 6, 8, 10, 12]), kind='cubic')(np.linspace(1, 6, num=40))
数据域是间隔[1.1, 6.6]
,它是x-values
的最小值到最大值.
the domain of the data is the interval [1.1, 6.6]
, the minimum to maximum values of the x-values
.
由于1
在np.linspace(1, 6, num=40)
中,并且1位于[1.1, 6.6]
之外,所以interp1d
举起
Since 1
is in np.linspace(1, 6, num=40)
and 1 lies outside [1.1, 6.6]
, interp1d
raises
ValueError: A value in x_new is below the interpolation range.
当您尝试在x-values
np.linspace(1, 6, num=40)
处插值数据时.
when you try to interpolate the data at the x-values
np.linspace(1, 6, num=40)
.
将数据的x-values
更改为
np.array([1.0, 2.2, 3.3, 4.4, 5.5, 6.6])
然后将数据域扩展到[1.0, 6.6]
,现在包括1
.因此,现在可以在np.linspace(1, 6, num=40)
处进行插值了.
then the domain of the data is expanded to [1.0, 6.6]
, which now includes 1
. Hence, interpolating at np.linspace(1, 6, num=40)
now works.
这篇关于scipy.interpolate.interp1d在以十进制值开头x值时是否有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!