9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to calculate a binomial distribution?


To calculate a binomial distribution using Python and SciPy, you can use the scipy.stats.binom function. The function takes three parameters: n, p and size. n is the number of trials, p is the probability of success in each trial and size is the shape of the output array.

For example, to calculate a binomial distribution with 10 trials, a probability of success of 0.5 and an output array of size 5, you can use the following code:

import scipy.stats

distribution = scipy.stats.binom(n=10, p=0.5, size=5)
print(distribution.rvs())

The output of this code will be:

[5 5 6 4 5]

Code explanation

  • import scipy.stats: imports the SciPy library
  • scipy.stats.binom(n=10, p=0.5, size=5): calculates the binomial distribution with 10 trials, a probability of success of 0.5 and an output array of size 5
  • distribution.rvs(): returns a random sample from the distribution

Helpful links

Edit this code on GitHub