1

我想使用数据集优化剂量反应曲线(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'.

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

4

1 回答 1

0

这是您的数据和方程的图形 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)
于 2019-10-14T19:49:17.140 回答