gpt4 book ai didi

python - 使用 Scipy 与 ROOT 等拟合(高斯)

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:45 27 4
gpt4 key购买 nike

我现在多次偶然发现使用 scipy.curve_fit 在 python 中进行拟合比使用其他工具(例如根 ( https://root.cern.ch/ )

例如,在拟合高斯分布时,使用 scipy 我主要得到一条直线: enter image description here

对应代码:

def fit_gauss(y, x = None):
n = len(y) # the number of data
if x is None:
x = np.arange(0,n,1)
mean = y.mean()
sigma = y.std()

def gauss(x, a, x0, sigma):
return a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))

popt, pcov = curve_fit(gauss, x, y, p0=[max(y), mean, sigma])

plt.plot(x, y, 'b+:', label='data')
plt.plot(x, gauss(x, *popt), 'ro:', label='fit')
plt.legend()
plt.title('Gauss fit for spot')
plt.xlabel('Pixel (px)')
plt.ylabel('Intensity (a.u.)')
plt.show()

使用 ROOT,我得到了一个完美的契合,甚至没有给出启动参数: enter image description here

同样,对应的代码:

import ROOT
import numpy as np

y = np.array([2., 2., 11., 0., 5., 7., 18., 12., 19., 20., 36., 11., 21., 8., 13., 14., 8., 3., 21., 0., 24., 0., 12., 0., 8., 11., 18., 0., 9., 21., 17., 21., 28., 36., 51., 36., 47., 69., 78., 73., 52., 81., 96., 71., 92., 70., 84.,72., 88., 82., 106., 101., 88., 74., 94., 80., 83., 70., 78., 85., 85., 56., 59., 56., 73., 33., 49., 50., 40., 22., 37., 26., 6., 11., 7., 26., 0., 3., 0., 0., 0., 0., 0., 3., 9., 0., 31., 0., 11., 0., 8., 0., 9., 18.,9., 14., 0., 0., 6., 0.])
x = np.arange(0,len(y),1)
#yerr= np.array([0.1,0.2,0.1,0.2,0.2])
graph = ROOT.TGraphErrors()
for i in range(len(y)):
graph.SetPoint(i, x[i], y[i])
#graph.SetPointError(i, yerr[i], yerr[i])
func = ROOT.TF1("Name", "gaus")
graph.Fit(func)

canvas = ROOT.TCanvas("name", "title", 1024, 768)
graph.GetXaxis().SetTitle("x") # set x-axis title
graph.GetYaxis().SetTitle("y") # set y-axis title
graph.Draw("AP")

有人可以向我解释一下,为什么结果差异如此之大? scipy 中的实现是否糟糕/依赖于良好的启动参数?有什么办法吗?我需要自动处理大量拟合,但无法访问目标计算机上的 ROOT,因此它只能使用 python。

当从 ROOT 拟合中获取结果并将它们作为开始参数提供给 scipy 时,拟合也适用于 scipy ...

最佳答案

如果没有实际数据,要重现您的结果并不容易,但如果使用人工创建的嘈杂数据,我觉得效果不错:

enter image description here

这是我正在使用的代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

# your gauss function
def gauss(x, a, x0, sigma):
return a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))

# create some noisy data
xdata = np.linspace(0, 4, 50)
y = gauss(xdata, 2.5, 1.3, 0.5)
y_noise = 0.4 * np.random.normal(size=xdata.size)
ydata = y + y_noise
# plot the noisy data
plt.plot(xdata, ydata, 'bo', label='data')

# do the curve fit using your idea for the initial guess
popt, pcov = curve_fit(gauss, xdata, ydata, p0=[ydata.max(), ydata.mean(), ydata.std()])

# plot the fit as well
plt.plot(xdata, gauss(xdata, *popt), 'r-', label='fit')

plt.show()

和你一样,我也使用 p0=[ydata.max(), ydata.mean(), ydata.std()] 作为初始猜测,这似乎适用于不同的噪声强度。

编辑

我刚刚意识到你实际上提供了数据;然后结果如下所示:

enter image description here

代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit


def gauss(x, a, x0, sigma):
return a * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))

ydata = np.array([2., 2., 11., 0., 5., 7., 18., 12., 19., 20., 36., 11., 21., 8., 13., 14., 8., 3., 21., 0., 24., 0., 12.,
0., 8., 11., 18., 0., 9., 21., 17., 21., 28., 36., 51., 36., 47., 69., 78., 73., 52., 81., 96., 71., 92., 70., 84.,72.,
88., 82., 106., 101., 88., 74., 94., 80., 83., 70., 78., 85., 85., 56., 59., 56., 73., 33., 49., 50., 40., 22., 37., 26.,
6., 11., 7., 26., 0., 3., 0., 0., 0., 0., 0., 3., 9., 0., 31., 0., 11., 0., 8., 0., 9., 18.,9., 14., 0., 0., 6., 0.])

xdata = np.arange(0, len(ydata), 1)

plt.plot(xdata, ydata, 'bo', label='data')

popt, pcov = curve_fit(gauss, xdata, ydata, p0=[ydata.max(), ydata.mean(), ydata.std()])
plt.plot(xdata, gauss(xdata, *popt), 'r-', label='fit')

plt.show()

关于python - 使用 Scipy 与 ROOT 等拟合(高斯),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832373/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com