简体   繁体   English

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

[英]Fitting a Normal distribution to 1D data

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))

You can use matplotlib to plot the histogram and the PDF (as in the link in @MrE's answer).您可以使用matplotlib绘制直方图和 PDF(如@MrE 回答中的链接所示)。 For fitting and for computing the PDF, you can use scipy.stats.norm , as follows.为了拟合和计算 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:这是脚本生成的图:

情节

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, fit=norm, kde=False)替换sns.distplot(data) sns.distplot(data, fit=norm, kde=False)应该可以解决问题。

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 https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.normal.html for exactly how to do this.请参阅https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.random.normal.html上的示例,了解如何执行此操作。

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:输出:

在此处输入图片说明

for more information : seaborn.distplot更多信息seaborn.distplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM