如何在python中拟合高斯曲线?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44480137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:02:51  来源:igfitidea点击:

How can I fit a gaussian curve in python?

pythonscipycurve-fittinggaussian

提问by P. Kaur

I'm given an array and when I plot it I get a gaussian shape with some noise. I want to fit the gaussian. This is what I already have but when I plot this I do not get a fitted gaussian, instead I just get a straight line. I've tried this many different ways and I just can't figure it out.

我得到一个数组,当我绘制它时,我得到一个带有一些噪音的高斯形状。我想拟合高斯。这是我已经拥有的,但是当我绘制此图时,我没有得到拟合的高斯分布,而是得到一条直线。我已经尝试了很多不同的方法,但我无法弄清楚。

random_sample=norm.rvs(h)

parameters = norm.fit(h)

fitted_pdf = norm.pdf(f, loc = parameters[0], scale = parameters[1])

normal_pdf = norm.pdf(f)

plt.plot(f,fitted_pdf,"green")
plt.plot(f, normal_pdf, "red")
plt.plot(f,h)
plt.show()

click for image

点击图片

回答by Miriam Farber

You can use fitfrom scipy.stats.normas follows:

您可以fitscipy.stats.norm如下使用:

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

data = np.random.normal(loc=5.0, scale=2.0, size=1000)
mean,std=norm.fit(data)

norm.fittries to fit the parameters of a normal distribution based on the data. And indeed in the example above meanis approximately 2 and stdis approximately 5.

norm.fit尝试根据数据拟合正态分布的参数。事实上,在上面的例子mean中大约是 2,std大约是 5。

In order to plot it, you can do:

为了绘制它,您可以执行以下操作:

plt.hist(data, bins=30, normed=True)
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
y = norm.pdf(x, mean, std)
plt.plot(x, y)
plt.show()

enter image description here

在此处输入图片说明

The blue boxes are the histogram of your data, and the green line is the Gaussian with the fitted parameters.

蓝色框是数据的直方图,绿线是具有拟合参数的高斯图。

回答by MSeifert

There are many ways to fit a gaussian function to a data set. I often use astropy when fitting data, that's why I wanted to add this as additional answer.

有很多方法可以将高斯函数拟合到数据集。我在拟合数据时经常使用 astropy,这就是我想将其添加为附加答案的原因。

I use some data set that should simulate a gaussian with some noise:

我使用了一些应该模拟带有一些噪声的高斯的数据集:

import numpy as np
from astropy import modeling

m = modeling.models.Gaussian1D(amplitude=10, mean=30, stddev=5)
x = np.linspace(0, 100, 2000)
data = m(x)
data = data + np.sqrt(data) * np.random.random(x.size) - 0.5
data -= data.min()
plt.plot(x, data)

enter image description here

在此处输入图片说明

Then fitting it is actually quite simple, you specify a model that you want to fit to the data and a fitter:

然后拟合它实际上非常简单,您指定一个要拟合数据的模型和一个拟合器:

fitter = modeling.fitting.LevMarLSQFitter()
model = modeling.models.Gaussian1D()   # depending on the data you need to give some initial values
fitted_model = fitter(model, x, data)

And plotted:

并绘制:

plt.plot(x, data)
plt.plot(x, fitted_model(x))

enter image description here

在此处输入图片说明



However you can also use just Scipy but you have to define the function yourself:

但是,您也可以只使用 Scipy,但您必须自己定义函数:

from scipy import optimize

def gaussian(x, amplitude, mean, stddev):
    return amplitude * np.exp(-((x - mean) / 4 / stddev)**2)

popt, _ = optimize.curve_fit(gaussian, x, data)

This returns the optimal arguments for the fit and you can plot it like this:

这将返回拟合的最佳参数,您可以像这样绘制它:

plt.plot(x, data)
plt.plot(x, gaussian(x, *popt))

enter image description here

在此处输入图片说明