【问题标题】:Exponential fitting using scipy.optimize.curve_fit without good guess使用 scipy.optimize.curve_fit 的指数拟合没有很好的猜测
【发布时间】:2019-08-28 02:48:14
【问题描述】:

我想找到一个描述以下数据的模型。

x = array([50000.,  100000.,  150000.,  200000.,  250000.,  300000.,
    350000.,  400000.,  450000.,  500000.,  550000.,  600000.,
    650000.,  700000.,  750000.,  800000.,  850000.,  900000.,
    950000., 1000000.])
y = array([1.87792730e-06, 3.81015841e-07, 1.89900422e-07, 1.21302069e-07,
   8.39703240e-08, 6.18937868e-08, 4.98975718e-08, 3.97720839e-08,
   3.23420144e-08, 2.79493666e-08, 2.35548293e-08, 2.01505953e-08,
   1.81079429e-08, 1.59391671e-08, 1.37227044e-08, 1.30031234e-08,
   1.19076952e-08, 1.10967303e-08, 9.43339053e-09, 8.98627485e-09])

查看数据点的分布,预计数据遵循指数函数。因此,我尝试按以下方式使用 scipy.optimize.curve_fit。

from scipy.optimize import curve_fit
import numpy as np
from matplotlib import pyplot as plt 

def f(x, a, b, c): 
    return a*np.exp(b*x)+c

curve_fit(f, x, y, p0=[np.min(y), -1, np.min(y)])

这并没有给我任何合理的 a、b 和 c 值。我尝试使用几个 p0 设置,但 pcov 矩阵总是只包含 inf。

如果您有数据但对这种情况下的参数没有很好的猜测,您如何实现合理的拟合?

【问题讨论】:

    标签: python curve-fitting


    【解决方案1】:

    这是一个图形 Python 拟合器,它使用与您发布的数据上的方程式搜索不同的方程式,它似乎使用所有 1.0 的 scipy 默认初始参数估计值提供了极好的拟合。

    import numpy, scipy, matplotlib
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    
    xData = numpy.array([50000.0, 100000.0, 150000.0, 200000.0, 250000.0, 300000.0, 350000.0, 400000.0, 450000.0, 500000.0, 550000.0, 600000.0, 650000.0, 700000.0, 750000.0, 800000.0, 850000.0, 900000.0, 950000.0, 1000000.0])
    yData = numpy.array([1.8779273e-06, 3.81015841e-07, 1.89900422e-07, 1.21302069e-07, 8.3970324e-08, 6.18937868e-08, 4.98975718e-08, 3.97720839e-08, 3.23420144e-08, 2.79493666e-08, 2.35548293e-08, 2.01505953e-08, 1.81079429e-08, 1.59391671e-08, 1.37227044e-08, 1.30031234e-08, 1.19076952e-08, 1.10967303e-08, 9.43339053e-09, 8.98627485e-09])
    
    
    def func(x, a, b, c): # from zunzun.com equation search
        return a / (b+numpy.power(x, c))
    
    
    # these are the same as the scipy defaults
    initialParameters = numpy.array([1.0, 1.0, 1.0])
    
    # curve fit the test data
    fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
    
    modelPredictions = func(xData, *fittedParameters) 
    
    absError = modelPredictions - yData
    
    SE = numpy.square(absError) # squared errors
    MSE = numpy.mean(SE) # mean squared errors
    RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
    Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
    
    print('Parameters:', fittedParameters)
    print('RMSE:', RMSE)
    print('R-squared:', Rsquared)
    
    print()
    
    
    ##########################################################
    # graphics output section
    def ModelAndScatterPlot(graphWidth, graphHeight):
        f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
        axes = f.add_subplot(111)
    
        # first the raw data as a scatter plot
        axes.plot(xData, yData,  'D')
    
        # create data for the fitted equation plot
        xModel = numpy.linspace(min(xData), max(xData))
        yModel = func(xModel, *fittedParameters)
    
        # now the model as a line plot
        axes.plot(xModel, yModel)
    
        axes.set_xlabel('X Data') # X axis data label
        axes.set_ylabel('Y Data') # Y axis data label
    
        plt.show()
        plt.close('all') # clean up after using pyplot
    
    graphWidth = 800
    graphHeight = 600
    ModelAndScatterPlot(graphWidth, graphHeight)
    

    【讨论】:

      【解决方案2】:

      有一种简单的方法(无需初始猜测,无需迭代演算),其原理在论文中进行了说明:https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

      非线性回归通过积分方程转换为线性回归。您的问题和数值演算的应用如下所示。

      请注意,拟合标准(最小均方)并不完全针对给定数据,而是针对具有积分数值演算的变换数据(在下面注明 S)。因此,如果结果的准确性不够,则需要进行非线性回归。开始迭代过程的初始值可以是已经找到的离目标不远的值。

      所以,我认为这回答了你关于寻找初始猜测的问题。

      您的数据的数值示例:

      注意:对于 Y 的高值,结果非常好。但对于 Y 的最小值,结果并不准确。根据拟合标准,可能需要使用非线性回归进行后处理。

      注意:在您的数据示例中,微积分涉及非常高和非常低的指数。建议对原始数据应用方便的因子,以便以通常的数量级对其进行绑定。上面的微积分示例没有这样做,以使其更易于理解。

      信息:

      允许线性回归的积分方程是:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-25
        • 2018-11-22
        • 1970-01-01
        • 2019-03-01
        • 1970-01-01
        • 2013-06-25
        • 2019-06-26
        • 1970-01-01
        相关资源
        最近更新 更多