2

I want to fit a curve to some data using curve_fit in scipy. After I searched for the syntax I found this,

import numpy as np
from scipy.optimize import curve_fit

def func(x, a, b, c):
     return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
ydata = y + 0.2 * np.random.normal(size=len(xdata))

popt, pcov = curve_fit(func, xdata, ydata)

But the documentation is not really clear, specially regarding the parameters of the function func, I know x is the numpy array of independent variable values, but what are a, b, and c? Also, what does this line mean,

a * np.exp(-b * x) + c 

to calculate y we call func with the independent variables and other parameters, but what is ydata? And why do we calculate it this way,

ydata = y + 0.2 * np.random.normal(size=len(xdata))

One last thing, if I get the fitted model (the equation) by scipy inside some function, how can I use it in another one?

Any help is appreciated. Thanks.

1 Answer 1

2

curve_fit fits a set of data, ydata, with each point given at a value of the independent variable, x, to some model function. In the example, the model function is a * exp(-b * x) + c, where a, b and c are some constants to be determined to best represent the data with this model.

The y calculated by func is the value of the model function at each data point, x.

In the example, ydata is simulated by the fit function with some random Gaussian (normally-distributed) noise added to it:

ydata = y + 0.2 * np.random.normal(size=len(xdata))

You can see this by plotting y (green line) and ydata (blue dots) against xdata:

enter image description here

To use the fitted parameters, popt, pass them to func:

yfit = func(xdata, *popt)

A plot will show you a comparison of the "exact" (green line) and fit (blue line):

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @xnx, in ydata, why the quantity is multiplied by .2? And what is good initial choice of a,b, and c? Because I've tried this curve_fit function with different a,b, and c and it doesn't fit at all, my data is scattered, is there a better solution than curve fit?
The 0.2 just rescales the noise so there isn't too much of it for the example; if you can't fit your particular data well to this func, perhaps there simply aren't any a, b, c that do the job well with this model. The solution is to use a different model function, func!
Thank you, Is there any well known nonlinear function to use? Also, from your answer I understand that y is the model, and ydata is just some randomly generated data made to show the effect of the fitted curve y, is this correct?
That's right. I'm afraid without knowing what your data represent, it's impossible to recommend a nonlinear function. Usually you would have a model function in mind before you attempt to fit.
Good, I will select the most appropriate function to the data shape. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.