fit function in python

from scipy import optimize
def f(x,a,b): return x*a+b
popt, pcov = optimize.curve_fit(f, xdata, ydata)
#popt contains the fitted parameters and pcov is the covariance matrix
#with variances on the diagonal

Here is what the above code is Doing:
1. Define a function f(x,a,b) that returns a*x+b.
2. Call optimize.curve_fit(f, xdata, ydata) to fit the function to the data.
3. popt contains the fitted parameters and pcov is the covariance matrix with variances on the diagonal.