Curve Fitting

In this lesson, we will discuss curve fitting by finding the optimized parameters of an equation.

We'll cover the following

In some applications, polynomials might not be the best choice to fit a model to data. Instead, functions involving exponentials or sinusoids might be needed. While polyfit() fits a polynomial to data, there are many other functions that you may want to use to fit your data. For example, the function curve_fit() can be used to fit an arbitrary function that you define; curve_fit is part of the scipy.optimize package.

Syntax #

The curve_fit function requires you to write a function that takes the independent variable as its first argument followed by the parameter(s) that you want to fit and returns the value of the function at all values of the independent variable for the supplied parameters. For example, to fit a straight line, you would need to write a function:

def func(x, a, b):
    y = a * x + b
    return y

where x is the independent variable, y is the dependent variable and a and b are the function parameters for fitting the data.

Get hands-on with 1200+ tech skills courses.