# Binomial Distribution in Python

## In this tutorial you will learn:

• What is a Binomial Distribution?
• Binomial Distribution Implementation in python
• Visualization of Binomial Distribution

## Binomial Distribution

If the experiment can only have two outcomes in a certain situation with certain conditions and limitations, and we perform it multiple times, then the results obtained will produce a binomial distribution. For example, if we toss a coin, there can be only two possibilities, either the result will be head or it will be tail. Two variables determine the results of a binomial distribution, first variable is “n” and it stands for the number of experiments/ repetitions we carry out, while the second variable is “p” which represents the probability of one specific outcome. Binomial distribution must meet three conditions.
1. The number of experiments are fixed
2. Each trial or observation is independent of the other trials
3. Probability of each outcome is same

## Binomial Distribution in Python

Binomial distribution in python is implemented using binomial() function. The binomial() function is part of random module. It takes in the “size” of the distribution which we want as an output as a first parameter. It takes “n” as a second parameter, “n” is number of times experiment will be carried. It take “p” as a third parameter, the “p” determines the probability of each outcome. All three arguments/ parameters are mandatory for binomial() function.
In this example we will generate a 2D array of binomial distribution having size (3,5), in the first line of code we are importing random module from numpy library and in third line we are defining repetitions(n) as 3 times and probability of occurrence as 0.5. You can observe that this example depicts the example of coin being tossed 3 times.

`from numpy import random#here we are using binomial function to generate binomial distribution of size 3 x 5res = random.binomial(size=(3,5), n=3, p=0.5)print('2D Binomial Distribution as output from binomial() function:\n')#printing the resultprint(res)`

Lets take another example, in this example we will generate a 3D array of binomial distribution of the size(4,2,8) with 80 repetitions and probability of 0.2.
`from numpy import random#here we are using binomial function to generate binomial distribution of size 4 x 2 x 8res = random.binomial (size=(4,2,8), n=80, p=0.2)print('3D binomial Distribution as output from binomial() function:\n')#printing the resultprint(res)`

## Visualization of Binomial Distribution

We will visualize the Binomial Distribution using an example. We will visualize the array being generated by the binomial() function with output array size 2000, number of experiments are 10 while probability of each outcome is 0.1 . Here we will use the distplot() to plot the graph.

`#importing all the required modules and packagesfrom numpy import randomimport matplotlib.pyplot as mplimport seaborn as sb#here we are using binomial function to generate binomial distribution of size 100sb.distplot(random.binomial(size=1000, n = 10, p = 0.1), hist=True, kde=False)#plotting the graphmpl.show()`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.