gpt4 book ai didi

python - scipy.optimize_curvefit 给出不好的结果

转载 作者:行者123 更新时间:2023-12-01 08:26:19 29 4
gpt4 key购买 nike

我正在尝试拟合 Material 模型(Carreau-Law)。一般来说,数据看起来非常好,但不可能(至少对我来说)使用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:电源中遇到无效值。我已经尝试了很多变体,但现在陷入困境。

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

RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.

这是双对数刻度上的数据图像:

Original data on log-log scale

我真的不知道我哪里错了!数据看起来相当不错,这就是为什么我永远不应该用完 maxfev

最佳答案

这是一个使用您的数据和方程的图形拟合器。此示例代码使用 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)

关于python - scipy.optimize_curvefit 给出不好的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226525/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com