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 create a 2D array of zeros using Python and NumPy?
- How do I use the Python Scipy package?
- How do I use scipy.optimize.curve_fit in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python SciPy to calculate the Hilbert transform?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Scipy zeros in Python?
- How can I use Python Scipy to zoom in on an image?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
See more codes...