# Implement Bernoulli Distribution Using Python NumPy

- Bernoulli Distribution
- Binomial Distribution
- Implement Bernoulli Distribution With the Help of NumPy in Python

As a really important topic in Statistics, the Bernoulli distribution is an essential aspect of Data Science, Machine Learning, and other related fields because it helps deal with data. This tutorial will demonstrate the Bernoulli distribution and how it can be implemented in Python with the help of the NumPy library.

When we talk about Bernoulli distribution, the term Binomial Distribution always comes to mind. These terms are alike but differ in some of their workings.

These two distributions come in handy for making problems easier at different times. It is important to understand and effectively differentiate between these terms, as described in the article below.

## Bernoulli Distribution

In simple terms, when there are only two possible outcomes in a single trial, this given data is appropriate for implementing Bernoulli Distribution.

The direct implementation of Bernoulli distribution can be done with the help of the SciPy library. However, the NumPy library can also be indirectly utilized to implement this distribution.

To explain it more clearly, let us assign Bernoulli Distribution as `D(b)`

.

## Binomial Distribution

The concept of Binomial Distribution can be defined as something that deals with a group or a set of Bernoulli Trials. The number of this group or set can usually be defined.

In simpler words, Binomial Distribution deals with multiple trials of an individual event, compared to Bernoulli Distribution which deals with a single trial of an individual event.

With respect to the above-taken assignment of Bernoulli Distribution as `D(b)`

, the binomial distribution would be assigned as `D(n,b)`

. This assignment reflects on the relationship between these terms.

## Implement Bernoulli Distribution With the Help of NumPy in Python

You must be wondering what is the significance of Binomial Distribution when talking about Bernoulli Distribution. Well, there is a great significance.

The NumPy library directly uses the function `NumPy.random.binomial()`

to implement the Binomial Distribution. The syntax of the `NumPy.random.binomial()`

function can be seen below.

```
random.binomial(n, p, size=None)
```

The parameters of this function are defined for a clearer understanding of the user.

`n`

: This is the number of trials. It can be inputted as float or integer but is eventually truncated into an int value.`p`

: This is the probability of success; it will always be more than`0`

or less than`1`

. It is a float value.`size`

: This helps provide the shape of the output. If set to`None`

, it only provides a single value.

When we give the value of the number of sets of Bernoulli trials to be `1`

in this case, we can indirectly implement the Bernoulli Distribution inside the Binomial Distribution.

The following code uses the `NumPy.random.binomial`

function to implement Bernoulli Distribution in Python.

We take an example of a coin (having only two possibilities, heads and tails) being thrown 4 times. When we take `n`

as `1`

, it qualifies as Bernoulli Distribution rather than Binomial Distribution, which is how we will proceed in the code.

```
import numpy as dragon
n = 1
p = 0.5
x = dragon.random.binomial(n, p, size=4)
print(x)
```

The above code provides the following output:

```
[1 0 0 1]
```

Here, we showed the result of flipping a single coin tested 4 times. It is important to note that as it is a random trial, rerunning the program would probably generate a different set of outputs.

**Vaibhhav Khetarpal**

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn