To perform continuous least squares polynomial regression in Python, we can use the numpy.polyfit
function which fits a polynomial of degree n
to a set of data points using the least squares method.
Here's an example code:
main.py571 chars23 lines
To make this a continuous curve, we can use the make_interp_spline
function from Scipy to interpolate the data points with a B-spline. Here's the modified code:
main.py707 chars29 lines
In this modified code, we first fit a polynomial of degree 2 to the data points, just like before. Then we use make_interp_spline
to interpolate the data points with a B-spline. We specify the degree of the spline to be the same as the polynomial degree, so that the fitted curve is still a polynomial. Finally, we evaluate the fitted curve at a higher resolution x range and plot it along with the original data points.
gistlibby LogSnag