【问题标题】:How do I code dose-response (4PL) curve fitting with optimize.minimize()如何使用 optimize.minimize() 编码剂量反应 (4PL) 曲线拟合
【发布时间】:2019-10-14 17:50:49
【问题描述】:

我想使用数据集优化剂量反应曲线(4 参数逻辑)。我需要使用 Powell 算法,因此,我必须使用 optimize.minimize() 而不是 curve_fit 或最小二乘。 我写了以下代码:

import numpy as np
from scipy.optimize import minimize

ydata = np.array([0.1879, 0.4257, 0.80975, 1.3038, 1.64305, 1.94055, 2.21605, 2.3917])
xdata = np.array([40, 100, 250, 400, 600, 800, 1150, 1400])
initParams = [2.4, 0.2, 600.0, 1.0]

def logistic(params):
    A = params[0]
    B = params[1]   
    C = params[2]
    D = params[3]

    logistic4 = ((A-D)/(1.0+((xdata/C)**B))) + D
    sse = np.sum(np.square(ydata-logistic4))
    print sse

results = minimize(logistic, initParams, method='Powell')
print results

理论上,这可以最小化实验和理论数据集的 sse,迭代最初使用 Powell 算法输入的 4 个参数。 实际上,它不起作用:它开始了,最后一个错误,在一个相当长的列表中,是

TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'.

关于如何编码的任何想法?

【问题讨论】:

    标签: python curve-fitting scipy-optimize-minimize


    【解决方案1】:

    这是一个用于数据和方程的图形化 Python 求解器,它使用带有 'Powell' 的 minimize() 并且还对 curve_fit 进行了注释掉的调用。我无法很好地适应您提供的初始参数估计值,因此在此处将其注释掉并替换为我自己的值。我的方程搜索证实,这是一个用于对该数据集建模的优秀方程。

    import numpy, scipy, matplotlib
    import matplotlib.pyplot as plt
    from scipy.optimize import curve_fit
    from scipy.optimize import minimize
    
    xData = numpy.array([40, 100, 250, 400, 600, 800, 1150, 1400], dtype=float)
    yData = numpy.array([0.1879, 0.4257, 0.80975, 1.3038, 1.64305, 1.94055, 2.21605, 2.3917], dtype=float)
    
    
    def func(xdata, A, B, C, D):
        return ((A-D)/(1.0+((xdata/C)**B))) + D
    
    # minimize() requires a function to be minimized, unlike curve_fit()
    def SSE(inParameters): # function to minimize, here sum of squared errors
        predictions = func(xData, *inParameters) 
        errors = predictions - yData
        return numpy.sum(numpy.square(errors))
    
    
    #initialParameters = numpy.array([2.4, 0.2, 600.0, 1.0])
    initialParameters = numpy.array([3.0, -1.5, 500.0, 0.1])
    
    
    # curve fit the data with curve_fit()
    #fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)
    
    # curve fit the data with minimize()
    resultObject = minimize(SSE, initialParameters, method='Powell')
    fittedParameters = resultObject.x
    
    
    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)
    

    【讨论】:

    • 您的解决方案建议有何不同?为什么你的解决方案提案有效而 OP 无效?
    • @ThomasHesse 如果愿意,请比较两个发布的源代码。
    猜你喜欢
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多