A follow-up to the previous post.
Polynomial fitting is also very easy with the numpy packages polyfit and poly1d.
If I want to use them directly as a fit function, just embed them in a new polynomial object:
Polynomial fitting is also very easy with the numpy packages polyfit and poly1d.
In [196]: x = range(100)Here I am asking polyfit to fit me a 2nd degree polynomial.
In [197]: y = randn(100)
In [198]: plot(x,y)
Out[198]: []
In [199]: polyfit(x,y,2)The polyfit function returns the polynomial coefficients in a list.
Out[199]: array([-0.00018313, 0.01669275, -0.09621319])
If I want to use them directly as a fit function, just embed them in a new polynomial object:
In [200]: fitfunc = poly1d(polyfit(x,y,2))Saving the plot like this
In [201]: plot(x,fitfunc(x))
Out[201]: []
In [202]: savefig('/Users/maye/Desktop/blog_polyfit.png')
and looks like this: