scipy.optimize_curvefit提供糟糕的结果

2024-09-30 00:29:13 发布

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

我在试着适应一个物质模型(卡罗定律)。一般来说,数据看起来很好,但是用curve_fit获得正确的模型数据和参数是不可能的(至少对我来说)。我试着设定合理的起始值等

import numpy as np
import matplotlib.pyplot as plt

## Y-DATA
eta = np.array([7128.67, 6814, 6490, 6135.67, 5951.67,
        5753.67, 5350, 4929.33, 4499.33,4068.67, 3641.33,
        3225.33, 2827.33, 2451, 2104.67, 1788, 1503, 1251.33,
        1032.33, 434.199, 271.707, 134.532, 75.7034, 40.9144, 21.7112, 14.9206, 9.29772])


##X-DATA
gamma = np.array([0.1, 0.1426, 0.2034, 0.29, 0.4135, 0.5897, 0.8409, 1.199,
         1.71, 2.438, 3.477, 4.959, 7.071, 10.08, 14.38, 20.5,
         29.24, 41.7, 59.46, 135.438, 279.707, 772.93,
         1709.91, 3734.32, 8082.32, 12665.8, 22353.3])


carreaulaw = lambda x, eta_0, lam, a, n: eta_0 / (1 + (lam * x)**a)**((n-1)/a)

popt, pcov = sp.optimize.curve_fit(carreaulaw, gamma, eta, p0=[8000, 3000, 0.8, 0.1])

print(popt)

x = np.linspace(gamma.min(), gamma.max(), 500)
fig = plt.figure()
diagram = fig.add_axes([0.1, 0.1, 0.8, 0.8])
diagram.set_xlabel(r"$log\ \. \gamma_{true}\ (s^{-1})$", fontsize = 12)
diagram.set_ylabel(r"$log\ \eta_{true}\ (Pa*s)$",fontsize = 12)
#diagram.set_xscale("log")
#diagram.set_yscale("log")
diagram.plot(gamma, eta, "r*")
diagram.plot(x, carreaulaw(x, popt[0], popt[1], popt[2], popt[3]), "g-")

我经常收到错误:RuntimeWarning: invalid value encountered in power。我已经试过很多变奏曲了,现在很难接受。在

如果我不给出任何起始值,我得到:

^{pr2}$

以下是日志刻度上的数据图像:

Original data on log-log scale

我真的不知道我哪里错了!数据看起来很好,这就是为什么我永远不会用完maxfev。在


Tags: 数据模型importlogasnpfitdiagram
2条回答

您需要做的就是传递curve_fit中的边界。当没有定义边界时,可以进行非实数运算,比如(在您的例子中)负数的浮点求幂。在

边界被简单地定义为两个具有上下限的列表/元组的列表:

bounds = [(-np.inf, 0, 0, 0), [np.inf, np.inf, 1, 1]]   #upper np.inf or lower -np.inf means no bound
popt, pcov = curve_fit(carreaulaw, gamma, eta, p0=[8000, 3000, 0.8, 0.1], bounds=bounds)

输出:

enter image description here

这是一个使用数据和公式的图形拟合器。此示例代码使用scipy的差分进化遗传算法来确定curve_fit()的初始参数估计。这个scipy模块使用拉丁超立方体算法来确保对参数空间的彻底搜索,这需要搜索范围。找到参数的范围要比单个值容易得多,在这里,我尝试了不同的边界,直到拟合看起来对我来说还可以。你应该检查一下我使用的界限,看看它们是否合理。在

plot

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.optimize import differential_evolution
import warnings

xData = numpy.array([7128.67, 6814, 6490, 6135.67, 5951.67,
        5753.67, 5350, 4929.33, 4499.33,4068.67, 3641.33,
        3225.33, 2827.33, 2451, 2104.67, 1788, 1503, 1251.33,
        1032.33, 434.199, 271.707, 134.532, 75.7034, 40.9144, 21.7112, 14.9206, 9.29772])

yData = numpy.array([0.1, 0.1426, 0.2034, 0.29, 0.4135, 0.5897, 0.8409, 1.199,
         1.71, 2.438, 3.477, 4.959, 7.071, 10.08, 14.38, 20.5,
         29.24, 41.7, 59.46, 135.438, 279.707, 772.93,
         1709.91, 3734.32, 8082.32, 12665.8, 22353.3])

def carreaulaw(x, eta_0, lam, n, a):
    return eta_0 * (1.0+(lam*x)**a)**((n-1.0)/a)


# function for genetic algorithm to minimize (sum of squared error)
def sumOfSquaredError(parameterTuple):
    warnings.filterwarnings("ignore") # do not print warnings by genetic algorithm
    val = carreaulaw(xData, *parameterTuple)
    return numpy.sum((yData - val) ** 2.0)


def generate_Initial_Parameters():

    parameterBounds = []
    parameterBounds.append([0.0, 50.0]) # search bounds for eta_0
    parameterBounds.append([0.0, 1.0]) # search bounds for lam
    parameterBounds.append([-1.0, 0.0]) # search bounds for n
    parameterBounds.append([-200.0, 0.0]) # search bounds for a

    # "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(carreaulaw, xData, yData, geneticParameters)
print('Fitted parameters:', fittedParameters)
print()

modelPredictions = carreaulaw(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 = carreaulaw(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)

相关问题 更多 >

    热门问题