编写一个错误函数来在python中提供scipy.optimize.least_squares

时间:2018-05-08 16:13:26

标签: python python-2.7 numpy scipy curve-fitting

我正在尝试将一些数据拟合到非线性函数中,并希望使用模型函数来查看我是否能够获得比我现有的更好的拟合。当我试图解决问题时,我提出了更多问题。我有:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares
from scipy.optimize import curve_fit

temperature = [ 38., 40., 42., 44., 46., 48., 50., 52., 54., 56., 58., 60., 62., 64., 66., 68., 70., 71.9, 73.81, 75.69, 77.6, 79.49, 81.38, 83.29, 85.19, 87.11, 89., 90., 91., 92., 93., 94., 95., 96., 97., 98., 99., 100. ]
exp_rate = [  8.71171203e-01, 1.15342914e+00, 1.39178845e+00, 1.66700007e+00, 1.96267002e+00, 2.32390602e+00, 2.68542886e+00, 3.13116448e+00, 3.60152705e+00, 4.12575295e+00, 4.67617489e+00, 5.29745193e+00, 6.06796117e+00, 6.99056274e+00, 8.40124338e+00, 1.04449551e+01, 1.38236107e+01, 1.96811651e+01, 2.91545190e+01, 4.67945718e+01, 7.36377025e+01, 1.19474313e+02, 1.91938580e+02, 3.07692308e+02, 4.92610837e+02, 7.87401575e+02, 1.20738388e+03, 1.51773627e+03, 1.89049140e+03, 2.33880380e+03, 2.90892166e+03, 3.53003887e+03, 4.28065700e+03, 5.15251443e+03, 6.18043152e+03, 7.49720729e+03, 9.57524225e+03, 1.17175325e+04]

def Orbach_Raman(temperature, pre_1, U_1, C, n): # This is my model function
    return np.array( (1./pre_1)*np.exp(-U_1/(temperature)) + C*(temperature**n) )

pre_1, U_1, C, n = np.array([1.17E-12, 1815, 1E-6, 3.77]) # Define the starting guess
guess = pre_1, U_1, C, n
popt_stret, pcov = curve_fit(Orbach_Raman, temperature, exp_rate, p0=guess)

但是curve_fit()无法找到最佳参数并且会引发

File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 680, in curve_fit
raise RuntimeError("Optimal parameters not found: " + errmsg)
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000.

这非常奇怪,因为起始猜测已经提供了非常好的数据拟合

plt.loglog(temperature, exp_rate, '-o')
plt.loglog(temperature, Orbach_Raman(temperature, pre_1, U_1, C, n ), '-*')
plt.show()

enter image description here

所以我接着尝试编写自己的错误函数来使用least_square()而不是curve_fit(),我为之前的代码添加了

def error(guess, rate):
    pre_1, U_1, C, n = guess
    return Orbach_Raman(temperature, pre_1, U_1, C, n) - rate

least_squares(error(guess, exp_rate), guess, args=(exp_rate))

收到以下错误

File "fit_experiment.py", line 46, in <module>
least_squares(error(guess, exp_rate), guess, args=(exp_rate))
File "/usr/lib/python2.7/dist-packages/scipy/optimize/_lsq/least_squares.py", line 769, in least_squares
f0 = fun_wrapped(x0)
File "/usr/lib/python2.7/dist-packages/scipy/optimize/_lsq/least_squares.py", line 764, in fun_wrapped
return np.atleast_1d(fun(x, *args, **kwargs))
TypeError: 'numpy.ndarray' object is not callable

有谁知道

  • 为什么即使猜测参数已经非常接近数据,curve_fit()也会失败?
  • 为什么在调用least_squares时会出现错误(error(guess,exp_rate),guess,args =(exp_rate))?
  • 为什么如果我调用least_squares(error,guess,args =(exp_rate)),它会引发 TypeError:error()只需要2个参数(给定39个)

1 个答案:

答案 0 :(得分:5)

我认为答案是:

  1. 为什么即使猜测参数已经非常接近数据,curve_fit()也会失败?
  2. 我不确定。它可能不是&#34;失败&#34;和#34;在多次迭代后放弃&#34;。你看看结果了吗?

    我还建议,由于你的情节实际上(并且合理地)在对数尺度上,你可能还会在对数尺度上拟合。也就是说,让模型函数返回模型的日志,并使其适合log(exp_rate)

    1. 为什么在调用least_squares时会出现错误(error(guess,exp_rate),guess,args =(exp_rate))?
    2. 这是因为least_squares()希望第一个参数是函数,它返回残差,计算的残差。因此,请使用least_squares(error, guess...)而不是least_squares(error(guess, exp_rate), guess, ...)

      1. 为什么如果我调用least_squares(error,guess,args =(exp_rate)),它会引发TypeError:error()只需要2个参数(39个给定)
      2. 这是因为容易被愚弄的方式说'#34; tuple with 1 element&#34;在Python中。 args=(exp_rate)被解释为具有exp_rate(可能是39个数据点)的组件的元组,而不是#34;具有一个元素且第一个元素为exp_rate的元组。你想要的是添加一个尾随逗号(这是真正定义元组的,而不是括号):    args=(exp_rate, )

        希望有所帮助。