栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

7 概率

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7 概率

1.概率
import torch
from torch.distributions import multinomial #torch关于概率的库
from d2l import torch as d2l

fair_probs = torch.ones([6])/6
fair_probs
:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216 from C header, got 232 from PyObject





tensor([0.1667, 0.1667, 0.1667, 0.1667, 0.1667, 0.1667])
#创建一个多项式 相当于投掷10次色子,6个面出现的次数 ,总共做500次实验
counts = multinomial.Multinomial(10,fair_probs).sample((500,))
counts,counts.shape

(tensor([[3., 0., 3., 0., 1., 3.],
         [2., 4., 1., 0., 2., 1.],
         [0., 2., 2., 0., 2., 4.],
         ...,
         [2., 1., 3., 2., 0., 2.],
         [3., 0., 1., 4., 2., 0.],
         [0., 2., 3., 1., 2., 2.]]),
 torch.Size([500, 6]))
#按0轴累加
cum_counts = counts.cumsum(dim=0)
estimates = cum_counts/cum_counts.sum(dim=1,keepdims=True)#按1轴求和
estimates
tensor([[0.3000, 0.0000, 0.3000, 0.0000, 0.1000, 0.3000],
        [0.2500, 0.2000, 0.2000, 0.0000, 0.1500, 0.2000],
        [0.1667, 0.2000, 0.2000, 0.0000, 0.1667, 0.2667],
        ...,
        [0.1697, 0.1661, 0.1635, 0.1657, 0.1679, 0.1673],
        [0.1699, 0.1657, 0.1633, 0.1661, 0.1679, 0.1669],
        [0.1696, 0.1658, 0.1636, 0.1660, 0.1680, 0.1670]])
#色子递进过程可视化 逐渐逼近fair_probs
d2l.set_figsize((6,4.5))
for i in range(6):
    d2l.plt.plot(estimates[:,i].numpy(),label=("P(die=)"+str(i+1)+")"))
d2l.plt.axhline(y=0.167,color="black",linestyle="dashed")
d2l.plt.gca().set_xlabel("Groups of experiments")
d2l.plt.gca().set_ylabel("Extimated probability")
d2l.plt.legend()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918602.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号