【问题标题】:Behavior of scipy's splrepscipy splrep 的行为
【发布时间】:2012-11-09 12:10:25
【问题描述】:

我有一组数据点,想用样条函数逼近它们。 我使用了两种不同的功能:

  1. splrep 来自 scipy
  2. 还有一个我发现的三次样条函数here

结果类似于this

代码如下:

from matplotlib.pyplot import *
from numpy import *
from scipy import interpolate
#----------------------------------------------
s = arange(257)/256.0
z = s[::-1]
b = transpose(array((z*z*z,
                 3*z*z*s, 
                 3*z*s*s,
                 s*s*s)))
def cubicspline(c,t): 
return dot(b[t],c)
#----------------------------------------------

A = array([
   [ -126.041   ,  246.867004],
   [ -113.745003,   92.083   ],
   [  208.518997, -183.796997],
   [  278.859009, -190.552994]])

a1 = A[:,0]
a2 = A[:,1] 
cs = reshape(A, (-1, 4, 2))
X = []
Y = []
#spline with cubicspline()
for (x,y) in [cubicspline(c,16*t) for c in cs for t in arange(17)]:
X.append(x)
Y.append(y)

# spline with splrep
tck = interpolate.splrep( a1, a2)

xnew = np.arange( min(a1), max(a1), 5)
ynew = interpolate.splev(xnew, tck)
plot(a1, a2, "--ob", ms = 9,  label = "points")
plot(X, Y, "r", lw=2, label = "cubicspline")
plot(xnew, ynew, "g", lw=2, label = "splrep")
legend(); savefig("image.png"); show()

如您所见,splrep 的结果远非令人满意。 有人可以解释一下这种行为以及如何从 splrep 中得到合理的近似值吗?

【问题讨论】:

    标签: python scipy splines


    【解决方案1】:

    您需要定义“满意”的含义。显然,您的三次样条曲线没有通过点进行插值,而 splrep 结果确实如此(并且在这个意义上非常令人满意)。另请注意,您的“cubicspline”实际上只是一个多项式而不是样条曲线(带有断点的多项式)。

    您需要明确告诉splrep,样条曲线不需要通过点 --- 传入一个非零的s 平滑参数。如何正确选择这个,看这个问题: scipy.interpolate.UnivariateSpline not smoothing regardless of parameters

    【讨论】:

    • 你是对的。在样条曲线不会“显着”振荡的意义上令人满意。我不确定平滑参数的含义是什么(我想我必须做数学)。但是,这次效果更好。 postimage.org/image/cb8crl2l7。感谢您的建议。
    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2015-06-01
    • 1970-01-01
    • 2020-10-02
    • 2015-06-23
    相关资源
    最近更新 更多