python-scipyHow do I use the Python Scipy binom function?
The scipy.stats.binom
function is a part of the SciPy library and is used to calculate the probability mass function (PMF) or cumulative density function (CDF) of a binomial distribution. A binomial distribution is a statistical distribution used to model the probability of a given number of successes in a set number of independent trials.
Example code
from scipy.stats import binom
# Calculate the probability of getting exactly 3 heads in 10 coin flips
binom.pmf(3, 10, 0.5)
Output example
0.11718750000000006
The code above calculates the probability mass function (PMF) of a binomial distribution with 10 trials and a probability of success of 0.5. The probability of getting exactly 3 heads in 10 coin flips is 0.11718750000000006.
The scipy.stats.binom
function can also be used to calculate the cumulative density function (CDF) of a binomial distribution.
Example code
from scipy.stats import binom
# Calculate the probability of getting 3 or fewer heads in 10 coin flips
binom.cdf(3, 10, 0.5)
Output example
0.998046875
The code above calculates the cumulative density function (CDF) of a binomial distribution with 10 trials and a probability of success of 0.5. The probability of getting 3 or fewer heads in 10 coin flips is 0.998046875.
Code explanation
from scipy.stats import binom
: this imports thebinom
function from the SciPy library.binom.pmf(3, 10, 0.5)
: this calculates the probability mass function (PMF) of a binomial distribution with 10 trials and a probability of success of 0.5, and returns the probability of getting exactly 3 successes.binom.cdf(3, 10, 0.5)
: this calculates the cumulative density function (CDF) of a binomial distribution with 10 trials and a probability of success of 0.5, and returns the probability of getting 3 or fewer successes.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a numpy array of zeros using Python?
- How can I use Python Numpy to select elements from an array based on multiple conditions?
- How do I create a zero matrix using Python and Numpy?
- How can I use Python Scipy to zoom in on an image?
- How can I use Python and Numpy to parse XML data?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How do I use scipy.optimize.curve_fit in Python?
See more codes...