【问题标题】:Fitting a Logistic Curve to Data将逻辑曲线拟合到数据
【发布时间】:2019-10-13 05:07:06
【问题描述】:

我想用 scipy 为一些数据拟合一个对数函数。

不幸的是,我收到以下错误:无法估计参数的协方差

如何防止这种情况发生?

import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt

x = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0]
y = [0.073, 2.521, 15.879, 48.365, 72.68, 90.298, 92.111, 93.44, 93.439, 93.389, 93.381, 93.367, 93.94, 93.269, 96.376]

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

(a_, b_, c_, d_), _ = opt.curve_fit(f, x, y)

y_fit = f(x, a_, b_, c_, d_)
fig, ax = plt.subplots(1, 1, figsize=(6, 4))
ax.plot(x, y, 'o')
ax.plot(x, y_fit, '-')

【问题讨论】:

    标签: python scipy curve-fitting


    【解决方案1】:

    经过几次尝试,我发现在计算与您的数据的协方差时存在问题。我试图删除 0.0 以防这是原因但不是。

    我发现的唯一选择是将计算方法从 lm 更改为 trf :

    x = np.array(x)
    y = np.array(y)
    
    popt, pcov = opt.curve_fit(f, x, y, method="trf")
    y_fit = f(x, *popt)
    fig, ax = plt.subplots(1, 1, figsize=(6, 4))
    ax.plot(x, y, 'o')
    ax.plot(x, y_fit, '-')
    plt.show()
    

    曲线与这些参数正确拟合[96.2823169 -2.38876852 1.39927921 2.98341838]

    【讨论】:

    • 这很容易解决!谢谢你:)
    【解决方案2】:

    这是一个包含您的数据和方程的图形拟合器,使用 scipy 的差分进化遗传算法进行初始参数估计。 scipy 实现使用拉丁超立方算法来确保对参数空间的彻底搜索,这需要搜索范围 - 正如您从代码中看到的那样,这些范围可以很宽,并且更容易为初始参数估计而不是给出具体值。

    import numpy, scipy, matplotlib
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    from scipy.optimize import differential_evolution
    import warnings
    
    
    xData = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0])
    yData = numpy.array([0.073, 2.521, 15.879, 48.365, 72.68, 90.298, 92.111, 93.44, 93.439, 93.389, 93.381, 93.367, 93.94, 93.269, 96.376])
    
    
    def func(x, a, b, c, d):
        return a / (1.0 + numpy.exp(-c * (x - d))) + b
    
    
    # function for genetic algorithm to minimize (sum of squared error)
    def sumOfSquaredError(parameterTuple):
        warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
        val = func(xData, *parameterTuple)
        return numpy.sum((yData - val) ** 2.0)
    
    
    def generate_Initial_Parameters():
        parameterBounds = []
        parameterBounds.append([0.0, 100.0]) # search bounds for a
        parameterBounds.append([-10.0, 0.0]) # search bounds for b
        parameterBounds.append([0.0, 10.0]) # search bounds for c
        parameterBounds.append([0.0, 10.0]) # search bounds for d
    
        # "seed" the numpy random number generator for repeatable results
        result = differential_evolution(sumOfSquaredError, parameterBounds, seed=3)
        return result.x
    
    # by default, differential_evolution completes by calling curve_fit() using parameter bounds
    geneticParameters = generate_Initial_Parameters()
    
    # now call curve_fit without passing bounds from the genetic algorithm,
    # just in case the best fit parameters are aoutside those bounds
    fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters)
    print('Fitted parameters:', fittedParameters)
    print()
    
    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()
    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)
    

    【讨论】:

    • 感谢您的这次旅行和非常好的示例代码:D
    【解决方案3】:

    我在 Python2.7 内核下尝试了你的代码。我没有收到你提到的错误。对于 x 的所有值,唯一的情况是 y_fit=71.50186844。

    【讨论】:

    • 由于您使用的初始参数估计,您的 y_fit 常数值看起来很可能,请使用 scipy 的差分进化遗传算法模块查看我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2021-08-07
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    相关资源
    最近更新 更多