尝试将高斯拟合到数据时 scipy.optimize.curve_fit 出现奇怪的行为
我必须将高斯曲线拟合到一些数据。这是我的高斯函数的定义
def gaussian(x,sigma,mu):
return np.array((1/(sigma*np.sqrt(2*np.pi)))*np.exp((-1/2)*np.power(((x-mu)/sigma),2)),dtype='float64')
这是我对高斯函数定义的测试
x = np.linspace(-5,5,100)
y = gaussian(x,1,0)
plt.plot(x,y)
它产生了一个行为正确的图, 这是绘图的图像
现在我必须将此高斯拟合到一些数据。
x = np.array([ 8., 10., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22.,
23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35.,
36.],dtype='float64')
y = np.array([0.00220264, 0.00220264, 0.00220264, 0.00220264, 0.0154185 ,
0.01321586, 0.02863436, 0.03303965, 0.06387665, 0.05506608,
0.1123348 , 0.08590308, 0.0814978 , 0.0814978 , 0.04625551,
0.06387665, 0.05726872, 0.05947137, 0.05726872, 0.02863436,
0.03744493, 0.02863436, 0.01101322, 0.00881057, 0.01101322,
0.00220264, 0.00881057],dtype='float64')
y_error = np.array([0.00220264, 0.00220264, 0.00220264, 0.00220264, 0.00582765,
0.00539535, 0.00794174, 0.0085308 , 0.0118616 , 0.01101322,
0.01573002, 0.0137555 , 0.01339816, 0.01339816, 0.01009378,
0.0118616 , 0.01123132, 0.01144527, 0.01123132, 0.00794174,
0.00908173, 0.00794174, 0.00492526, 0.00440529, 0.00492526,
0.00220264, 0.00440529],dtype='float64')
使用绘制此数据
plt.errorbar(x,y,yerr=y_error,fmt='.')
会产生以下 plot
当我尝试使用 scipy.optimize 拟合一些数据时, .curve_fit 我得到
popt, pcov = scipy.optimize.curve_fit(gaussian,x,y,sigma=y_error,absolute_sigma=True)
plt.plot(x,gaussian(x,*popt)) # plotting the gaussian
这会产生这个图,叠加在我的原始数据的上一个图上。这根本不是钟形曲线。为什么会这样?我检查了我的数据是否有问题,但没有发现任何奇怪的地方。所有传递到曲线拟合的数组都是 float64,我的高斯函数可以接受“float64”的 np.array 并输出相同类型的数组。我很困惑会出什么问题。
I have to fit a gaussian curve to some data. Here is the definition of my gaussian
def gaussian(x,sigma,mu):
return np.array((1/(sigma*np.sqrt(2*np.pi)))*np.exp((-1/2)*np.power(((x-mu)/sigma),2)),dtype='float64')
Here is my test of my gaussian function definition
x = np.linspace(-5,5,100)
y = gaussian(x,1,0)
plt.plot(x,y)
It produces an plot that behaves correctly,
here is the image of the plot
Now I have to fit this gaussian to some data.
x = np.array([ 8., 10., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22.,
23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35.,
36.],dtype='float64')
y = np.array([0.00220264, 0.00220264, 0.00220264, 0.00220264, 0.0154185 ,
0.01321586, 0.02863436, 0.03303965, 0.06387665, 0.05506608,
0.1123348 , 0.08590308, 0.0814978 , 0.0814978 , 0.04625551,
0.06387665, 0.05726872, 0.05947137, 0.05726872, 0.02863436,
0.03744493, 0.02863436, 0.01101322, 0.00881057, 0.01101322,
0.00220264, 0.00881057],dtype='float64')
y_error = np.array([0.00220264, 0.00220264, 0.00220264, 0.00220264, 0.00582765,
0.00539535, 0.00794174, 0.0085308 , 0.0118616 , 0.01101322,
0.01573002, 0.0137555 , 0.01339816, 0.01339816, 0.01009378,
0.0118616 , 0.01123132, 0.01144527, 0.01123132, 0.00794174,
0.00908173, 0.00794174, 0.00492526, 0.00440529, 0.00492526,
0.00220264, 0.00440529],dtype='float64')
Plotting this data using
plt.errorbar(x,y,yerr=y_error,fmt='.')
yields the following plot
now when I try to fit some data using scipy.optimize.curve_fit
I get
popt, pcov = scipy.optimize.curve_fit(gaussian,x,y,sigma=y_error,absolute_sigma=True)
plt.plot(x,gaussian(x,*popt)) # plotting the gaussian
This produces this plot, superimposed on the previous plot of my original data. This is not a bell curve at all. Why is it behaving this way? I checked for stuff that was wrong with my data and couldn't find anything strange. All the arrays passed into curve fit are float64, my gaussian function can accept a np.array of 'float64' and output an array of the same type. I'm perplexed as to what can go wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过一番修改后,解决方案只是通过将 p0=[sigma_guess,mean_guess] 作为参数传递给 curve_fit() 来提供对平均值的良好初始猜测。 此图就是结果。对于上下文(我试图在存在放射源的情况下拟合盖革计数器测量的每个采样间隔的发生次数与计数,因此计算平均值的估计值很容易)。
After some tinkering, the solution was just to supply a good initial guess on the mean by passing p0=[sigma_guess,mean_guess] as a parameter to curve_fit(). this plot is the result. For context (I was trying to fit occurrence vs counts per sampling interval measured by a Geiger counter in the presence of a radioactive source, so computing an estimate for the mean was easy).