numpy.random.randint() / choice() / binomial()

来源:udacity

# import numpy
import numpy as np

1. Two fair coin flips produce exactly two heads

# simulate 1 million tests of two fair coin flips
tests = np.random.randint(2, size=(int(1e5), 2))

# sums of all tests
test_sums = tests.sum(axis=1)

# proportion of tests that produced exactly two heads
(test_sums == 0).mean()

2. Three fair coin flips produce exactly one head

# simulate 1 million tests of three fair coin flips
tests = np.random.randint(2, size=(int(1e6), 3))

# sums of all tests
test_sums = tests.sum(axis = 1)

# proportion of tests that produced exnumpyactly one head
(test_sums == 2).mean()

3. Three biased coin flips with P(H) = 0.6 produce exactly one head

# simulate 1 million tests of three biased coin flips
# hint: use np.random.choice()
tests = np.random.choice([0, 1], size=(int(1e6),2), p=[0.6, 0.4])
                
# sums of all tests
test_sums = tests.sum(axis = 1)

# proportion of tests that produced exactly one head
(test_sums == 2).mean()

4. A die rolls an even number

# simulate 1 million tests of one die roll
tests = np.random.randint(1, 7, size=int(1e6))

# proportion of tests that produced an even number
(tests%2 == 0).mean()

5. Two dice roll a double

# simulate the first million die rolls
first = np.random.randint(1,7, size=1000000)

# simulate the second million die rolls
second = np.random.randint(1,7, size=int(1e6))

# proportion of tests where the 1st and 2nd die rolled the same number
(first == second).mean()

Simulating Many Coin Flips

# number of heads from 10 fair coin flips
np.random.binomial(10, 0.5)

output : 4

# results from 20 tests with 10 coin flips
np.random.binomial(10, 0.5, 20)

output : array([5, 7, 6, 4, 5, 6, 4, 4, 6, 5, 3, 6, 5, 4, 4, 4, 3, 6, 6, 5])

# reflects the fairness of the coin more closely as # tests increases
np.random.binomial(10, 0.5, 1000000).mean()

output : 5.0041070000000003

import matplotlib.pyplot as plt
% matplotlib inline
plt.hist(np.random.binomial(10, 0.5, 1000000));

图没有了~

Usenp.random.binomialto create simulations and compute proportions for the following outcomes.

1. A fair coin flip produces heads

# simulate 1 million tests of one fair coin flip
# remember, the output of these tests are the # successes, or # heads
tests = np.random.binomial(1, 0.5, int(1e6))

# proportion of tests that produced heads
(tests == 1).mean()

2. Five fair coin flips produce exactly one head

# simulate 1 million tests of five fair coin flips
tests = np.random.binomial(5, 0.5, int(1e6))

# proportion of tests that produced 1 head
(tests == 1).mean()

3. Ten fair coin flips produce exactly four heads

# simulate 1 million tests of ten fair coin flips
tests = np.random.binomial(10, 0.5, int(1e6))

# proportion of tests that produced 4 heads
(tests == 4).mean()

4. Five biased coin flips with P(H) = 0.8 produce exactly five heads

# simulate 1 million tests of five biased coin flips
tests = np.random.binomial(5, 0.8, int(1e6))

# proportion of tests that produced 5 heads
(tests == 5).mean()

5. Ten biased coin flips with P(H) = 0.15 produce at least 3 heads

# simulate 1 million tests of ten biased coin flips
tests = np.random.binomial(10, 0.15, int(1e6))

# proportion of tests that produced at least 3 heads
(tests >= 3).mean()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值