【发布时间】:2020-02-26 03:07:56
【问题描述】:
是否有适合高斯的方法,我不只是提供平均值的建议或最佳猜测,但它必须接受它并调整其他参数才能使其正常工作?我知道这不会为我提供最适合数据的方法,但这不是必需的。
【问题讨论】:
-
是的,只是不要参数化解决方案中的平均值(仅标准)。您有什么特别想采取的方法吗?
标签: python curve-fitting gaussian
是否有适合高斯的方法,我不只是提供平均值的建议或最佳猜测,但它必须接受它并调整其他参数才能使其正常工作?我知道这不会为我提供最适合数据的方法,但这不是必需的。
【问题讨论】:
标签: python curve-fitting gaussian
@BenedictWilkinsAI 建议是最简单的方法,用固定值代替平均值编写方程。但是,如果您想使用编程解决方案,这里有一个图形 Python 拟合器,它允许正常(双关语)和固定均值高斯峰值方程拟合。 当使用 9.0 的固定平均参数值时,拟合明显更差 - 正如预期的那样。此外,curve_fit() 会发出警告,指出它无法计算协方差矩阵,因为均值参数不能变化。
import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
xData = numpy.array([5.357, 5.797, 5.936, 6.161, 6.697, 6.731, 6.775, 8.442, 9.861])
yData = numpy.array([0.376, 0.874, 1.049, 1.327, 2.054, 2.077, 2.138, 4.744, 7.104])
# normally fitted mean is 10.67571675
# set this value to None to fit normally, else
# set to the value of the fixed mean
fixedMean = 9.0
def func(x, a, b, c): # Gaussian peak equation
if fixedMean:
b = fixedMean
return a * numpy.exp(-0.5 * numpy.power((x-b) / c, 2.0))
# these are the same as the scipy defaults except for the fixed mean
if fixedMean:
initialParameters = numpy.array([1.0, fixedMean, 1.0])
else:
initialParameters = numpy.array([1.0, 1.0, 1.0])
# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, p0=initialParameters)
modelPredictions = func(xData, *fittedParameters)
absError = modelPredictions - yData
SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))
print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)
print()
##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(xData, yData, 'D')
# create data for the fitted equation plot
xModel = numpy.linspace(min(xData), max(xData))
yModel = func(xModel, *fittedParameters)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
plt.show()
plt.close('all') # clean up after using pyplot
graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
【讨论】:
您可以考虑为此使用lmfit(披露:我是主要作者),因为它支持修复或添加任何参数的界限。根据您的数据,进行这样的拟合可能很简单:
import numpy as np
import matplotlib.pyplot as plt
from lmfit.models import GaussianModel
# get x, y data from some source
data = np.loadtxt('somedatafile.dat')
xdata = data[:, 0]
ydata = data[:, 1]
# create Gaussian, set initial parameter values
model = GaussianModel()
parameters = model.make_params(amplitude=10, center=12, sigma=3)
# tell the center parameter to not vary in the fit
parameters['center'].vary = False
# run fit
result = model.fit(ydata, parameters, x=xdata)
# print fit statistics, parameter values and uncertainties
print(result.fit_report())
# make a simple plot of data + fit, with residual
result.plot()
plt.show()
还有其他峰形和控制参数值和范围的选项。
【讨论】: