使用 numpy、python 和 pandas.Sample 输入的二项分布:0 10 0.5。示例输出:[5 6 5 5 5 6 5 7 8 5]

Binomial Distribution with numpy, python and pandas.Sample Input: 0 10 0.5. Sample output: [5 6 5 5 5 6 5 7 8 5]

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

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

也许我遗漏了所有细节,但自然的选择是 Scipy 的二项分布特征。可以看scipy.stats.binom的文档。参考文档,

def sample_binomial_size(size, n, p):
    """
    :param size: number of samples to produce
    :param n: number of available values
    :param p: probability shape factor
    """
    from scipy.stats import binom
    return binom.rvs(n, p, size=size)