python: curve_fit() to achieve data fitting

This function can be used not only to fit and draw straight lines, quadratic curves, and cubic curves, but also to fit and draw any form of curve based on the form in the code , as long as a suitable curve equation is defined.

Features ⬇ xdata can be an array, that is, it can achieve multiple regression

xdata:array_like or object

The independent variable where the data is measured. Should usually be an M-length sequence or an (k,M)-shaped array for functions with k predictors, but can actually be any object.

Application examples:

The following are the extreme temperature values ​​(degrees Celsius) for each month in Alaska since January:
Maximum value: 17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18
Minimum value: -62, -59, -56, -46, -32, -18, -9, -13, -25, -46, -52, -58
Use scipy.optimize.curve_fit() to fit this function and data

analysis:

It can be seen that the temperature is a sine function with a period of 12 (here I regarded it as 365 at the beginning, and the result is not good...)

Construction function
f (x) = a ⋅ sin (2 π ⋅ x 12 + b) + cf(x)=a\cdot sin(\frac{2\pi \cdot x)(12)+b )+cf(x)=as i n (122 πx+b)+c
Use this function to complete the fitting, the following are the original data and the fitting curve (the code is eaten):
Insert picture description here
Insert picture description here
see the official documentcurve_fit document for theimplementation method

Guess you like

Origin blog.csdn.net/qq_45268474/article/details/108065667