簡體   English   中英

編寫錯誤函數以在python中喂scipy.optimize.least_squares

[英]Writing an error function to feed scipy.optimize.least_squares in python

我正在嘗試將一些數據擬合到一個非線性函數中,並希望使用模型函數來查看是否可以得到比現有函數更好的擬合。 在嘗試解決問題時,我提出了更多問題。 我有:

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()

在此處輸入圖片說明

因此,我然后嘗試編寫自己的錯誤函數以使用Minimum_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

有人知道嗎

  • 即使guess參數已經很好地逼近數據,為什么curve_fit()也會失敗?
  • 為什么在調用minimum_squares(error(guess,exp_rate),guess,args =(exp_rate))時出現該錯誤?
  • 為什么如果我改為調用minimum_squares(error,guess,args = {exp_rate)),則會引發TypeError:error()恰好接受2個參數(給定39個)

我認為答案是:

  1. 即使guess參數已經很好地逼近數據,為什么curve_fit()也會失敗?

我不確定。 它可能不會像“多次迭代后放棄”那樣“失敗”。 你看結果了嗎?

我還建議,由於您的繪圖實際上(並且明智地)在對數刻度上,因此您可能也適合對數刻度。 也就是說,讓您的模型函數返回模型的日志,並擬合log(exp_rate)

  1. 為什么在調用minimum_squares(error(guess,exp_rate),guess,args =(exp_rate))時出現該錯誤?

這是因為least_squares()希望第一個參數是返回殘差而不是計算出的殘差的函數 因此,請使用least_squares(error, guess...)而不是least_squares(error(guess, exp_rate), guess, ...)

  1. 為什么如果我改為調用minimum_squares(error,guess,args = {exp_rate)),則會引發TypeError:error()恰好接受2個參數(給定39個)

這是因為在Python中說“帶有1個元素的元組”的方法很容易上當。 args=(exp_rate)解釋為具有exp_rate組件(可能是39個數據點)的元組,而不是“具有一個元素且第一個元素為exp_rate的元組。您想要的是添加尾隨逗號(這就是確實定義了一個元組,而不是括號): args=(exp_rate, )

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM