📜  np.random.exponential - Python (1)

📅  最后修改于: 2023-12-03 15:17:59.303000             🧑  作者: Mango

np.random.exponential - Python

np.random.exponential() is a NumPy function that returns samples from an exponential distribution. The exponential distribution is a probability distribution that describes the time between events in a Poisson point process, i.e., a process where events occur continuously and independently at a constant average rate.

Syntax
numpy.random.exponential(scale=1.0, size=None)
Parameters
  • scale: the scale parameter, which is the inverse of the rate parameter (default is 1.0)
  • size: the shape of the output array (default is None)
Return Value

An array of random samples from the exponential distribution with the specified shape.

Examples
Example 1: Generate a Random Sample from Exponential Distribution
import numpy as np

scale = 0.5  # inverse of the rate parameter

sample = np.random.exponential(scale=scale)
print(sample)

Output:

1.0585248086954751

This means that the time between events in a Poisson process with a rate of 2 per unit time is 1.06 units of time on average.

Example 2: Generate a Random Sample from Exponential Distribution with a Specific Size
import numpy as np

scale = 0.5  # inverse of the rate parameter
size = (3, 4)  # shape of the output array

samples = np.random.exponential(scale=scale, size=size)
print(samples)

Output:

[[0.89686142 0.727328   0.6058988  0.408746  ]
 [1.28496671 1.08679299 0.19109097 0.25695103]
 [0.23624417 0.71487949 0.96480785 0.15792563]]

This means that the time between events in a Poisson process with a rate of 2 per unit time is 0.89, 0.73, 0.61, 0.41 units of time on average for the first row; 1.28, 1.09, 0.19, 0.26 units of time on average for the second row; and 0.24, 0.71, 0.96, 0.16 units of time on average for the third row.

Conclusion

np.random.exponential() is a useful NumPy function for generating random samples from an exponential distribution, which is a probability distribution that describes the time between events in a Poisson point process.