如何解决尝试使用curve_fit拟合3D高斯并遇到广播问题Python
今天我花了至少2个小时来尝试使3D高斯拟合工作,但是到目前为止,我一直没有成功!我的数据在一个名为“ data”的NumPy数组中,并且data [x,y,z]给出位置(x,y,z)(所有位置均为非负整数)的函数值。我正在尝试使用以下形式的3D高斯:
# Our function gauss_3d is a function w = f(x,y,z)
# Returns the value "w" of the 3D Gaussian at point (x,z)
def gauss_3d(x,z,amplit,x0,y0,z0,sig_x,sig_y,sig_z,offset):
sum = offset + amplit*np.exp(-(((x-x0)**2/(2*sig_x**2)) + \
((y-y0)**2/(2*sig_y**2) + ((z-z0)**2/(2*sig_z**2)))))
return sum
我的曲线拟合代码如下:
dim = data.shape
# Create meshgrid
XX,YY,ZZ = np.meshgrid(np.arange(dim[2]),np.arange(dim[1]),np.arange(dim[0]))
# Create data_in,which consists of (x,z) points. data_in has shape (392,3)
# i.e. 392 points of the form (x,z),where x,z are non-negative integers
data_in = np.vstack((XX.ravel(),YY.ravel(),ZZ.ravel())).T
# Create data_out,which is the ravelled data. data_out has shape (392,)
data_out = data.ravel()
print(data_in)
p_param,p_cov = opt.curve_fit(gauss_3d,data_in,data_out)
print("Fitted params:")
print(p_param)
但是,当前出现错误“ ValueError:操作数不能与形状(392,3)(392,)一起广播”。我检查了文档,并说Y和X必须具有相同的尺寸M,在我的情况下,它们都具有392作为第一尺寸M = 392。那么为什么这不起作用呢? data_in必须具有尺寸(392,3),否则,我无法在其中放置(x,y,z)值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。