有没有一种方法可以插值一个带有预定义节点数量的样条曲线?

huangapple go评论62阅读模式
英文:

python - Is there a way to interpolate a spline with a predefined number of knots?

问题

我基本上正在尝试做的是将这个 Matlab 行翻译成 Python。到目前为止,我找到了 make_inter_spline 这个函数,但它不允许我控制有多少个节点,并且还有 make_lsq_spline 这个函数,需要我提前计算节点。

英文:

What I am trying to do basically is translate this Matlab line:

slm = slmengine(x,y,'degree',1,'knots',numOfKnots,'plot','off');

to python.

So far I've found make_inter_spline which doesn't let me control how many knots there are, and make_lsq_spline which makes me calculate the knots before hand.

答案1

得分: 0

你可以考虑使用 scipy.interpolate.LSQUnivariateSpline(至少在1.8.1版本中可用)。如文档中所述:

> class scipy.interpolate.LSQUnivariateSpline(x, y, t, w=None, bbox=[None, None], k=3, ext=0,
check_finite=False)

> 具有显式内部结点的一维样条。

> 将度为 k 的样条 y = spl(x) 拟合到提供的 x、y 数据。t 指定了样条的内部结点。

英文:

You could consider using scipy.interpolate.LSQUnivariateSpline (in at least 1.8.1). As noted in the documentation:

> class scipy.interpolate.LSQUnivariateSpline(x, y, t, w=None, bbox=[None, None], k=3, ext=0,
check_finite=False)

>1-D spline with explicit internal knots.

>Fits a spline y = spl(x) of degree k to the provided x, y data. t specifies the internal knots of the spline

答案2

得分: 0

我发现了这个名为pwlf的软件包,它解决了这个问题,尽管与SciPy中的任何方法相比速度非常慢。要使用它,您只需执行以下操作:

spline = pwlf.PiecewiseLinFit(x, y)
spline.fitfast(num_of_knots)
y_hat = spline.predict(x)

这正是我在寻找的东西。

英文:

I have found this package called pwlf that solves the problem, even though it is very slow compared to anything from SciPy. To use it you can simply do

spline = pwlf.PiecewiseLinFit(x, y)
spline.fitfast(num_of_knots)
y_hat = spline.predict(x)

which was exactly what I was looking for.

huangapple
  • 本文由 发表于 2023年6月15日 18:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定