Search code examples
python-3.xpandasbinomial-theorem

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]


The output should contain a NumPy array with 10 numbers representing the required binomial distribution.

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

Solution

  • Perhaps I am missing all of the specifics, but the natural choice would be Scipy's binomial distribution features. You can see the documentation of scipy.stats.binom. Referencing the documentation,

    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)