[英]fitting data to 2D function using lmfit in python
I wish to fitting data with a 2D function in order to extract parameter (a) and (b) using lmfit package. 我希望使用2D函数拟合数据,以便使用lmfit软件包提取参数(a)和(b)。 Basically as a 1D function fitting, I am trying to fit every data point to the 2D function at the same coordinates (x,y).
基本上,作为一维函数拟合,我正在尝试在相同坐标(x,y)上将每个数据点拟合到2D函数。 This is mean that every data point has its initial guess value which different from another data point since each data has different coordinate(x,y).
这意味着每个数据点都具有与另一个数据点不同的初始猜测值,因为每个数据具有不同的坐标(x,y)。 This is my code:
这是我的代码:
#!/usr/bin/ python
import pyfits
import numpy as np
import math
from lmfit import minimize, Parameters, Parameter, report_errors,report_fit,
conf_interval, printfuncs
xn =np.linspace(0,3,4) # x-component
yn =np.linspace(0,3,4) # y-component
data= [0.0, 0.16, 0.33, 0.5, 0.2, 0.26, 0.38, 0.53, 0.4, 0.43, 0.52, 0.64, 0.6, 0.62,
0.67, 0.78] # (x1,y1) generate (data[0]), (x1,y2) generate (data[1]) and so on
params = Parameters()
params.add('a', value=3)
params.add('b', value=5)
def residual(params,x,y,data=None):
a = params['a'].value # parameter
b = params['b'].value # parameter
model=(x**2/a**2+y**2/b**2)**0.5 # 2D function
if data is None:
return data
return model - data
out=minimize(residual,params,args=(x,y,data,)) # lmfit minimizer
final=data+out.residual
report_fit(params)
ci = conf_interval(out, sigmas=[0.68,0.95]) # confidence interval
printfuncs.report_ci(ci)
However, I got this error message: 但是,我收到此错误消息:
ValueError: operands could not be broadcast together with shapes (4) (16)
Obviously, dimension of x and y is not the same of data, but I don't know how to make data[0] takes (x1,y1), data[1] takes (x1,y2)..., data[5] takes (x2,y1) and so on. 显然,x和y的维数与数据不同,但是我不知道如何使data [0]取(x1,y1),data [1]取(x1,y2)...,data [ 5]取(x2,y1),依此类推。 Please could anyone help me to fix this problem or make any suggestion, thank you in advance.
请任何人帮助我解决此问题或提出任何建议,谢谢。
Do you need to zip your data? 您需要压缩数据吗?
x = [x1, x2, ...]
y = [y1, y2, ...]
data = zip(x, y) # [(x1, y1), (x2, y2), ... ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.