Exponential Distribution in Python

In this tutorial you will learn:

  • What is Exponential Distribution?
  • Exponential Distribution Implementation in python
  • Visualization of Exponential Distribution

Exponential Distribution

Exponential Distribution is most widely used continuous distribution. The exponential distribution is concerned with amount of time until a specific event has occurred. As an example, the time elapsed between two pandemics or the time spent after last engine oil change of a vehicle can be modeled using exponential distribution. It has fewer larger values and more number of smaller values.

Exponential Distribution in Python

Exponential distribution in python is implemented using an inbuilt function exponential() which is included in the random module of NumPy library. The exponential() function takes in two parameters. First parameter “size” is the mandatory parameter and it is size of the output array which could be 1D, 2D, 3D or n-dimensional (depending on the programmer’s requirements). The second parameter is inverse of rate defined by “scale”, it is an optional parameter and if it is not explicitly defined, its value is taken as 1.0. Lets take an example, we will generate a 1D array(1,5) of exponential distribution with scale 2.5. In the second line of code we are importing random module from NumPy library and in fourth line we are calling the exponential function and passing it the value of scale.
  1. #importing the random module
  2. from numpy import random
  3. #applying the exponential function
  4. res_arr= random. exponential(scale = 2.5, size=5)
  5. #printing the results
  6. print('1D array of size(1,5) having exponential distribution with scale 2.5:\n')
  7. print(res_arr)
In this example we will generate a 2D array of exponential distribution having size (6,2) with default scale value.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using exponential function to generate exponential distribution of size 6 x 2 with default scale value
  4. res_arr = random.exponential(size=(6,2))
  5. print('2D Exponential Distribution as output from exponential() function:\n')
  6. #printing the result
  7. print(res_arr)
Lets take another example, in this example we will generate a 3D array of exponential distribution of the size(3,3,5) with scale 0.8
  1. #importing the random module
  2. from numpy import random
  3. #here we are using exponential function to generate exponential distribution of size 3 x 3 x 5
  4. res = random.exponential(size=(3,3,5), scale=0.8)
  5. print('3D Exponential Distribution as output from exponential() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Exponential Distribution

In this example we will visualize the Exponential Distribution with scale of 1.5.
  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using exponential function to generate distributions of size 1000 with scale 1.5
  6. sb.distplot(random.exponential(size=1000,scale=1.5), hist=False, label='Exp Distr')
  7. #plotting the graph
  8. mpl.show()

Add new comment