python正态分布

import pandas as pd # 导入另一个包“pandas” 命名为 pd,理解成pandas是在 numpy 基础上的升级包
import numpy as np #导入一个数据分析用的包“numpy” 命名为 np
import matplotlib.pyplot as plt # 导入 matplotlib 命名为 plt,类似 matlab,集成了许多可视化命令


#正态分布的概率密度函数。可以理解成 x 是 mu(均值)和 sigma(标准差)的函数
def normfun(x,mu,sigma):
    pdf = np.exp(-((x - mu)**2)/(2*sigma**2)) / (sigma * np.sqrt(2*np.pi))
    return pdf


result = np.random.normal(60, 1, 100)  # 均值为0.5,方差为1
x = np.arange(142,157,0.1)
#设定 y 轴,载入刚才的正态分布函数
y = normfun(x, result.mean(), result.std())
plt.plot(x,y)
#画出直方图,最后的“normed”参数,是赋范的意思,数学概念
plt.hist(result, bins=10, rwidth=0.9, density=True)
plt.title('Time distribution')
plt.xlabel('Time')
plt.ylabel('Probability')
#输出
plt.show()

猜你喜欢

转载自www.cnblogs.com/judes/p/12627177.html