![]() |
VOOZH | about |
Laplace distribution (also called double exponential distribution) models data with a sharp peak at the mean and heavier tails than a normal distribution. In Python, numpy.random.laplace() is used to generate random samples from Laplace distribution, defined by two parameters:
Here, we generate a single random value from a Laplace distribution with mean = 0 and scale = 1.
2.093642767866976
Explanation: Here, a single random value is generated from a Laplace distribution centered at 0 with spread 1. Each execution will produce a different random number.
numpy.random.laplace(loc=0.0, scale=1.0, size=None)
Parameters:
Return Value: out (ndarray or float) Random samples from a Laplace distribution.
Example 1: Generate 1000 random values with mean = 1.45 and scale = 15, then visualize them with a histogram.
Output
Explanation: histogram shows data centered around 1.45 with wide spread due to the high scale value of 15.
Example 2: Generate 1000 random values from a Laplace distribution, then use those values as input to generate another Laplace-distributed sample.
Output
Explanation: second sample (arr2) is generated by using the first Laplace sample (arr1) as the mean values, resulting in a more spread-out distribution.
Example 3: Generate a 2D NumPy array (3×3) of Laplace-distributed random values.
2D Array: [[-1.32414275 0.90883889 -0.41223986] [ 2.49728308 1.33401096 -9.83063736] [ 0.3321555 1.71840122 1.0742276 ]]
Explanation: Here, a 3×3 matrix is generated with values sampled from a Laplace distribution centered at 0 with scale 2.