【问题标题】:Random experiment of Binomial distribution using stats in python在 python 中使用 stats 进行二项分布的随机实验
【发布时间】:2019-08-02 10:46:08
【问题描述】:

模拟掷硬币 10000 次的随机实验,并确定正面的数量。
提示:用n = 1p = 0.5 定义二项分布。
使用 scipy.stats 中的 binom 函数。
将随机种子设置为1
从定义的分布中抽取10000 元素的样本。
假设值 01 分别代表 Heads 和 Tails。
计算正面的数量并显示出来。使用bincount 方法,在numpy 中可用。

我找到了答案,但不是来自scipy.stats 包,而是来自随机包。以下是我的尝试,但答案并不如预期。请帮助我纠正我的错误。

import scipy as sp
from scipy import stats
import numpy as np
import random

from scipy.stats import binom
data_binom = binom.rvs(n=1,p=0.5,size=10000)

np.random.seed(1)

#print(data_binom)

y = np.bincount(data_binom)
head = print(y[0])
print(head)

【问题讨论】:

  • head = print(y[0]) - 是你的错误。它将 head 分配给 None。你需要类似最后两行的东西:heads = y[0];尾巴 = y[1];打印(头);打印(尾巴)
  • 嗨,亚历山大。谢谢,我试过这个,但它不是预期的。如果您有任何其他歧义,请告诉我。
  • 你期待什么?
  • 我添加了代码示例并删除了“随机”以消除歧义。

标签: python numpy statistics data-science


【解决方案1】:

似乎问题在于您设置种子的位置。目前您正在发布您的样本选择,理想情况下应该在如下所示之前完成:

import scipy as sp
from scipy import stats
import numpy as np
from scipy.stats import binom

np.random.seed(1)

data_binom = binom.rvs(n=1,p=0.5,size=10000)
#print(data_binom)

y = np.bincount(data_binom)
head = print(y[0])
print(head)

猜猜这就是您的预期输出。干杯!!

【讨论】:

    【解决方案2】:
    from scipy.stats import binom
    import numpy as np
    b = binom(n=1,p=0.5)
    np.random.seed(1)
    sample = b.rvs(size=10000)
    print(np.count_nonzero(sample==0)) # heads is assumed to be zero
    

    此代码将在您的环境中运行。相信我!

    【讨论】:

      【解决方案3】:

      我得到了我所期望的。不知道哪个是头:4995还是5005?

      print(y[0])
      print(y[1])
      
      4995
      5005
      

      这里有更多代码来解释你的折腾:

      from scipy.stats import binom
      data_binom = binom.rvs(n=1,p=0.5,size=10000)
      
      heads = 0
      tails = 0
      edges = 0
      count = 0
      
      for coin in data_binom:
          count += 1
          if coin == 1:
              heads += 1
          elif coin == 0:
              tails += 1
          else:
              edges += 1
      
      print("Observed " + str(count) + " of coin tossing with heads " + str(heads)
            + ", tails " + str(tails) + ", and edges " + str(edges))
      

      四次测试结果:

      $ python3.7 test.py
      Observed 10000 of coin tossing with heads 4989, tails 5011, and edges 0
      $ python3.7 test.py
      Observed 10000 of coin tossing with heads 5109, tails 4891, and edges 0
      $ python3.7 test.py 
      Observed 10000 of coin tossing with heads 4968, tails 5032, and edges 0
      $ python3.7 test.py 
      Observed 10000 of coin tossing with heads 5046, tails 4954, and edges 0
      

      【讨论】:

        【解决方案4】:

        设置 binom.rvs size=1,然后将正面的概率设置为 0.5

        from scipy.stats import binom
        flips=binom.rvs(1000,0.5,size=1)
        print(flips)
        plt.pie([flips,1000])
        plt.legend(['heads','tails'])
        plt.show()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-27
          相关资源
          最近更新 更多