几周前我刚刚学习了 python,目前在将数据拟合到给定函数时遇到了问题。我尝试了不同的方法来拟合我的数据,但我不断收到错误 RuntimeWarning: invalid value, or sth similiar (比如除以零)。我定义了我想要适合以下 python 函数的函数:
def pval(x, A_0, n_0, E_0, a_troe, T_3, T_1, T_2,):#depending on the method 'p' insted of the parameters i.e: pval(x,p)
#A_0, n_0, E_0, a_troe, T_3, T_1, T_2 = p[0:7]
A_u = 14099393133.869781
n_u = 0.043936990336299386
E_u = 16281.619590689397
k_u = A_u * (x[:,0]**n_u) * numpy.exp(-E_u/x[:,0])
x_troe = ((A_0 * (x[:,0]**n_0) * numpy.exp(-E_0 / x[:, 0])) / k_u) * (x[:, 1] * 1.01325 * 10**(-1)) / (8.3141 * x[:, 0])
Fc = (1-a_troe) * numpy.exp(-x[:,0]/T_3) + a_troe * numpy.exp(-x[:,0]/T_1) + numpy.exp(-T_2/x[:,0])
O = numpy.log10(x_troe) - 0.4 - 0.67 * numpy.log10(Fc)
U = 0.75 - 1.27 * numpy.log10(Fc) - 0.14 * (numpy.log10(x_troe) - 0.4 - 0.67 * numpy.log10(Fc))
log_F = numpy.log10(Fc)/(1 + (O/U)**2)
f = numpy.log10(k_u) - numpy.log10(1+1/x_troe) + log_F
return f
如您所见,我想拟合 7 个参数,并且该函数有 2 个自变量。这里给出了我的数据的摘录,我尝试过的任何方法都不起作用:
x= [[1.0e+03 1.0e-02]
[1.0e+03 1.0e-01]
[1.0e+03 1.0e+00]
[1.0e+03 1.0e+01]
[1.0e+03 1.0e+02]
[1.1e+03 1.0e-02]
[1.1e+03 1.0e-01]
[1.1e+03 1.0e+00]
[1.1e+03 1.0e+01]
[1.1e+03 1.0e+02]
[1.2e+03 1.0e-01]
[1.2e+03 1.0e+00]
[1.2e+03 1.0e+01]
[1.2e+03 1.0e+02]
[1.3e+03 1.0e+00]
[1.3e+03 1.0e+01]
[1.3e+03 1.0e+02]
[1.4e+03 1.0e+00]
[1.4e+03 1.0e+01]
[1.4e+03 1.0e+02]
[1.5e+03 1.0e+01]
[1.5e+03 1.0e+02]
[1.6e+03 1.0e+01]
[1.6e+03 1.0e+02]
[1.7e+03 1.0e+02]
[1.8e+03 1.0e+02]
[1.9e+03 1.0e+02]
[2.0e+03 1.0e+02]]
y = [2.89894501 2.99443594 3.11048533 3.18421145 3.20302744 3.15204054
3.37720969 3.62462651 3.78744276 3.83868541 3.64709041 4.00417085
4.26080811 4.36152197 4.28960902 4.63156552 4.79327409 4.50830342
4.9238101 5.15010835 5.1568065 5.44522578 5.34414656 5.68986891
5.89350044 6.06378356 6.20646696 6.32558954]
我尝试的第一个解决方案是 leastsq() 函数:
import numpy
from scipy import *
from scipy.optimize import leastsq
def residuals(p, y, x): # in this case pval defined as pval(x,p)
err = y - pval(x, p)
return err
startpars = numpy.array([1.88e+13, -1.03, 11980, 0.76, 1.0e+10, 1.74, 9.33e+09], dtype=numpy.float64)
plsq = leastsq(residuals, startpars, args=(y, x), maxfev=2000)
print(plsq)
输出:
RuntimeWarning: invalid value encountered in log10
我尝试了几种启动参数下一个方法:curve_fit
import numpy
from scipy import *
from scipy.optimize import curve_fit
print(curve_fit(pval, x, y,))
它返回与前一个方法相同的错误消息。我试图定义 startparameter 并设置边界,但下一个方法不起作用:lmfit
import numpy
from lmfit import Model
startpars = numpy.array([1.88e+13, -1.03, 11980, 0.76, 1.0e+10, 1.74, 9.33e+09], dtype=numpy.float64)
lmfit_model = Model(pval)
lmfit_result = lmfit_model.fit(y, x=x, A_0 = startpars[0], n_0= startpars[1], E_0= startpars[2], a_troe= startpars[3], T_3= startpars[4], T_1= startpars[5], T_2= startpars[6])
lmfit_Rsquared = 1 - lmfit_result.residual.var() / numpy.var(y)
print('Fit R-squared:', lmfit_Rsquared, '\n')
print(lmfit_result.fit_report())
输出:
ValueError: The model function generated NaN values and the fit aborted! Please check your model function and/or set boundaries on parameters where applicable. In cases like this, using "nan_policy='omit'" will probably not work.
我还尝试了另一种在网上找到的解决方案。那里使用了 Levenberg-Marquardt 算法,但它的代码很长,我认为我的问题已经足够长了。但如果需要,我也可以发布此解决方案。
我在优化运行时打印了一些值,并且一些值确实变成了 inf 或 -inf,但正如在设置边界之前所写的那样,它不起作用。
我希望你们中的任何人都有一个想法。预先感谢。如果您需要更多信息,我很乐意提供我的问题