1

From a set of data points, I wish to generate a single cubic spline function, that best fits the points.

That is, seeking to obtain a single 3rd order polynomial function, that best-fits the data - as opposed to a piecewise polynomial. (the end-goal is to then find the maxima of the second derivative of this function)

I have tried scipy.interpolate.CubicSpline, but it seems to only allow generation of a piecewise cubic spline.

Is there another function, or another library, that will generate a single cubic spline best-fitting given data points?

Background: The data points are from a load cell (weight sensor) that represents two physical bodies coming into contact with each other. Intention is to fit a cubic spine for data smoothing, then calculate the maxima of the second derivative - in order to find the exact moment they come into contact - as best possible with the given sensor. (If there are any flaws observed in this methodology, observations welcomed)

Raw Data: Raw Data Points Piecewise cubic spline generated from scipi Not Smooth Cubic Spline Desired result should yield a curve something like this: Smooth single cubic spline

Mtl Dev
  • 1,604
  • 20
  • 29
  • Are you looking for polynomial regression, or for a univariate spline? https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.UnivariateSpline.html –  Aug 17 '19 at 05:37
  • Univariate spline gave me what I sought - thankyou! Though, to answer your question, after [research](https://math.stackexchange.com/questions/452844/spline-interpolation-versus-polynomial-interpolation) I am unable to precisely determine the difference... I am seeking a single 3rd order polynomial, so I can take the second derivative. Which would you advise? Some definitions of "cubic" are "piecewise polynomial" which is not what I want. – Mtl Dev Aug 17 '19 at 06:27

2 Answers2

2

If you really want a single cubic parabola, you can use numpy.polyfit.

If what you need is a continuous 2nd derivative, then you're indeed after a cubic spline, which is nothing but a collection of cubics on successive intervals (hence piecewise), which are matched up to the 2nd derivative.

Depending on whether you want interpolation (match the data points exactly), or fitting (i.e. your data contain the signal and some noise, and you want to extract the former), you are after either CubicSpline, or splrep/BSpline.

ev-br
  • 24,968
  • 9
  • 65
  • 78
0

You can use polynomial regression to do this, the core concept of doing polynomial regression is

  • create polynomial features from your features. eg. if you have one feature x you need to add new features x^2, x^3 etc. upto the degree you want
  • now you will train linear regression model using new features set
  • you can also choose cost function relevant to you, like RMSE, MAE and more based one what you want to optimize for fitting the curve
  • you can also use regularization like l1 or l2 to get more smooth function

I found this blog explaining polynomial regression
https://towardsdatascience.com/machine-learning-polynomial-regression-with-python-5328e4e8a386

Dev Khadka
  • 5,142
  • 4
  • 19
  • 33