Hi,(adsbygoogle = window.adsbygoogle || []).push({});

I'm trying to get how well a known function fits to a set of data. I'm not interested in the data's line of best fit or anything, I just want to know how close it is to my model. I've tried using curve_fit and linregress but neither really give me what I'm after. My data follows a logarithmic curve, which I've been plotting up on loglog scales to get a gradient of about -4, which is close to my model (-3.9), but I'd like to know exactly how close. Linregress so far is the closest match for what I'm after, as it gives the correlation coefficient, how well the data follows the line of best fit, but it's still not exactly what I want.

def line(x,a,b):

return a*x+b

x = np.log(range(len(coll_ave)))

x = x[1:] # I've done this to avoid the whole ln(0)=infinity thing

y = np.log(coll_ave[1:])

popt, pcov = curve_fit(line, x, y, sigma=error[1:])

grad, inter, r_value, p_value, std_err = stats.linregress(x, y)

These give me great info, just not quite what I'm looking for. As far as I'm aware, polyfit doesn't work for linear models, and I'd rather work with the loglog of my data than the raw data, as I know what gradient I'm after, but I have the equation for the model as well, so really it doesn't matter. If there's a numpy or scipy version, that would be great. Or a modification to curve_fit or linregress that would make it work.

Thanks for the help :D

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Python 2.7: Fit a model to data

Tags:

Loading...

Similar Threads - Python model data | Date |
---|---|

Python How can I run Python by getting values from Matlab | Mar 7, 2018 |

Python Interesting article how to use Pandas with Excel sheets | Feb 19, 2018 |

Porting Python Code to Fortran for Parallel Computing | Dec 15, 2017 |

What's wrong with my bisection method code? | Dec 10, 2017 |

Python Topic Modeling takes too long | Nov 24, 2017 |

**Physics Forums - The Fusion of Science and Community**