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 libraryscipy.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 5distribution.rvs()
: returns a random sample from the distribution
Helpful links
More of Python Scipy
- How do I use Python Numpy to create a tutorial?
- How do I calculate a Jacobian matrix using Python and NumPy?
- How do I create a 2D array of zeros using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I create a numpy array of zeros using Python?
- How can I use Python and SciPy to find the zeros of a function?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I create a zero matrix using Python and Numpy?
See more codes...