Sample Bernoulli random values with given shape and mean.
Parameters:
p (float, array_like, optional) – A float or array of floats for the mean of the random
variables. Must be broadcast-compatible with shape and the values
should be within [0, 1]. Default 0.5.
size (optional, tuple of int, int) – A tuple of nonnegative integers representing the result
shape. Must be broadcast-compatible with p.shape. The default (None)
produces a result shape equal to p.shape.
Returns:
out – A random array with boolean dtype and shape given by shape if shape
is not None, or else p.shape.
The Beta distribution is a special case of the Dirichlet distribution,
and is related to the Gamma distribution. It has the probability
distribution function
It is often seen in Bayesian inference and order statistics.
Parameters:
a (float or array_like of floats) – Alpha, positive (>0).
b (float or array_like of floats) – Beta, positive (>0).
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if a and b are both scalars.
Otherwise, np.broadcast(a,b).size samples are drawn.
Returns:
out – Drawn samples from the parameterized beta distribution.
Sample random values from categorical distributions.
Parameters:
logits – Unnormalized log probabilities of the categorical distribution(s) to sample from,
so that softmax(logits, axis) gives the corresponding probabilities.
axis (int) – Axis along which logits belong to the same categorical distribution.
shape – Optional, a tuple of nonnegative integers representing the result shape.
Must be broadcast-compatible with np.delete(logits.shape,axis).
The default (None) produces a result shape equal to np.delete(logits.shape,axis).
key – a PRNG key used as the random key.
Returns:
A random array with int dtype and shape given by shape if shape
is not None, or else np.delete(logits.shape,axis).
a (1-D array-like or int) – If an ndarray, a random sample is generated from its elements.
If an int, the random sample is generated as if it were np.arange(a)
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. Default is None, in which case a
single value is returned.
replace (boolean, optional) – Whether the sample is with or without replacement. Default is True,
meaning that a value of a can be selected multiple times.
p (1-D array-like, optional) – The probabilities associated with each entry in a.
If not given, the sample assumes a uniform distribution over all
entries in a.
Returns:
samples – The generated random samples
Return type:
single item or ndarray
Raises:
ValueError – If a is an int and less than zero, if a or p are not 1-dimensional,
if a is an array-like of size 0, if p is not a vector of
probabilities, if a and p have different lengths, or if
replace=False and the sample size is greater than the population
size
Setting user-specified probabilities through p uses a more general but less
efficient sampler than the default. The general sampler produces a different sample
than the optimized sampler even if each element of p is 1 / len(a).
Sampling random rows from a 2-D array is not possible with this function,
but is possible with Generator.choice through its axis keyword.
Examples
Generate a uniform random sample from np.arange(5) of size 3:
>>> importbrainpy.mathasbm>>> bm.random.choice(5,3)array([0, 3, 4]) # random>>> #This is equivalent to brainpy.math.random.randint(0,5,3)
Generate a non-uniform random sample from np.arange(5) of size 3:
>>> bm.random.choice(5,3,p=[0.1,0,0.3,0.6,0])array([3, 3, 0]) # random
Generate a uniform random sample from np.arange(5) of size 3 without
replacement:
>>> bm.random.choice(5,3,replace=False)array([3,1,0]) # random>>> #This is equivalent to brainpy.math.random.permutation(np.arange(5))[:3]
Generate a non-uniform random sample from np.arange(5) of size
3 without replacement:
>>> bm.random.choice(5,3,replace=False,p=[0.1,0,0.3,0.6,0])array([2, 3, 0]) # random
Any of the above can be repeated with an arbitrary array-like
instead of just integers. For instance:
>>> aa_milne_arr=['pooh','rabbit','piglet','Christopher']>>> bm.random.choice(aa_milne_arr,5,p=[0.5,0.1,0.1,0.3])array(['pooh', 'pooh', 'pooh', 'Christopher', 'piglet'], # random dtype='<U11')
a (float, array_like) – A float or array of floats broadcast-compatible with shape representing the parameter of the distribution.
size (optional, int, tuple of int) – A tuple of nonnegative integers specifying the result shape.
Must be broadcast-compatible with a. The default (None) produces a result shape equal to a.shape.
This is a convenience function for users porting code from Matlab,
and wraps random_sample. That function takes a
tuple to specify the size of the output, which is consistent with
other NumPy functions like numpy.zeros and numpy.ones.
Create an array of the given shape and populate it with
random samples from a uniform distribution
over [0,1).
Parameters:
d0 (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
d1 (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
... (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
dn (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of
the specified dtype in the “half-open” interval [low, high). If
high is None (the default), then results are from [0, low).
Parameters:
low (int or array-like of ints) – Lowest (signed) integers to be drawn from the distribution (unless
high=None, in which case this parameter is one above the
highest such integer).
high (int or array-like of ints, optional) – If provided, one above the largest (signed) integer to be drawn
from the distribution (see above for behavior if high=None).
If array-like, must contain integer values
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. Default is None, in which case a
single value is returned.
dtype (dtype, optional) – Desired dtype of the result. Byteorder must be native.
The default value is int.
Returns:
out – size-shaped array of random integers from the appropriate
distribution, or a single such random int if size not provided.
Return a sample (or samples) from the “standard normal” distribution.
Note
This is a convenience function for users porting code from Matlab,
and wraps standard_normal. That function takes a
tuple to specify the size of the output, which is consistent with
other NumPy functions like numpy.zeros and numpy.ones.
Note
New code should use the standard_normal method of a default_rng()
instance instead; please see the random-quick-start.
If positive int_like arguments are provided, randn generates an array
of shape (d0,d1,...,dn), filled
with random floats sampled from a univariate “normal” (Gaussian)
distribution of mean 0 and variance 1. A single float randomly sampled
from the distribution is returned if no argument is provided.
Parameters:
d0 (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
d1 (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
... (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
dn (int, optional) – The dimensions of the returned array, must be non-negative.
If no argument is given a single Python float is returned.
Returns:
Z – A (d0,d1,...,dn)-shaped array of floating-point samples from
the standard normal distribution, or a single such float if
no parameters were supplied.
Random integers of type np.int_ between low and high, inclusive.
Return random integers of type np.int_ from the “discrete uniform”
distribution in the closed interval [low, high]. If high is
None (the default), then results are from [1, low]. The np.int_
type translates to the C long integer type and its precision
is platform dependent.
Parameters:
low (int) – Lowest (signed) integer to be drawn from the distribution (unless
high=None, in which case this parameter is the highest such
integer).
high (int, optional) – If provided, the largest (signed) integer to be drawn from the
distribution (see above for behavior if high=None).
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. Default is None, in which case a
single value is returned.
Returns:
out – size-shaped array of random integers from the appropriate
distribution, or a single such random int if size not provided.
Choose five random numbers from the set of five evenly-spaced
numbers between 0 and 2.5, inclusive (i.e., from the set
\({0, 5/8, 10/8, 15/8, 20/8}\)):
Return random floats in the half-open interval [0.0, 1.0).
Results are from the “continuous uniform” distribution over the
stated interval. To sample \(Unif[a, b), b > a\) multiply
the output of random_sample by (b-a) and add a:
(b-a)*random_sample()+a
Note
New code should use the random method of a default_rng()
instance instead; please see the random-quick-start.
Parameters:
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. Default is None, in which case a
single value is returned.
Returns:
out – Array of random floats of shape size (unless size=None, in which
case a single float is returned).
It can be an integer for initial seed of the random number generator,
or it can be a JAX’s PRNKey, which is an array with two elements and uint32 dtype.
Modify a sequence in-place by shuffling its contents.
This function only shuffles the array along the first axis of a
multi-dimensional array. The order of sub-arrays is changed but
their contents remains the same.
Parameters:
x (ndarray or MutableSequence) – The array, list or mutable sequence to be shuffled.
df (float, array_like) – A float or array of floats broadcast-compatible with shape representing the parameter of the distribution.
size (optional, int, tuple of int) – A tuple of non-negative integers specifying the result shape.
Must be broadcast-compatible with df. The default (None) produces a result shape equal to df.shape.
Sample truncated standard normal random values with given shape and dtype.
Parameters:
lower (float, ndarray) – A float or array of floats representing the lower bound for
truncation. Must be broadcast-compatible with upper.
upper (float, ndarray) – A float or array of floats representing the upper bound for
truncation. Must be broadcast-compatible with lower.
size (optional, list of int, tuple of int) – A tuple of nonnegative integers specifying the result
shape. Must be broadcast-compatible with lower and upper. The
default (None) produces a result shape by broadcasting lower and
upper.
scale (float, ndarray) – Standard deviation (spread or “width”) of the distribution. Must be
non-negative.
Returns:
out – A random array with the specified dtype and shape given by shape if
shape is not None, or else by broadcasting lower and upper.
Returns values in the open interval (lower,upper).
Draw samples from a 1-parameter Weibull distribution with the given
shape parameter a.
\[X = (-ln(U))^{1/a}\]
Here, U is drawn from the uniform distribution over (0,1].
The more common 2-parameter Weibull, including a scale parameter
\(\lambda\) is just \(X = \lambda(-ln(U))^{1/a}\).
Note
New code should use the weibull method of a default_rng()
instance instead; please see the random-quick-start.
Parameters:
a (float or array_like of floats) – Shape parameter of the distribution. Must be nonnegative.
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if a is a scalar. Otherwise,
np.array(a).size samples are drawn.
Returns:
out – Drawn samples from the parameterized Weibull distribution.
The Weibull (or Type III asymptotic extreme value distribution
for smallest values, SEV Type III, or Rosin-Rammler
distribution) is one of a class of Generalized Extreme Value
(GEV) distributions used in modeling extreme value problems.
This class includes the Gumbel and Frechet distributions.
The probability density for the Weibull distribution is
Samples are drawn from a Zipf distribution with specified parameter
a > 1.
The Zipf distribution (also known as the zeta distribution) is a
discrete probability distribution that satisfies Zipf’s law: the
frequency of an item is inversely proportional to its rank in a
frequency table.
Note
New code should use the zipf method of a default_rng()
instance instead; please see the random-quick-start.
Parameters:
a (float or array_like of floats) – Distribution parameter. Must be greater than 1.
size (int or tuple of ints, optional) – Output shape. If the given shape is, e.g., (m,n,k), then
m*n*k samples are drawn. If size is None (default),
a single value is returned if a is a scalar. Otherwise,
np.array(a).size samples are drawn.
Returns:
out – Drawn samples from the parameterized Zipf distribution.
Return type:
ndarray or scalar
See also
scipy.stats.zipf
probability density function, distribution, or cumulative density function, etc.
random.Generator.zipf
which should be used for new code.
Notes
The probability density for the Zipf distribution is
\[p(k) = \frac{k^{-a}}{\zeta(a)},\]
for integers \(k \geq 1\), where \(\zeta\) is the Riemann Zeta
function.
It is named for the American linguist George Kingsley Zipf, who noted
that the frequency of any word in a sample of a language is inversely
proportional to its rank in the frequency table.