Search code examples
pythonscipyspline

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


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.


Solution

  • 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.