蝴蝶不菲
好吧,您需要编写一个函数来计算参数化的超高斯函数,并使用它来对数据进行建模,例如.作为LMFIT(https://lmfit.github.io/lmfit-py/)的主要作者,它提供了一个高级接口来拟合和曲线拟合,我建议尝试该库。使用这种方法,超高斯和用于拟合数据的模型函数可能如下所示:scipy.optimize.curve_fitimport numpy as np from lmfit import Model def super_gaussian(x, amplitude=1.0, center=0.0, sigma=1.0, expon=2.0): """super-Gaussian distribution super_gaussian(x, amplitude, center, sigma, expon) = (amplitude/(sqrt(2*pi)*sigma)) * exp(-abs(x-center)**expon / (2*sigma**expon)) """ sigma = max(1.e-15, sigma) return ((amplitude/(np.sqrt(2*np.pi)*sigma)) * np.exp(-abs(x-center)**expon / 2*sigma**expon))# generate some test datax = np.linspace(0, 10, 101)y = super_gaussian(x, amplitude=7.1, center=4.5, sigma=2.5, expon=1.5)y += np.random.normal(size=len(x), scale=0.015)# make Model from the super_gaussian functionmodel = Model(super_gaussian)# build a set of Parameters to be adjusted in fit, named from the arguments # of the model function (super_gaussian), and providing initial valuesparams = model.make_params(amplitude=1, center=5, sigma=2., expon=2)# you can place min/max bounds on parametersparams['amplitude'].min = 0params['sigma'].min = 0params['expon'].min = 0params['expon'].max = 100# note: if you wanted to make this strictly Gaussian, you could set # expon=2 and prevent it from varying in the fit:### params['expon'].value = 2.0### params['expon'].vary = False# now do the fitresult = model.fit(y, params, x=x)# print out the fit statistics, best-fit parameter values and uncertaintiesprint(result.fit_report())# plot resultsimport matplotlib.pyplot as pltplt.plot(x, y, label='data')plt.plot(x, result.best_fit, label='fit')plt.legend()plt.show()这将打印一个报告,如[[Model]] Model(super_gaussian)[[Fit Statistics]] # fitting method = leastsq # function evals = 53 # data points = 101 # variables = 4 chi-square = 0.02110713 reduced chi-square = 2.1760e-04 Akaike info crit = -847.799755 Bayesian info crit = -837.339273[[Variables]] amplitude: 6.96892162 +/- 0.09939812 (1.43%) (init = 1) center: 4.50181661 +/- 0.00217719 (0.05%) (init = 5) sigma: 2.48339218 +/- 0.02134446 (0.86%) (init = 2) expon: 3.25148164 +/- 0.08379431 (2.58%) (init = 2)[[Correlations]] (unreported correlations are < 0.100) C(amplitude, sigma) = 0.939 C(sigma, expon) = -0.774 C(amplitude, expon) = -0.745并生成这样的情节