python最小二乘法拟合

这篇博客详细介绍了如何使用Python进行最小二乘法拟合,通过实例展示了多项式拟合的过程,帮助读者理解并应用该方法解决实际问题。
摘要由CSDN通过智能技术生成

https://www.jianshu.com/p/354b1f2a5fd0
https://www.cnblogs.com/qi-yuan-008/p/12323535.html

import numpy as np
import matplotlib.pyplot as plt
from numpy import polyval
# https://baijiahao.baidu.com/s?id=1643468349140251072&wfr=spider&for=pc
# 多项式拟合代码如下
from scipy.optimize import curve_fit
def ax_bfit(x,y,calculate_x,n):
    x=np.array(x)
    y=np.array(y)
    calculate_x=np.array(calculate_x)
    fit_coef=np.polyfit(x,y,n)
    calculate_y=np.polyval(fit_coef,calculate_x)
    fit_coef=[round(i,3) for i in fit_coef]
    plt.plot(x,y,'^')
    plt.plot(calculate_x,calculate_y)
    plt.show()
    return fit_coef,calculate_y
def func(x,a,b,c):
    return  a*np.exp(b*x)*c
# 幂函数拟合
def a_xfit(x,y,calculate_x):
    plt.plot(x,y,'.')
    popt,pcov=curve_fit(func,x,y)
    calculate_y = [func(i,popt[0],popt[1],popt[2]) for i in calculate_x]
    plt.plot(calculate_x,calculate_y,'r--')
    plt.show()
    return popt,calculate_y
def fund(x,a,b):
    return x**a+b
# 指数拟合
def x_afit(x,y,calculate_x):
    plt.plot(x,y,'.')
    popt,pcov= curve_fit(fund,x,y)
    calculate_y=[fund(i,popt[0],popt[1]) for i in calculate_x]
    popt=[round(i,3) for i in popt]
    plt.plot(calculate_x,calculate_y,'r__')
    plt.show()
    return  popt,calculate_y

if __name__ == '__main__':
    # x=[1,2,3,4,5,6,7,8,9,10,11,12,13]
    # y = [1, 2, 4, 26, 33, 50, 58, 84, 104, 142, 170, 186, 199]
    # calculate_x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14,15,16]
    dataList = [1476917.5, 1476800.0, 1476697.0, 1476697.0, 1476289.0, 1476118.0, 1475955.5, 1475810.5, 1475727.5,
                1475671.0, 1475576.0, 1475497.0, 1475428.5, 1475340.0, 1475263.5, 1475153.5, 1474983.0, 1474947.5,
                1474852.0, 1474789.0, 1474661.5, 1474608.5, 1474484.0, 1474468.5, 1476153.0, 1476049.0, 1475947.0,
                1475870.0, 1475792.0, 1475713.5, 1474991.5, 1474777.0, 1474334.5, 1474266.0, 1474210.0, 1473672.5,
                1473571.0, 1473155.5, 1473116.0, 1473042.0, 1472923.5, 1472857.5, 1472750.0, 1472635.5, 1472530.0,
                1471566.0, 1471491.0, 1471464.0, 1471423.5, 1471363.5, 1471306.5, 1471252.5, 1471207.5, 1471146.0,
                1471103.5, 1471077.0, 1471002.5, 1470933.5, 1470875.5, 1470764.0, 1470718.0, 1470661.0, 1470623.5,
                1470623.5, 1470564.0, 1470526.5, 1470409.5, 1470478.5, 1469858.0, 1469804.5, 1469825.5, 1470201.0,
                1470186.5, 1470138.5, 1470057.0, 1469579.0, 1469501.5, 1469425.0, 1469412.0, 1469402.0, 1469393.0,
                1469358.5, 1469324.5, 1469288.0, 1469255.5, 1469265.5, 1469198.5, 1469132.5, 1469180.5, 1469205.5,
                1469269.0, 1469289.5, 1469278.5, 1469234.5, 1469240.0, 1469223.0, 1469208.0, 1469186.5, 1469154.5,
                1469172.5, 1469140.5, 1469115.5, 1469116.5, 1469121.5, 1469068.0, 1468998.5, 1468944.0, 1468903.0,
                1468889.0, 1468843.5, 1468831.0, 1468818.0, 1468816.0, 1469257.0, 1469318.5, 1469402.5, 1469398.0,
                1469511.5, 1469490.5, 1469432.5, 1469439.5, 1469427.0, 1469468.0, 1469468.0, 1469442.5, 1469436.0,
                1469455.5, 1469381.5, 1469243.0, 1469238.0, 1468692.5, 1469425.0, 1469478.0, 1469455.0, 1469472.0,
                1469405.0, 1469411.5, 1469393.5, 1469395.0, 1469438.0, 1469421.0, 1469428.5, 1469433.5, 1469322.5,
                1469204.5, 1469086.0, 1469113.0, 1469185.5, 1469269.5, 1469396.0, 1469477.5, 1469490.0, 1469577.5,
                1469624.5, 1469657.5, 1469701.5, 1469756.5, 1469763.5, 1469765.5, 1469814.0, 1469823.5, 1469859.5,
                1469866.0, 1469866.0, 1469865.0, 1469909.5, 1469925.5, 1469918.5, 1469904.0, 1469558.5, 1469665.5,
                1469665.0, 1469822.5, 1468417.0, 1468393.0, 1468376.5, 1468283.5, 1468269.0, 1468260.5, 1468244.0,
                1468189.0, 1468245.5, 1468296.5, 1468296.5, 1468327.0, 1468230.5, 1468289.0, 1468280.0, 1468273.0,
                1468263.5, 1469467.0, 1469288.5, 1469322.5, 1469333.0, 1469343.5, 1469339.5, 1469299.0, 1469297.5,
                1469291.5, 1469236.0, 1469206.5, 1469167.5, 1469177.5, 1469157.0, 1469145.5, 1469128.0, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值