Python fit polynomial, power law and exponential from data

Nadir picture Nadir · Jun 8, 2014 · Viewed 11.3k times · Source

I have some data (x and y coordinates) coming from a study and I have to plot them and to find the best curve that fits data. My curves are:

  • polynomial up to 6th degree;
  • power law;
  • exponential.

I am able to find the best fit for polynomial with

while(i < 6):
    coefs, val = poly.polyfit(x, y, i, full=True)

and I take the degree that minimizes val.

When I have to fit a power law (the most probable in my study), I do not know how to do it correctly. This is what I have done. I have applied the log function to all x and y and I have tried to fit it with a linear polynomial. If the error (val) is lower than the others polynomial tried before, I choose the power law function(naturally if m of the line is negative). Am I correct?

Now how can I reconstruct my power law starting from the line y = mx + q in order to draw it with the original points? I need also to display the function found.

I have tried with:

def power_law(x, m, q):
    return q * (x**m)

using

x_new = np.linspace(x[0], x[-1], num=len(x)*10)
y1 = power_law(x_new, coefs[0], coefs[1])
popt, pcov = curve_fit(power_law, x_new, y1)

but the resulting curve is not fitting the data.

Answer

James Phillips picture James Phillips · Jun 8, 2014

If you google the phrase "curve fitting", my web site is the top return - so I know a bit about this sort of thing.

I recommend not making any log or other transform of the data, as scipy has a nonlinear solver that is perfect for this type of fitting. Look at:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html

I use the scipy nonlinear solver on my web site, which can directly fit your data online. Try:

http://zunzun.com/Equation/2/Power/Standard%20Power/

and to ensure there was no experimentally-introduced offset, such as a DC offset voltage for example, try:

http://zunzun.com/Equation/2/Power/Standard%20Power%20With%20Offset/

One problem you may run in to with non-linear fitting is choice of a suitable starting set of parameters for the non-linear solver to iteratively refine. The BSD-licensed source code for the web site uses a genetic algorithm to determine a starting point automatically, so you may want to try it yourself. It comes with many examples, including a "function finder" that fits hundreds of equations and ranks them - which you can also try online. The source code is at the Google Code Repository at:

https://code.google.com/p/pyeq2/

or links to zipped and tgz'd source distributions are at the bottom of every page on the web site.

Please contact me directly if you have any questions, I will be glad to help. I love this stuff.

James [email protected]