英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论