14

I'm trying to fit some data and stuff, I know there is a simple command to do this with python/numpy/matplotlib, but I can't find it. I think it is something like

popt,popc = numpy.curvefit(f,x)

where popt is the paramters of f, popc is the fit quality and f is a predefined function of f. Does any of you know it?

cxxl
  • 4,939
  • 3
  • 31
  • 52
Yotam
  • 10,295
  • 30
  • 88
  • 128
  • 1
    If this is a one-off, it's easier to just plug the values into the function finder at http://zunzun.com/ and let it find the best matching curve. Their Python code is available, too. – endolith Dec 02 '11 at 14:57

2 Answers2

29

Take a look at scipy.optimize.curve_fit:

scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw)

Use non-linear least squares to fit a function, f, to data.

ars
  • 120,335
  • 23
  • 147
  • 134
  • One question: is there an easy way to get R-squared value out of curve_fit, or get SSE so that I can calculate R-squared? I can do some work and get it done in scipy.optimize.leastsq, but the code will be much more verbose. – Yuxiang Wang Aug 20 '13 at 02:36
-1

Found it. Curve_fit from optimize in scipy

Yotam
  • 10,295
  • 30
  • 88
  • 128