如何将样条曲线拟合转换为分段函数? [英] How to convert a spline fit into a piecewise function?

查看:725
本文介绍了如何将样条曲线拟合转换为分段函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

import numpy as np
from scipy.interpolate import UnivariateSpline

# "true" data; I don't know this function
x = np.linspace(0, 100, 1000)
d = np.sin(x * 0.5) + 2 + np.cos(x * 0.1)

# sample data; that's what I actually measured
x_sample = x[::20]
d_sample = d[::20]

# fit spline
s = UnivariateSpline(x_sample, d_sample, k=3, s=0.005)

plt.plot(x, d)
plt.plot(x_sample, d_sample, 'o')
plt.plot(x, s(x))
plt.show()

我知道

我现在想拥有的是所有橙色点之间的功能,所以类似

What I would now like to have are functions between all the orange dots, so something like

knots = s.get_knots()
f0 = <some expression> for knots[0] <= x < knots[1]
f1 = <some expression> for knots[1] <= x < knots[2]
...

因此,选择fi的方式应使其重现样条曲线拟合的形状.

Thereby, fi should be chosen in a way that it reproduces the shape of the spline fit.

我在此处找到了帖子,但样条对于上面的示例,在那里产生的结果似乎是不正确的,并且它也不正是我所需要的,因为它不返回表达式.

I found the post here, but the spline produced there seems incorrect for the example above and it's also not exactly what I need as it does not return expressions.

如何将样条曲线变成分段函数?是否有(简单)方式来表达每个间隔,例如作为多项式?

How could I turn the spline into a piecewise function? Is there a (straightforward) way to express each interval e.g. as a polynomial?

推荐答案

简而言之,如果您对以标准幂为基础的多项式系数感兴趣,最好使用

The short answer is, if you're interested in coefficients of a polynomial in standard power basis, you'd be better off using CubicSpline (and see this discussion):

cu = scipy.interpolate.CubicSpline(x_sample, d_sample)

plt.plot(x_sample, y_sample, 'ko')
for i in range(len(cu.x)-1):
    xs = np.linspace(cu.x[i], cu.x[i+1], 100)
    plt.plot(xs, np.polyval(cu.c[:,i], xs - cu.x[i]))

要回答您的问题,您可以使用numpy.piecewise在此处创建分段函数,在cu.x中使用断点,在cu.c中使用系数,然后直接自己编写多项式表达式或使用numpy.polyval .例如,

And to answer your question, you could instead create a piecewise function from here using numpy.piecewise, the breakpoints in cu.x and the coefficients in cu.c, and either directly code the polynomial expressions yourself or use numpy.polyval. For example,

cu.c[:,0]  # coeffs for 0th segment
# array([-0.01316353, -0.02680068,  0.51629024,  3.        ])

# equivalent ways to code polynomial for this segment
f0 = lambda x: cu.c[0,0]*(x-x[0])**3 + cu.c[1,0]*(x-x[0])**2 + cu.c[2,0]*(x-x[0]) + cu.c[3,0]
f0 = lambda x: [cu.c[i,0]*(x-x[0])**(3-i) for i in range(4)]

# ... or getting values directly from x's
y0 = np.polyval(cu.c[:,0], xs - cu.x[0])

更长的答案:

这里有一些潜在的困惑点:

There are a few points of potential confusion here:

  • UnivariateSpline符合 B样条曲线,因此系数不是与标准多项式幂基础相同
  • 为了从B样条曲线进行转换,我们可以使用PPoly.from_spline,但是不幸的是,UnivariateSpline返回的截断的列表不能使用此功能.我们可以通过访问样条线对象的内部数据来解决此问题,这是一个禁忌.
  • 此外,系数矩阵c(无论来自UnivariateSpline还是CubicSpline)的阶次相反,并且假设您自己处于居中"状态,例如c[k,i]处的系数属于c[k,i]*(x-x[i])^(3-k).
  • UnivariateSpline fits a B-spline basis, so the coefficients are not the same as a standard polynomial power basis
  • In order to convert from B-spline, we can use PPoly.from_spline, but unfortunately UnivariateSpline returns a truncated list of knots and coefficients that won't play with this function. We can resolve this problem by accessing the internal data of the spline object, which is a little taboo.
  • Also, the coefficient matrix c (whether from UnivariateSpline or CubicSpline) is in reverse degree order and assumes you are "centering" yourself, e.g. the coefficient at c[k,i] belongs to c[k,i]*(x-x[i])^(3-k).

根据您的设置,请注意,如果我们不使用UnivariateSpline包装器而直接使用splrep且不进行平滑处理(s=0),则可以获取tck(结系数系数度)元组并发送将其添加到PPoly.from_spline函数并获取所需的系数:

Given your setup, note that if instead of using the UnivariateSpline wrapper, we directly fit with splrep and no smoothing (s=0), we can grab the tck (knots-coefficients-degree) tuple and send it to the PPoly.from_spline function and get the coefficients we want:

tck = scipy.interpolate.splrep(x_sample, d_sample, s=0)
tck
# (array([0.        , 0.        , 0.        , 0.        , 2.68456376,
#        4.02684564, 5.36912752, 6.7114094 , 9.39597315, 9.39597315,
#        9.39597315, 9.39597315]),
# array([3.        , 3.46200469, 4.05843704, 3.89649312, 3.33792889,
#        2.29435138, 1.65015175, 1.59021688, 0.        , 0.        ,
#        0.        , 0.        ]),
# 3)

p = scipy.interpolate.PPoly.from_spline(tck)
p.x.shape  # breakpoints in unexpected shape
# (12,)

p.c.shape  # polynomial coeffs in unexpected shape
# (4, 11)

请注意在tck中和在p.x中又一次重复的怪异断点:这是FITPACK(运行所有这些的算法).

Notice the weird repeated breakpoints in tck and again in p.x: this is a FITPACK thing (the algorithm running all this).

如果我们尝试从UnivariateSpline发送带有(s.get_knots(), s.get_coeffs(), 3)tck元组,则会丢失这些重复,因此from_spline不起作用.查看来源尽管看起来完整的向量都存储在self._data中,所以我们可以做到

If we try to send a tck tuple from UnivariateSpline with (s.get_knots(), s.get_coeffs(), 3), we are missing those repeats, so from_spline doesn't work. Checking out the source though it appears the full vector is stored in self._data, so we can do

s = scipy.interpolate.UnivariateSpline(x_sample, d_sample, s=0)
tck = (s._data[8], s._data[9], 3)
p = scipy.interpolate.PPoly.from_spline(tck)

,并获得与以前相同的结果.要检查这些系数是否起作用:

and get the same as before. To check these coefficients work:

plt.plot(x_sample, d_sample, 'o')

for i in range(len(p.x)-1):
    xs = np.linspace(p.x[i], p.x[i+1], 10)
    plt.plot(xs, np.polyval(p.c[:,i], xs - p.x[i]))

请注意 numpy.polyval 希望coeffs的顺序相反,因此我们可以照原样通过p.c.

这篇关于如何将样条曲线拟合转换为分段函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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