Scipy curve_fit不能拟合高斯函数

huangapple go评论103阅读模式
英文:

Scipy curve_fit not fitting to gaussian

问题

我已生成了以下代码的高斯分布:

  1. x_array = np.linspace(0, 0.5, 100)
  2. mu = 0.25 # 均值
  3. sigma = 0.1 # 标准差
  4. y_array_gauss = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_array - mu) / sigma) ** 2)
  5. y_array_gauss = y_array_gauss * (1.3 - 1.1) + 1.1
  6. df = pd.DataFrame({'时间(秒)': x_array, 'CH1(伏特)': y_array_gauss})

Scipy函数:

  1. def fit2(data, func, fit_init_guess, fit_bounds):
  2. fig, ax = plt.subplots(figsize=(20, 6), facecolor='w')
  3. best_vals, covar = curve_fit(func, data['时间(秒)'], data['CH1(伏特)'], p0=fit_init_guess, bounds=fit_bounds)
  4. print(best_vals, '\n', covar)
  5. ax.plot(data['时间(秒)'], data['CH1(伏特)'], '-')
  6. 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))
  7. plt.legend(loc='lower right')
  8. return None

拟合函数:

  1. def gauss2(B, A, delta, gamma, t_0):
  2. delta_2 = delta - t_0
  3. return B + A * np.exp((-(delta_2) ** 2) / (2 * gamma ** 2))

拟合参数:

  1. fitInitGuess = [1.2, 1.9, 0.25, 0.25]
  2. fitBounds = ((1.1, 0, 0, 0), (1.3, 2.5, 0.5, 0.5))
  3. fit2(df, gauss2, fitInitGuess, fitBounds)

拟合结果:
Scipy curve_fit不能拟合高斯函数

问题:

如您在上面的图中所见,我生成的高斯分布的拟合效果非常差。我传递给我的 fit2 函数和 scipy.optimize.curve_fit 的初始猜测和边界看起来应该产生更好的拟合结果。我以前从未遇到过这个问题,已经有几个人审查了这段代码,也许我错过了一些明显的东西,但为什么这个拟合不起作用呢?

英文:

I've generated a gaussian with the following code:

  1. x_array = np.linspace(0,0.5,100)
  2. mu = 0.25 # Mean
  3. sigma = 0.1 # Standard deviation
  4. y_array_gauss = (1 / (sigma * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_array - mu) / sigma) ** 2)
  5. y_array_gauss = y_array_gauss * (1.3 - 1.1) + 1.1
  6. df = pd.DataFrame({'Time(s)': x_array, 'CH1(V)': y_array_gauss})

Scipy function:

  1. def fit2(data, func, fit_init_guess, fit_bounds):
  2. fig, ax = plt.subplots(figsize = (20,6), facecolor='w')
  3. best_vals, covar = curve_fit(func, data['Time(s)'],data['CH1(V)'],p0 = fit_init_guess, bounds=fit_bounds)
  4. print(best_vals,'\n', covar)
  5. ax.plot(data['Time(s)'], data['CH1(V)'], '-')
  6. 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))
  7. plt.legend(loc='lower right')
  8. return None

Fitting Function:

  1. def gauss2(B, A, delta, gamma, t_0):
  2. delta_2 = delta - t_0
  3. return B+A*np.exp((-(delta_2)**2)/(2*gamma**2))

Fit Parameters:

  1. fitInitGuess = [1.2, 1.9, 0.25, 0.25]
  2. fitBounds=((1.1, 0, 0, 0),(1.3, 2.5, 0.5, 0.5))
  3. fit2(df, gauss2, fitInitGuess, fitBounds)

Resulting fit:
Scipy curve_fit不能拟合高斯函数

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

变量参数应该放在第一位,而不是最后。

  1. def gauss2(t_0, B, A, delta, gamma):
  2. delta_2 = delta - t_0
  3. return B + A * np.exp((-(delta_2) ** 2) / (2 * gamma ** 2))

Scipy curve_fit不能拟合高斯函数

英文:

variable parameter is supposed to be first, not last.

  1. def gauss2(t_0, B, A, delta, gamma):
  2. delta_2 = delta - t_0
  3. return B+A*np.exp((-(delta_2)**2)/(2*gamma**2))

Scipy curve_fit不能拟合高斯函数

huangapple
  • 本文由 发表于 2023年6月16日 08:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定