如何改进多相指数方程数据的曲线拟合?

2024-04-29 15:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个项目,我需要将一些数据拟合到方程中 y = bmax_1 * np.exp(-koff_1 * x) + bmax_2 * np.exp(-koff_2*x) with bmax_1, koff_1, bmax_2, koff_2作为参数。我尝试过使用curve_fit,但结果很差,给出的R平方值为0.16。我想知道我是否可以做些什么来改善体型


    #Define model function
    def func(x, bmax_1, koff_1, bmax_2, koff_2):
        return bmax_1*np.exp(-koff_1*x) + bmax_2 * np.exp(-koff_2 *x)
    
    # 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 np.sum((yData - val) ** 2.0)
    
    def generate_Initial_Parameters():
        parameterBounds = []
        parameterBounds.append([0.0, 200.0]) # search bounds for bamx_1
        parameterBounds.append([0.0, 10.0]) # search bounds for koff_1
        parameterBounds.append([0.0, 200.0]) # search bounds for bmax_2
        parameterBounds.append([0.0, 10.0]) # search bounds for koff_2
        # "seed" the numpy random number generator for repeatable results
        result = differential_evolution(sumOfSquaredError, parameterBounds, seed=4)
        return result.x

    column = input('Column to be analysed: ')
    xData = df.loc[:, 'T']
    yData = df.loc[:, column]
    geneticParameters = generate_Initial_Parameters()
    
    fittedParameters, pcov = curve_fit(func, xData, yData, geneticParameters, bounds = )
    print('Fitted parameters:', fittedParameters)

拟合参数为[1.24066146e+02 1.48240328e-02 1.34805335e+01 8.26108828e-01]

拟合结果:
Result for fitting


Tags: forsearch参数returndefnpfunccurve
1条回答
网友
1楼 · 发布于 2024-04-29 15:22:24

既然迭代方法所需的初始值对评估提出了挑战,为什么不尝试一种不需要初始值的非迭代方法呢

下面的方法来自https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

最终,如果迭代方法对于达到某些特殊规范是绝对必要的,那么下面方法的结果可以用作非常好的初始值

enter image description here

-

如果要在不使用参数a的情况下拟合函数:

enter image description here

相关问题 更多 >