python:curve_fit()实现数据拟合
本函数不仅可以用于直线、二次曲线、三次曲线的拟合和绘制,仿照代码中的形式,可以适用于任意形式的曲线的拟合和绘制,只要定义好合适的曲线方程即可。
特点⬇ xdata可以是数组,就是可以实现多元回归
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.
应用例子:
下面是从1月开始阿拉斯加每个月的温度极值(摄氏度):
最大值:17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18
最小值:-62, -59, -56, -46, -32, -18, -9, -13, -25, -46, -52, -58
用scipy.optimize.curve_fit()拟合这个函数与数据
分析:
可以看出温度是以周期为12的正弦函数(这里一开始被我当成365了结果咋也做不好。。。)
构建函数
使用本函数完成拟合,下面是原始数据和拟合曲线(代码被吃了):
实现方法见官方文档 curve_fit文档