NumPy Exponential Distribution (Python Tutorial)

Exponential Distribution

This is a detailed tutorial of NumPy Exponential Distribution. Learn to implement Exponential Distribution using NumPy and visualize using Seaborn.

Exponential Distribution

In the theory of probability and statistics, this is the distribution of time between the events which will occur in the future. In this process, the events will continuously and independently. As a result, it will always have a constant average rate.

Similarly, it helps in predicting the success and failure of an event. It takes in two parameters as input which are:

  • scale –  It is the inverse of the rate which is by default set to 1.0
  • size – this will help us in determining the shape of the array.

Let us go through an example in order to understand properly:

# here first we will import the numpy package with random module
from numpy import random
# we will use method
x=random.exponential(size=(3,6))
#now we will print the data
print(x)

Output.

[[ 1.19227214  0.33706748  0.25728408  0.80143368  1.27107094  0.55009754]
 [ 3.95098207  1.15464227  0.65572019  0.4977737   0.42034225  1.3058199 ]
 [ 0.57727218  0.0615328   0.50757135  1.33939324  0.20149426  0.19291963]]

Here we are taking only the size of the array. Let us take another example where we would pass all the parameters of the exponential distribution.

# here first we will import the numpy package with random module
from numpy import random
# we will use method
x=random.exponential( scale=2,size=(3,6))
#now we will print the data
print(x)

Output.

[[ 2.38454429  0.67413497  0.51456816  1.60286736  2.54214187  1.10019507]
 [ 7.90196414  2.30928455  1.31144039  0.9955474   0.8406845   2.61163979]
 [ 1.15454435  0.1230656   1.0151427   2.67878648  0.40298852  0.38583925]]

Here we have given the scale value as well which is equal to 2.0 and the result is according to the shape of the array.

Visualization of this Distribution

When we want to visualize the data we can work with size parameter alone as scale will be set to 1.0 by default.

Let us visualize the exponential distribution:

# here first we will import the numpy package with random module
from numpy import random
#here we ill import matplotlib 
import matplotlib.pyplot as plt 
#now we will import seaborn 
import seaborn as sns 
#we will plot a displot here 
sns.distplot(random.exponential(size=500), hist=False) 
# now we have the plot printed 
plt.show()

Output.

Exponential Distribution

Let us visualize another exponential distribution:

# here first we will import the numpy package with random module
from numpy import random
#here we ill import matplotlib 
import matplotlib.pyplot as plt 
#now we will import seaborn 
import seaborn as sns 
#we will plot a displot here 
sns.distplot(random.exponential(size=1000), hist=False) 
# now we have the plot printed 
plt.show()

Output.

Exponential Distribution Example 2

Difference between Poisson and Exponential Distribution

As we know that Poisson distribution is related to the number of events occurring in a particular time period whereas Exponential Distribution deals with the time within which an event will take place. so, as a result, we get to know the difference between two and how they are related to each other.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *