在 scipy.insterpolate 的 splrep 函数上选择结的错误 (?) [英] Bug (?) on selecting knots on scipy.insterpolate's splrep function

查看:91
本文介绍了在 scipy.insterpolate 的 splrep 函数上选择结的错误 (?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 scipy 的 splrep 函数的问题,我认为这是一个错误,所以我会发布每段代码,以便您可以在您的计算机上重现它.假设我想找到一些数据的 b 样条表示,比如说,通过以下代码获得的数据,它创建了一个数据集,作为十个高斯和一些附加噪声的混合:

I have this question regarding scipy's splrep function, which I think is a bug, so I'll post every piece of code so you can reproduce it on your computers. Suppose I want to find the b-spline representation of some data, say, the one obtained by the following code, which creates a dataset as a mixture of ten gaussians with some addeded noise:

import numpy as np
# First we define the number of datapoints:
ndata = 100
x = np.arange(0,1,1./np.double(ndata))
means = np.random.uniform(0,1,10)
y = 0.0
for i in range(len(means)):
    y = y+np.exp(-(x-means[i])**2./0.01)
# We add some noise to obtain the data:
data = y + np.random.normal(0,0.05,len(y))

应该是这样的:现在,让我们使用 splrep 和 splev 函数来获得这条曲线的 b 样条表示:

Which should see like this: Now, let's use splrep and splev functions to obtain the b-spline representation of this curve:

from scipy.interpolate import splrep,splev
# First define the number of knots. Let's put, say, 10 knots:
nknots = 10
# Now we crate the array of knots:
knots = np.arange(x[1],x[len(x)-1],(x[len(x)-1]-x[1])/np.double(nknots))
tck = splrep(x,data,t=knots)
fit = splev(x,tck)

如果你把所有的东西都画到这里,一切似乎都没问题:但是,数据点数量和节点数量的某些组合存在问题.例如,如果您使用 ndata = 1931nknots = 796 尝试上述代码,我会收到以下错误:

If you plot everything up to here, everything seems ok: However, there are problems with certain combinations of the number of datapoints and number of knots. For example, if you try the above code with ndata = 1931 and nknots = 796, I get the following error:

File "/usr/lib/python2.7/dist-packages/scipy/interpolate/fitpack.py", line 465, in
splrep raise _iermess[ier][1](_iermess[ier][0])
ValueError:     Error on input data

这给我带来了问题,因为上面的代码不可自动化.我正在使用具有约 19000 个数据点的数据集,其中带有 tryexceptwhile 循环对计算的要求非常高.所以我的问题是:

This is giving me problems because the above code is not automatizable. I'm playing with datasets which have ~19000 datapoints, where a while cycle with try and except can be very computationally demanding. So my questions are:

  1. 你能重现这个问题吗?如果可以的话...
  2. 你知道发生了什么吗?

推荐答案

我创造了解决问题的方法.这可能与两个结之间的数据点数量少有关,所以我所做的是用以下内容替换我创建结数的线:

I created a way around the problem. It probably has to do with a low number of datapoints to fit between two knots, so what I did was to replace the line where I create the number of knots with:

idx_knots = (np.arange(1,len(x)-1,(len(x)-2)/np.double(nknots))).astype('int')
knots = x[idx_knots]

通过这种方式,我确保节点之间有足够的数据点,因为我使用了 x 向量的索引.

In that way, I ensure that there are enough datapoints between the knots because I play with the indices of the x vector.

这篇关于在 scipy.insterpolate 的 splrep 函数上选择结的错误 (?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆