0

需要生成一个二项分布,测试 10 次,给定试验次数 (n) 和每次试验的概率 (p)。

输出应包含一个 numpy 数组,其中 10 个数字表示所需的二项式分布。

样本输入:0 10 0.5

预期输出:

[5 6 5 5 5 6 5 7 8 5]

我的代码:

import numpy as np
import pandas as pd
pd.set_option('display.max_columns', 500)
np.random.seed(0)
seed=int(input())
n=int(input())
p=float(input())
i = 1
while i <= n:
    x = np.random.binomial(n, p)
    s=np.array(x)
    print(s)
    i += 1

代码输出:

5 6 5 5 5 6 5 7 8 5

输出没有按预期输出我在这里做错了什么?

4

4 回答 4

2

试试这个,它会生成 10 个没有循环的二项式 RV:

import numpy as np
n = 8
p = 0.1
np.random.seed(0)
s = np.random.binomial(n, p, 10)
print(s)
# array([0, 1, 2, 0, 2, 0, 2, 0, 3, 0])
于 2020-09-08T16:44:19.223 回答
1

保留原始输入值,请找到以下代码:

import numpy as np
import pandas as pd
pd.set_option('display.max_columns', 500)
np.random.seed(0)
seed=int(input())
n=int(input())
p=float(input())
# Above code has been taken from the original requester as using the above input 
# output will be generated
npr = 10
np.random.seed(seed) 
s = np.random.binomial(n, p, npr)
print(s)
于 2021-09-04T17:34:40.403 回答
0
import numpy as np 

seed=int(input())
n=int(input())
p=float(input())

np.random.seed(seed)
s =np.random.binomial(n, p, 10)
print(s)
于 2021-08-04T07:01:41.417 回答
0

尝试这个,

np.random.seed(0)
s=np.random.binomial(n,p,10)
print(s)
于 2021-01-22T10:09:36.080 回答