从np random normal 到正态分布的拟合

                       

先看伟大的高斯分布(Gaussian Distribution)的概率密度函数(probability density function):

f(x)=12π − −  √ σ exp((xμ) 2 2σ 2  ) f(x)=12πσexp⁡(−(x−μ)22σ2)
),对应于 np.random.normal(loc=0, scale=1, size)

采样(sampling)

# 从某一分布(由均值和标准差标识)中获得样本mu, sigma = 0, .1s = np.random.normal(loc=mu, scale=sigma, size=1000)
  
  
  • 1
  • 2
  • 3

也可使用scipy库中的相关api(这里的类与函数更符合数理统计中的直觉):

import scipy.stats as stmu, sigma = 0, .1s = st.norm(mu, sigma).rvs(1000)
  
  
  • 1
  • 2
  • 3

校验均值和方差:

>>> abs(mu < np.mean(s)) < .01True>>> abs(sigma-np.std(s, ddof=1)) < .01True            # ddof,delta degrees of freedom,表示自由度            # 一般取1,表示无偏估计,      
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

拟合

我们看使用matplotlib.pyplot便捷而强大的语法如何进行高斯分布的拟合:

import matplotlib.pyplot as pltcount, bins, _ = plt.hist(s, 30, normed=True)        # normed是进行拟合的关键        # count统计某一bin出现的次数,在Normed为True时,可能其值会略有不同plt.plot(bins, 1./(np.sqrt(2*np.pi)*sigma)*np.exp(-(bins-mu)**2/(2*sigma**2), lw=2, c='r')plt.show()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

或者:

s_fit = np.linspace(s.min(), s.max())plt.plot(s_fit, st.norm(mu, sigma).pdf(s_fit), lw=2, c='r')
  
  
  • 1
  • 2


这里写图片描述

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值