Python Data Science 简明教程

Python - Normal Distribution

正态分布是一种通过排列数据中各个值的概率分布来呈现数据的方式。大多数值都围绕平均值,使排列呈现对称性。

我们使用 numpy 库中的各种函数对正态分布的值进行数学计算。创建直方图并绘制概率分布曲线。

import matplotlib.pyplot as plt
import numpy as np

mu, sigma = 0.5, 0.1
s = np.random.normal(mu, sigma, 1000)

# Create the bins and histogram
count, bins, ignored = plt.hist(s, 20, normed=True)

# Plot the distribution curve
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) *
    np.exp( - (bins - mu)**2 / (2 * sigma**2) ),       linewidth=3, color='y')
plt.show()

它的 output 如下所示 −

normdist