You can also use numpy polyfit :
data = np.array([[1,5], [2,10], [3,15], [4,20], [5,25]]) fit = np.polyfit(data[:,0], data[:,1] ,1)
This allows you to easily change the degree of the polynomial, since the polyfit function takes the following arguments np.polyfit(x data, y data, degree) . A linear binding is displayed in which the returned array looks like fit[0]*x^n + fit[1]*x^(n-1) + ... + fit[n-1]*x^0 for any degree n . The poly1d function allows poly1d to turn this array into a function that returns the value of the polynomial for any given value of x .
In general, extrapolation without a well-understood model will result in sporadic results.
Exponential curve setting .
from scipy.optimize import curve_fit def func(x, a, b, c): return a * np.exp(-b * x) + c x = np.linspace(0,4,5) y = func(x, 2.5, 1.3, 0.5) yn = y + 0.2*np.random.normal(size=len(x)) fit ,cov = curve_fit(func, x, yn) fit [ 2.67217435 1.21470107 0.52942728]
Daniel
source share