Curve Fitting with Scipy in Python

Shen Ge
CodeX
Published in
4 min readNov 22, 2021

--

Photo by Chris Liverani on Unsplash

Curve fitting is frequently encountered to model real-world systems or observations. Given a set of inputs collected by some manner — through experiments, censuses, government archives, user inputted databases, etc. — what we are looking for is to find a function (called the objective function) that best models this data.

There are many types of objective functions. The simplest curve fitting function is a line. Fitting by a linear function is frequently called linear regression. Other types of objective functions include polynomial, trigonometric and anything else you can think of under the sun. Depending on the data, some would fit better than others. Having a good theoretical foundation of what generated the data would give you a better idea of which objective function to actually use.

In the scenario that you do not know any theoretical equation that could describe the relationship between the two variables, you will have to make educated guesses of objective functions. We won’t dive into the mathematics here of how we show what objective function is considered the best, i.e. perform regression diagnostics, since that deserves a separate article.

Coding Time with Scipy

Let us do a real-world example with some real data and using the Python library scipy to do the heavy lifting for us. Let…

--

--

Shen Ge
CodeX

Engineer and console operator who helped America land back on the moon 2/22/2024. Enjoys code + poetry. Become a member: https://medium.com/@shenge86/membership