【发布时间】:2019-06-07 06:50:34
【问题描述】:
我在实现一些不太常见的插值问题时遇到了问题。我有一些(x,y)数据点散布在我不知道的先验曲线上,我想尽我所能重建这条曲线,用最小平方误差对我的点进行插值。我曾想过为此目的使用scipy.interpolate.splrep(但也许您会建议使用更好的选项)。在我的情况下,另一个困难是我想约束样条曲线通过我的原始数据的一些特定点。我认为玩结和权重可能会成功,但我不知道该怎么做(除了基本的拟合程序外,我还在拖延避免样条插值理论)。此外,由于一些未公开的原因,当我尝试在我的splrep 中设置结时,我得到了与this post 相同的错误,这使事情变得复杂。以下是我的示例代码:
from __future__ import division
import numpy as np
import scipy.interpolate as spi
import matplotlib.pylab as plt
# Some surrogate sample data
f = lambda x : x**2 - x/2.
x = np.arange(0.,20.,0.1)
y = f(4*(x + np.random.normal(size=np.size(x))))
# I want to use spline interpolation with least-square fitting criterion, making sure though that the spline starts
# from the origin (or in general passes through a precise point of my dataset).
# In my case for example I would like the spline to originate from the point in x=0. So I attempted to include as first knot x=0...
# but it won't work, nor I am sure this is the right procedure...
fy = spi.splrep(x,y)
fy = spi.splrep(x,y,t=fy[0])
yy = spi.splev(x,fy)
plt.plot(x,y,'-',x,yy,'--')
plt.show()
尽管我什至传递了从第一次调用 splrep 计算的结,但它会给我:
File "/usr/lib64/python2.7/site-packages/scipy/interpolate/fitpack.py", line 289, in splrep
res = _impl.splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet)
File "/usr/lib64/python2.7/site-packages/scipy/interpolate/_fitpack_impl.py", line 515, in splrep
raise _iermess[ier][1](_iermess[ier][0])
ValueError: Error on input data
【问题讨论】:
标签: python scipy constraints interpolation spline