我如何使用optimize.minimize()编码剂量-反应(4PL)曲线拟合

2024-09-30 18:25:47 发布

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

我想用数据集优化剂量-反应曲线(4参数logistic)。我需要使用鲍威尔算法,因此,我必须使用优化.最小化()代替曲线拟合或最小二乘法。 我编写了以下代码:

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'.

你知道怎么编码吗?你知道吗


Tags: 数据import算法参数npparamsarraysse
1条回答
网友
1楼 · 发布于 2024-09-30 18:25:47

这是一个用于数据和方程的图形化Python解算器,它使用带“Powell”的minimize(),还有一个注释掉的对curve\u fit的调用。我无法很好地拟合您提供的初始参数估计值,因此这里对这些参数进行了注释,并用我自己的值替换。我的方程搜索证实了这是一个很好的方程,用于建模这个数据集。你知道吗

plot

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)

相关问题 更多 >