The Exponential Distribution is a fundamental concept in probability and statistics. It describe the time between events in a Poisson process where events occur continuously and independently at a constant average rate. You can generate random numbers which follow exponential Distribution using numpy.random.exponential()
method.
Syntax :numpy.random.exponential(scale=1.0, size=None)
- scale : The inverse of the rate parameter (β=1/λ) which determines the spread of the distribution.
- size : The shape of the returned array.
Example 1: Generate a Single Random Number
To generate a single random number from a default Exponential Distribution (scale=1
):
Pythonimportnumpyasnprandom_number=np.random.exponential()print(random_number)
Output:
0.008319485004465102
To generate multiple random numbers:
Pythonrandom_numbers=np.random.exponential(size=5)print(random_numbers)
Output:
[1.15900802 0.1997201 0.73995988 0.19688073 0.54198053]
Visualizing the Exponential Distribution
Visualizing the generated numbers helps in understanding their behavior. Below is an example of plotting a histogram of random numbers generated using numpy.random.exponential
.
Pythonimportnumpyasnpimportmatplotlib.pyplotaspltimportseabornassnsscale=2size=1000data=np.random.exponential(scale=scale,size=size)sns.histplot(data,bins=30,kde=True,color='orange',edgecolor='black')plt.title(f"Exponential Distribution (Scale={scale})")plt.xlabel("Value")plt.ylabel("Frequency")plt.grid(True)plt.show()
Output:
Exponential DistributionThe above image shows an Exponential Distribution with a scale parameter of 2. The histogram represents simulated data while the orange curve depicts the theoretical distribution.