为什么Python';s曲线拟合未完成优化?

为什么Python';s曲线拟合未完成优化?,python,curve-fitting,Python,Curve Fitting,我需要找到方程的两个参数,它们最适合给定的x和y值 我使用的是Python3,带有Numpy和Scipy from scipy.optimize import curve_fit def func(dx, d50, p): return (1 / (1 + ((d50 / dx) ** p))) xdata = [280, 150, 75, 45, 38, 20, 10, 5.1, 2.6] ydata = [99.57592773, 95.53773499, 81.14313507

我需要找到方程的两个参数,它们最适合给定的x和y值

我使用的是Python3,带有Numpy和Scipy

from scipy.optimize import curve_fit

def func(dx, d50, p):
    return (1 / (1 + ((d50 / dx) ** p)))

xdata = [280, 150, 75, 45, 38, 20, 10, 5.1, 2.6]
ydata = [99.57592773, 95.53773499, 81.14313507, 67.08183289, 62.93716431, 49.961483, 37.80876923, 24.53152657, 13.2219696]

# curve fit:
popt, pcov = curve_fit(func, xdata, ydata)
print(popt)

I expect a d50 ~ 20 and a p > 0.
但请寄给我:

[0.00221498 1.60291553]

> /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:4:
> RuntimeWarning: invalid value encountered in power

从sys.path中删除
cwd
后。

我无法使用您帖子中的公式获得与您数据的良好拟合。我的方程搜索发现,对于参数a=103.1533969、b=498.93546398和c=2.67321918,标准Weibull峰值方程“a*exp(-0.5*pow(log(x/b)/c,2.0))”给出了RMSE=1.619和R平方=0.997,如下所示。我已经包括了一个Python图形装配师,他使用这个方程和标准的scipy微分进化遗传算法模块来查找curve_fit()的初始参数估计,这个scipy模块使用拉丁超立方体算法来确保对参数空间的彻底搜索,该算法需要搜索的范围。在本例中,搜索边界是从数据派生的。确定初始参数估计值的范围比查找特定值容易得多


我无法使用你帖子中的等式获得与你数据的良好拟合。我的方程搜索发现,对于参数a=103.1533969、b=498.93546398和c=2.67321918,标准Weibull峰值方程“a*exp(-0.5*pow(log(x/b)/c,2.0))”给出了RMSE=1.619和R平方=0.997,如下所示。我已经包括了一个Python图形装配师,他使用这个方程和标准的scipy微分进化遗传算法模块来查找curve_fit()的初始参数估计,这个scipy模块使用拉丁超立方体算法来确保对参数空间的彻底搜索,该算法需要搜索的范围。在本例中,搜索边界是从数据派生的。确定初始参数估计值的范围比查找特定值容易得多


重要的是,数据输入与等式不一致,这是我后来意识到的。。。sigmoid方程要求y的值在0和1之间,即yData除以100。因此,曲线调整工作良好一个重要的事情,数据输入与方程式不一致,这是我后来意识到的。。。sigmoid方程要求y的值在0和1之间,即yData除以100。因此,曲线调整效果良好
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.optimize import differential_evolution
import warnings


xData = [280, 150, 75, 45, 38, 20, 10, 5.1, 2.6]
yData = [99.57592773, 95.53773499, 81.14313507, 67.08183289, 62.93716431, 49.961483, 37.80876923, 24.53152657, 13.2219696]


def func(x, a, b, c): # Peak_WeibullPeak_model from zunzun.com
    return a * numpy.exp(-0.5 * numpy.power(numpy.log(x/b) / c, 2.0))


# 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():
    # min and max used for bounds
    maxX = max(xData)
    minX = min(xData)
    maxY = max(yData)
    minY = min(yData)

    minData = min(minX, minY)
    maxData = max(maxY, maxX)

    parameterBounds = []
    parameterBounds.append([minData, maxData]) # search bounds for a
    parameterBounds.append([minData, maxData]) # search bounds for b
    parameterBounds.append([minData, maxData]) # search bounds for c

    # "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)