bundles / numpy 2.4.4 / numpy / random / RandomState / normal
cython_function_or_method
numpy.random:RandomState.normal
Signature
def normal ( loc = 0.0 , scale = 1.0 , size = None ) Summary
Draw random samples from a normal (Gaussian) distribution.
Extended Summary
The probability density function of the normal distribution, first derived by De Moivre and 200 years later by both Gauss and Laplace independently [2], is often called the bell curve because of its characteristic shape (see the example below).
The normal distributions occurs often in nature. For example, it describes the commonly occurring distribution of samples influenced by a large number of tiny, random disturbances, each with its own unique distribution [2].
Parameters
loc: float or array_like of floatsMean ("centre") of the distribution.
scale: float or array_like of floatsStandard deviation (spread or "width") of the distribution. Must be non-negative.
size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn. If size isNone(default), a single value is returned iflocandscaleare both scalars. Otherwise,np.broadcast(loc, scale).sizesamples are drawn.
Returns
out: ndarray or scalarDrawn samples from the parameterized normal distribution.
Notes
The probability density for the Gaussian distribution is
where is the mean and the standard deviation. The square of the standard deviation, , is called the variance.
The function has its peak at the mean, and its "spread" increases with the standard deviation (the function reaches 0.607 times its maximum at and [2]). This implies that normal is more likely to return samples lying close to the mean, rather than those far away.
Examples
Draw samples from the distribution:mu, sigma = 0, 0.1 # mean and standard deviation s = np.random.normal(mu, sigma, 1000)✓
abs(mu - np.mean(s))
✗abs(sigma - np.std(s, ddof=1))
✗import matplotlib.pyplot as plt count, bins, ignored = plt.hist(s, 30, density=True)✓
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ), linewidth=2, color='r')✗
plt.show()
✓
np.random.normal(3, 2.5, size=(2, 4))
✗See also
- random.Generator.normal
which should be used for new code.
- scipy.stats.norm
probability density function, distribution or cumulative density function, etc.
Aliases
-
numpy.random.normal -
numpy.random.RandomState.normal