I wanted to fit a power law function to data, not a polynomial. Here's how I did it.

I used the least squares method.

If you prefer other types of functions, just change the function form to whatever you want to fit when you define it.

The inputs for leastsq are the error function (difference between data and a function you want to fit) and initial conditions. When full_output = nonzero, it returns the covariance matrix in addition to the parameters that minimize the sum of squares of the error function.

What would you fit a power law function to?

I attended the Summer School on Statistics for Astronomers 2017 at Penn State University last week and came to know that least square fitting is not a good idea to fit the power law models. If you need more accurate results, use other methods like maximum likelihood method. You also may want to see fittings for Pareto distribution for which they have well defined analytic solutions for fitting power laws.