Nonlinear Regression - Using a Spline

From: Is there a way to plot a curve of best fit without function?

Typically to smooth without a guess of a generating function, people use a spline. For example

import matplotlib.pyplot as plt from scipy.interpolate import UnivariateSpline import numpy as np x = range(30) y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ] s = UnivariateSpline(x, y, s=5) xs = np.linspace(0, 29, 100) ys = s(xs) plt.plot(x, y, 'o') plt.plot(xs, ys) plt.show()

As you've probably guessed (without a model/function), the keyword 's' is used to set how closely the fit matches the data, where s=0 will go through every point.

Splines basically fit a simple function to local sets of points from the curve and then match the derivatives at the boundaries to connect these local curves so the end result looks smooth.

There are a variety of spline routines to choose from in scipy.