英文:
Scipy curve_fit not fitting to gaussian
问题
我已生成了以下代码的高斯分布:
x_array = np.linspace(0, 0.5, 100)
mu = 0.25 # 均值
sigma = 0.1 # 标准差
y_array_gauss = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_array - mu) / sigma) ** 2)
y_array_gauss = y_array_gauss * (1.3 - 1.1) + 1.1
df = pd.DataFrame({'时间(秒)': x_array, 'CH1(伏特)': y_array_gauss})
Scipy函数:
def fit2(data, func, fit_init_guess, fit_bounds):
fig, ax = plt.subplots(figsize=(20, 6), facecolor='w')
best_vals, covar = curve_fit(func, data['时间(秒)'], data['CH1(伏特)'], p0=fit_init_guess, bounds=fit_bounds)
print(best_vals, '\n', covar)
ax.plot(data['时间(秒)'], data['CH1(伏特)'], '-')
ax.plot(data['时间(秒)'], func(data['时间(秒)'], *best_vals), 'r-', label='拟合:B+A*e^((-(delta_2)^2)/(2*gamma^2)) \n B=%5.4f, A=%5.4f, gamma=%5.6f, t_0=%5.6f,' % tuple(best_vals))
plt.legend(loc='lower right')
return None
拟合函数:
def gauss2(B, A, delta, gamma, t_0):
delta_2 = delta - t_0
return B + A * np.exp((-(delta_2) ** 2) / (2 * gamma ** 2))
拟合参数:
fitInitGuess = [1.2, 1.9, 0.25, 0.25]
fitBounds = ((1.1, 0, 0, 0), (1.3, 2.5, 0.5, 0.5))
fit2(df, gauss2, fitInitGuess, fitBounds)
问题:
如您在上面的图中所见,我生成的高斯分布的拟合效果非常差。我传递给我的 fit2
函数和 scipy.optimize.curve_fit
的初始猜测和边界看起来应该产生更好的拟合结果。我以前从未遇到过这个问题,已经有几个人审查了这段代码,也许我错过了一些明显的东西,但为什么这个拟合不起作用呢?
英文:
I've generated a gaussian with the following code:
x_array = np.linspace(0,0.5,100)
mu = 0.25 # Mean
sigma = 0.1 # Standard deviation
y_array_gauss = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_array - mu) / sigma) ** 2)
y_array_gauss = y_array_gauss * (1.3 - 1.1) + 1.1
df = pd.DataFrame({'Time(s)': x_array, 'CH1(V)': y_array_gauss})
Scipy function:
def fit2(data, func, fit_init_guess, fit_bounds):
fig, ax = plt.subplots(figsize = (20,6), facecolor='w')
best_vals, covar = curve_fit(func, data['Time(s)'],data['CH1(V)'],p0 = fit_init_guess, bounds=fit_bounds)
print(best_vals,'\n', covar)
ax.plot(data['Time(s)'], data['CH1(V)'], '-')
ax.plot(data['Time(s)'], func(data['Time(s)'], *best_vals),'r-',label='Fit:B+A*e^((-(delta_2)^2)/(2*gamma^2)) \n B=%5.4f, A=%5.4f, gamma=%5.6f, t_0=%5.6f,' % tuple(best_vals))
plt.legend(loc='lower right')
return None
Fitting Function:
def gauss2(B, A, delta, gamma, t_0):
delta_2 = delta - t_0
return B+A*np.exp((-(delta_2)**2)/(2*gamma**2))
Fit Parameters:
fitInitGuess = [1.2, 1.9, 0.25, 0.25]
fitBounds=((1.1, 0, 0, 0),(1.3, 2.5, 0.5, 0.5))
fit2(df, gauss2, fitInitGuess, fitBounds)
Question:
As you can see in the plot above the fit to the gaussian that I generated is very poor. The initial guesses and bounds which I pass to my fit2
function, and to scipy.optimize.curve_fit
, seem like they should produce a better fit. I have never had this issue before, and I've had several people look over this code, perhaps I'm missing something obvious, but why is this fit not working?
答案1
得分: 3
变量参数应该放在第一位,而不是最后。
def gauss2(t_0, B, A, delta, gamma):
delta_2 = delta - t_0
return B + A * np.exp((-(delta_2) ** 2) / (2 * gamma ** 2))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论