Python `ValueError:x_new 中的值高于插值范围。` - 除了不升序值之外还有什么其他原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45429831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:01:27  来源:igfitidea点击:

`ValueError: A value in x_new is above the interpolation range.` - what other reasons than not ascending values?

pythonscipyinterpolation

提问by durbachit

I receive this error in scipy interp1d function. Normally, this error would be generated if the x was not monotonically increasing.

我在 scipy interp1d 函数中收到此错误。通常,如果 x 不是单调增加,就会产生这个错误。

import scipy.interpolate as spi
def refine(coarsex,coarsey,step):
    finex = np.arange(min(coarsex),max(coarsex)+step,step)
    intfunc = spi.interp1d(coarsex, coarsey,axis=0)
    finey = intfunc(finex)
    return finex, finey

for num, tfile in enumerate(files):
    tfile = tfile.dropna(how='any')
    x = np.array(tfile['col1'])
    y = np.array(tfile['col2'])
    finex, finey = refine(x,y,0.01)

The code is correct, because it successfully worked on 6 data files and threw the error for the 7th. So there must be something wrong with the data. But as far as I can tell, the data increase all the way down. I am sorry for not providing an example, because I am not able to reproduce the error on an example.

代码是正确的,因为它成功地处理了 6 个数据文件,并在第 7 个时抛出了错误。所以数据肯定有问题。但据我所知,数据一直在增加。我很抱歉没有提供示例,因为我无法在示例中重现错误。

There are two things that could help me:

有两件事可以帮助我:

  1. Some brainstorming - if the data are indeed monotonically increasing, what else could produce this error? Another hint, regarding the decimals, could be in this question, but I think my solution (the min and max of x) is robust enough to avoid it. Or isn't it?
  2. Is it possible (how?) to return the value of x_new and it's index when throwing the ValueError: A value in x_new is above the interpolation range.so that I could actually see where in the file is the problem?
  1. 一些头脑风暴 - 如果数据确实是单调递增的,还有什么会产生这个错误?关于小数的另一个提示可能在这个问题中,但我认为我的解决方案(x 的最小值和最大值)足够强大,可以避免它。或者不是吗?
  2. 是否有可能(如何?)在抛出时返回 x_new 的值和它的索引,ValueError: A value in x_new is above the interpolation range.以便我可以实际看到文件中的问题所在?

UPDATE

更新

So the problem is that, for some reason, max(finex)is larger than max(coarsex)(one is .x39 and the other is .x4). I hoped rounding the original values to 2 significant digits would solve the problem, but it didn't, it displays fewer digits but still counts with the undisplayed. What can I do about it?

所以问题是,由于某种原因,max(finex)大于max(coarsex)(一个是 .x39,另一个是 .x4)。我希望将原始值四舍五入为 2 位有效数字可以解决问题,但事实并非如此,它显示的数字较少,但仍与未显示的数字有关。我该怎么办?

回答by saintsfan342000

If you are running Scipy v. 0.17.0 or newer, then you can pass fill_value='extrapolate'to spi.interp1d, and it will extrapolate to accomadate these values of your's that lie outside the interpolation range. So define your interpolation function like so:

如果您正在运行 Scipy v. 0.17.0 或更高版本,那么您可以传递fill_value='extrapolate'spi.interp1d,它将外推以适应您的这些位于插值范围之外的值。所以像这样定义你的插值函数:

intfunc = spi.interp1d(coarsex, coarsey,axis=0, fill_value="extrapolate")

intfunc = spi.interp1d(coarsex, coarsey,axis=0, fill_value="extrapolate")

Be forewarned, however!

但是,请预先警告!

Depending on what your data looks like and the type on interpolation you are performing, the extrapolated values can be erroneous. This is especially true if you have noisy or non-monotonic data. In your case you might be ok because your x_new value is only slighlybeyond your interpolation range.

根据您的数据外观和您正在执行的插值类型,外推值可能是错误的。如果您有嘈杂或非单调的数据,则尤其如此。在您的情况下,您可能没问题,因为您的 x_new 值仅略微超出您的插值范围。

Here's simple demonstration of how this feature can work nicely but also give erroneous results.

下面是这个功能如何很好地工作但也给出错误结果的简单演示。

import scipy.interpolate as spi
import numpy as np

x = np.linspace(0,1,100)
y = x + np.random.randint(-1,1,100)/100
x_new = np.linspace(0,1.1,100)
intfunc = spi.interp1d(x,y,fill_value="extrapolate")
y_interp = intfunc(x_new)

import matplotlib.pyplot as plt
plt.plot(x_new,y_interp,'r', label='interp/extrap')
plt.plot(x,y, 'b--', label='data')
plt.legend()
plt.show()

enter image description here

在此处输入图片说明

So the interpolated portion (in red) worked well, but the extrapolated portion clearly fails to follow the otherwise linear trend in this data because of the noise. So have some understanding of your data and proceed with caution.

因此,内插部分(红色)运行良好,但由于噪声,外推部分显然未能遵循该数据中的线性趋势。因此,对您的数据有所了解并谨慎行事。

回答by hpaulj

A quick test of your finexcalc shows that it can (always?) gets into the extrapolation region.

对您的finex计算的快速测试表明它可以(总是?)进入外推区域。

In [124]: coarsex=np.random.rand(100)
In [125]: max(coarsex)
Out[125]: 0.97393109991816473
In [126]: step=.01;finex=np.arange(min(coarsex), max(coarsex)+step, step);(max(
     ...: finex),max(coarsex))
Out[126]: (0.98273730602114795, 0.97393109991816473)
In [127]: step=.001;finex=np.arange(min(coarsex), max(coarsex)+step, step);(max
     ...: (finex),max(coarsex))
Out[127]: (0.97473730602114794, 0.97393109991816473)

Again it is a quick test, and may be missing some critical step or value.

同样,这是一个快速测试,可能会遗漏一些关键步骤或值。