Python interp1d vs. UnivariateSpline

后端 未结 4 1745
攒了一身酷
攒了一身酷 2020-12-28 20:21

I\'m trying to port some MatLab code over to Scipy, and I\'ve tried two different functions from scipy.interpolate, interp1d and UnivariateSpline. The interp1d results matc

4条回答
  •  时光说笑
    2020-12-28 20:29

    The reason why the results are different (but both likely correct) is that the interpolation routines used by UnivariateSpline and interp1d are different.

    • interp1d constructs a smooth B-spline using the x-points you gave to it as knots

    • UnivariateSpline is based on FITPACK, which also constructs a smooth B-spline. However, FITPACK tries to choose new knots for the spline, to fit the data better (probably to minimize chi^2 plus some penalty for curvature, or something similar). You can find out what knot points it used via g.get_knots().

    So the reason why you get different results is that the interpolation algorithm is different. If you want B-splines with knots at data points, use interp1d or splmake. If you want what FITPACK does, use UnivariateSpline. In the limit of dense data, both methods give same results, but when data is sparse, you may get different results.

    (How do I know all this: I read the code :-)

提交回复
热议问题