我知道有一个问题
Fitting only one parameter of a function with many parameters in python
但我的情况有点不同。 lambda函数参数问题。
我正在尝试使用Lorentz func
<b>Warning</b>: move_uploaded_file(../upload/hangouts_message.ogg): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\Files\UploadToServer.php</b> on line <b>7</b><br />
<br />
<b>Warning</b>: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php6FE8.tmp' to '../upload/hangouts_message.ogg' in <b>C:\xampp\htdocs\Files\UploadToServer.php</b> on line <b>7</b><br />
fail
如果我只适合一个参数(xc)它的工作效果。
def lorentz(ph, A, xc, w, f0):
return f0 + A * w**2 / (w**2 + (ph - xc)**2)
但是,如果我尝试仅适合2个参数(a,xc)则失败
p1, p2, p3, p4 = params
popt, pcov = curve_fit(lambda x, xc: lorentz(x, p1, xc, p3, p4), abjd, adata, bounds=param_bounds)
错误讯息是
p1, p2, p3, p4 = params
popt, pcov = curve_fit(lambda x, a, xc: lorentz(x, a, xc, p3, p4), abjd, adata, bounds=param_bounds)
答案 0 :(得分:2)
以下是Lorentz函数的所有4个参数的解决方案
import numpy as np
from scipy.optimize import curve_fit
def lorentz(ph, A, xc, w, f0):
return f0 + A * w**2 / (w**2 + (ph - xc)**2)
A, xc, w, f0 = 2,2,2,2 # true values
ph = np.linspace(-5, 10, 100)
y = lorentz(ph, A, xc, w, f0)
ydata = y + 0.15 * np.random.normal(size=len(ph)) # sample data
popt, pcov = curve_fit(lambda x, _A, _xc: lorentz(x, _A, _xc, w, f0), ph, ydata,bounds=([1,1], [3, 3]))
A, xc = popt # fitted values (only two)
您可以通过将参数放在函数lorentz()或lambda
下来轻松添加或删除参数