解释scipy.interpolate样条拟合的结果 [英] interpret result of scipy.interpolate spline fit
问题描述
我有一些数据点,我想用三次b样条拟合进行近似。在另一个程序中,我喜欢仅使用拟合的结和系数对一些点进行插值。这意味着我需要自己编写代码以评估给定结点和系数的点。使用此Wiki页面,我已经能够使用
I have some data points which I like to approximate with a cubic b-spline fit. In an other program, I like to interpolate some points using only the fitted knots and coefficients. This means that I need to write the code to evaluate a point given the knots and coefficients myself. Using this wiki page, I was already able to evaluate various points correctly with the result of
knots,coeff,n=scipy.interpolate.splrep(x,y)
奇怪的是,我需要删除数组中的前3个条目以使代码正常工作(注意:前4个结和最后4个结)相同,因此将产生除以0的错误)。有人知道为什么会有这些重复的条目吗?除了删除这些条目之外,Wiki页面上的等式也有效。根据结果
Strangely, I needed to remove the first and last 3 entries in the arrays to get the code to work (note: the first 4 and last 4 knots entries are the same and will therefore produce a division by 0 error). Does somebody know why there are these duplicated entries? Apart from removing those entries, the equation from the wiki page works. From the result
knots=[ 15., 15., 15., 15., 75., 105., ..., 2895., 2925., 2985., 2985., 2985., 2985.]
coeffs=[ 1.87979615, 1.54132042, 1.46751212, 1.31223359, 1.34208367, 1.2181689, ..., 0.99327417,0.5866527, 0., 0., 0., 0. ]
我得到了数据点的近似值。
I get a good approximation of the data points.
但是,当我也想使用权重时,我注意到结和 coeff数组看起来很奇怪。谁知道我需要如何解释样条拟合函数的结果?目前我得到
However, when I want to use weights as well, I noticed that the "knots" and "coeff" arrays look every strange. Who knows how I need to interpret the result of the spline fit function? Currently I get
knots=[15.,15.,15.,15.,2985.,2985.,2985.,2985.]
coeff=[ 1.48725714,0.3513811,0.71970619,0.66119226,0.,0.,0.,0. ]
还是这是一个错误?
理想情况下,我喜欢编写一些代码(C / C ++,python),该函数根据函数的结和系数来评估各个点。
Ideally, I like to have some code (C/C++,python) which evaluates various points given the knots and coefficients from the function.
推荐答案
我认为您在开头和结尾处有重复的结是因为它形成了 clamped 样条曲线。使用其他数字语言(例如 Scilab)创建样条线时,这是一个选项)。根据 Math.stackexchange ,重复打结是获得带固定样条线的标准方法,该样条线通过端点的样条线的初始斜率和结束斜率使数据近似于第二个点和最后一个点。
I think the reason that you have duplicate knots at the beginning and end is because it is forming a clamped spline. This is an option when creating the spline in other numerical languages (such as Scilab). According to Math.stackexchange repeating the knots is the standard way to get a clamped spline that goes through the endpoints with a spline whose initial and end slope approximate the data to the second and next to last points.
上面的评论说明如何评估B样条。但是,您也可以使用 interpolate.PPoly (通过 interpolate.PPoly.from_spline )可以将B样条转换为易于评估的分段多项式样条,具体说明请参见此答案。
The comment above explains how to evaluate the B-spline. However, you can also use interpolate.PPoly (via interpolate.PPoly.from_spline) to convert from a B-spline to a piece-wise polynomial spline that is easy to evaluate, as is well explained in this answer.
这篇关于解释scipy.interpolate样条拟合的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!