Python 将正态分布拟合到一维数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20011122/
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-18 19:15:56  来源:igfitidea点击:

Fitting a Normal distribution to 1D data

pythonnumpymatplotlibscipy

提问by Adel

I have a 1 dimensional array. I can compute the "mean" and "standard deviation" of this sample and plot the "Normal distribution" but I have a problem:

我有一个一维数组。我可以计算这个样本的“平均值”和“标准偏差”并绘制“正态分布”,但我有一个问题:

I want to plot the data and Normal distribution in the same figure.

我想在同一个图中绘制数据和正态分布。

I dont know how to plot both the data and the normal distribution.

我不知道如何绘制数据和正态分布。

Any Idea about "Gaussian probability density function in scipy.stats"?

关于“scipy.stats 中的高斯概率密度函数”的任何想法?

s = np.std(array)
m = np.mean(array)
plt.plot(norm.pdf(array,m,s))

采纳答案by Warren Weckesser

You can use matplotlibto plot the histogram and the PDF (as in the link in @MrE's answer). For fitting and for computing the PDF, you can use scipy.stats.norm, as follows.

您可以使用matplotlib绘制直方图和 PDF(如@MrE 回答中的链接)。为了拟合和计算 PDF,您可以使用scipy.stats.norm,如下所示。

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


# Generate some data for this demonstration.
data = norm.rvs(10.0, 2.5, size=500)

# Fit a normal distribution to the data:
mu, std = norm.fit(data)

# Plot the histogram.
plt.hist(data, bins=25, density=True, alpha=0.6, color='g')

# Plot the PDF.
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = norm.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title(title)

plt.show()

Here's the plot generated by the script:

这是脚本生成的图:

Plot

阴谋

回答by YXD

To see both the normal distribution and your actual data you should plot your data as a histogram, then draw the probability density function over this. See the example on http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.htmlfor exactly how to do this.

要查看正态分布和实际数据,您应该将数据绘制为直方图,然后在其上绘制概率密度函数。请参阅http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html上的示例,了解如何执行此操作。

回答by LonelyDaoist

There is a much simpler way to do it using seaborn:

使用seaborn有一种更简单的方法:

import seaborn as sns
from scipy.stats import norm

data = norm.rvs(5,0.4,size=1000) # you can use a pandas series or a list if you want

sns.distplot(data)
plt.show()

output:

输出:

enter image description here

在此处输入图片说明

for more information:seaborn.distplot

更多信息seaborn.distplot

回答by Jérome Chevalier

Here you are not fitting a normal distribution. Replacing sns.distplot(data)by sns.distplot(data, fit=norm, kde=False)should do the trick.

在这里,您没有拟合正态分布。更换sns.distplot(data)sns.distplot(data, fit=norm, kde=False)应该做的伎俩。