物理学中的高斯曲线拟合
Gaussian curve fitting in physics
我有这个数据,我试图通过高斯函数进行拟合,但找不到合适的函数,我尝试使用 scipy.optimize 中的 curve_fit :
time_s = [1.44692600e+09, 1.44692634e+09, 1.44692671e+09, 1.44692707e+09,
1.44692743e+09, 1.44692785e+09, 1.44692826e+09, 1.44692941e+09,
1.44692967e+09, 1.44692997e+09, 1.44693029e+09, 1.44693062e+09,
1.44693096e+09, 1.44693131e+09, 1.44693200e+09, 1.44693227e+09,
1.44693254e+09, 1.44693284e+09, 1.44693313e+09, 1.44693342e+09,
1.44693370e+09, 1.44693398e+09, 1.44693429e+09, 1.44693460e+09,
1.44693492e+09, 1.44693522e+09, 1.44693552e+09, 1.44693586e+09,
1.44693620e+09, 1.44693652e+09, 1.44693683e+09, 1.44693713e+09,
1.44693744e+09, 1.44693775e+09, 1.44693804e+09, 1.44693831e+09,
1.44693858e+09, 1.44693886e+09, 1.44693914e+09, 1.44693941e+09,
1.44693967e+09, 1.44693993e+09, 1.44694020e+09, 1.44694047e+09,
1.44694075e+09, 1.44694102e+09, 1.44694130e+09, 1.44694160e+09,
1.44694190e+09, 1.44694220e+09, 1.44694251e+09, 1.44694284e+09,
1.44694319e+09, 1.44694356e+09, 1.44694392e+09, 1.44694427e+09,
1.44694464e+09, 1.44694505e+09, 1.44694546e+09, 1.44694586e+09,
1.44694624e+09, 1.44694662e+09, 1.44694703e+09, 1.44694744e+09]
Temperature = [829.331306, 931.702088, 890.075633, 830.659093, 878.715978, 866.238768
, 897.958014, 940.495055, 841.990924, 875.391469, 898.393043, 925.048353
, 931.445104, 904.151363, 965.550728, 916.348809, 936.315168, 900.445995
, 887.76832, 875.064126, 881.480871, 878.240278, 862.958271, 893.813659
, 883.678318, 923.593998, 915.52458, 877.919073, 891.754242, 919.274917
, 862.223914, 881.275387, 862.33147, 869.461632, 890.014577, 902.656117
, 874.446393, 876.284046, 866.751916, 854.095049, 844.540741, 870.263794
, 866.687327, 818.019291, 821.875267, 813.385138, 843.198211, 870.558259
, 794.039978, 813.497634, 812.217789, 801.361143, 800.263045, 747.101493
, 735.923635, 732.930255, 775.930026, 783.786631, 775.255742, 774.938671
, 704.186773, 747.612911, 729.315237, 694.021293]
我使用了这个代码:
def Gauss(x, a1, b1, c1, a2, b2, c2 ):
return a1*np.exp(-((x-b1)/c1)**2) + a2*np.exp(-((x-b2)/c2)**2)
parameters, covariance = curve_fit(Gauss, time_s, Temperature)
plt.plot(time_s, Gauss(time_s, *parameters))
plt.show()
这是此拟合的结果:
非常不合身
错误信息:
/home/lhoussine/anaconda3/lib/python3.8/site-packages/scipy/optimize/minpack.py:828: OptimizeWarning: Covariance of the parameters could not be estimated
warnings.warn('Covariance of the parameters could not be estimated',
首先,您不是在拟合高斯函数,而是在拟合几个函数的总和。我不确定为什么,但这肯定没有帮助,也不符合您的问题。
但即使我们拟合单个高斯分布,主要问题是 curve_fit
的初始猜测。除非您另有说明,否则 the default for all parameters is 1
。考虑到您的时间戳似乎符合 1e9 的顺序,这在您的案例中是一个非常糟糕的初始猜测。
如果您为初始猜测提供粗略估计,curve_fit
实际上能够做得相当不错:
def gaussian(x, amplitude, mean, sd):
return amplitude * np.exp(-(x - mean) ** 2.0 / sd ** 2.0)
parameters, covariance = curve_fit(gaussian, time_s, Temperature, p0=(900, np.mean(time_s), 1000))
plt.plot(time_s, Temperature)
plt.plot(time_s, gaussian(time_s, *parameters))
这给了我们:
我有这个数据,我试图通过高斯函数进行拟合,但找不到合适的函数,我尝试使用 scipy.optimize 中的 curve_fit :
time_s = [1.44692600e+09, 1.44692634e+09, 1.44692671e+09, 1.44692707e+09,
1.44692743e+09, 1.44692785e+09, 1.44692826e+09, 1.44692941e+09,
1.44692967e+09, 1.44692997e+09, 1.44693029e+09, 1.44693062e+09,
1.44693096e+09, 1.44693131e+09, 1.44693200e+09, 1.44693227e+09,
1.44693254e+09, 1.44693284e+09, 1.44693313e+09, 1.44693342e+09,
1.44693370e+09, 1.44693398e+09, 1.44693429e+09, 1.44693460e+09,
1.44693492e+09, 1.44693522e+09, 1.44693552e+09, 1.44693586e+09,
1.44693620e+09, 1.44693652e+09, 1.44693683e+09, 1.44693713e+09,
1.44693744e+09, 1.44693775e+09, 1.44693804e+09, 1.44693831e+09,
1.44693858e+09, 1.44693886e+09, 1.44693914e+09, 1.44693941e+09,
1.44693967e+09, 1.44693993e+09, 1.44694020e+09, 1.44694047e+09,
1.44694075e+09, 1.44694102e+09, 1.44694130e+09, 1.44694160e+09,
1.44694190e+09, 1.44694220e+09, 1.44694251e+09, 1.44694284e+09,
1.44694319e+09, 1.44694356e+09, 1.44694392e+09, 1.44694427e+09,
1.44694464e+09, 1.44694505e+09, 1.44694546e+09, 1.44694586e+09,
1.44694624e+09, 1.44694662e+09, 1.44694703e+09, 1.44694744e+09]
Temperature = [829.331306, 931.702088, 890.075633, 830.659093, 878.715978, 866.238768
, 897.958014, 940.495055, 841.990924, 875.391469, 898.393043, 925.048353
, 931.445104, 904.151363, 965.550728, 916.348809, 936.315168, 900.445995
, 887.76832, 875.064126, 881.480871, 878.240278, 862.958271, 893.813659
, 883.678318, 923.593998, 915.52458, 877.919073, 891.754242, 919.274917
, 862.223914, 881.275387, 862.33147, 869.461632, 890.014577, 902.656117
, 874.446393, 876.284046, 866.751916, 854.095049, 844.540741, 870.263794
, 866.687327, 818.019291, 821.875267, 813.385138, 843.198211, 870.558259
, 794.039978, 813.497634, 812.217789, 801.361143, 800.263045, 747.101493
, 735.923635, 732.930255, 775.930026, 783.786631, 775.255742, 774.938671
, 704.186773, 747.612911, 729.315237, 694.021293]
我使用了这个代码:
def Gauss(x, a1, b1, c1, a2, b2, c2 ):
return a1*np.exp(-((x-b1)/c1)**2) + a2*np.exp(-((x-b2)/c2)**2)
parameters, covariance = curve_fit(Gauss, time_s, Temperature)
plt.plot(time_s, Gauss(time_s, *parameters))
plt.show()
这是此拟合的结果:
非常不合身
错误信息:
/home/lhoussine/anaconda3/lib/python3.8/site-packages/scipy/optimize/minpack.py:828: OptimizeWarning: Covariance of the parameters could not be estimated
warnings.warn('Covariance of the parameters could not be estimated',
首先,您不是在拟合高斯函数,而是在拟合几个函数的总和。我不确定为什么,但这肯定没有帮助,也不符合您的问题。
但即使我们拟合单个高斯分布,主要问题是 curve_fit
的初始猜测。除非您另有说明,否则 the default for all parameters is 1
。考虑到您的时间戳似乎符合 1e9 的顺序,这在您的案例中是一个非常糟糕的初始猜测。
如果您为初始猜测提供粗略估计,curve_fit
实际上能够做得相当不错:
def gaussian(x, amplitude, mean, sd):
return amplitude * np.exp(-(x - mean) ** 2.0 / sd ** 2.0)
parameters, covariance = curve_fit(gaussian, time_s, Temperature, p0=(900, np.mean(time_s), 1000))
plt.plot(time_s, Temperature)
plt.plot(time_s, gaussian(time_s, *parameters))
这给了我们: