Univariate nonlinear regression in Python
Regression analysis is an area of statistics and data science which is used extensively for finding relationships between independent and dependent variables in datasets collected from real sources (for instance, measured data from scientific experiments). There are many types of regression we can do, but ultimately it all comes down to either linear or nonlinear regression. I already discussed the idea of linear regression in another set of articles (starting with https://medium.com/@oscarnieves100/linear-regression-with-straight-lines-8c476fc566d0), so now I will discuss how to do nonlinear regression with multiple parameters using Python’s scipy.optimize module, for cases in which simple variable transformation tricks (like taking logarithms of the data) simply won’t work.
Disclaimer: this is not an article on the mathematical theory of nonlinear regression, but rather how to use existing Python modules and intuition to find good fits for our data.
Loading the right modules
We start by importing the following modules and functions into our Python workspace:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
plt.style.use('seaborn-poster')
We use numpy for handling numerical data and array, matplotlib for plotting, scipy.optimize for the regression, and I use the ‘seaborn-poster’ style from matplotlib for my plots.