bundles / numpy 2.5.0.dev0+git20251130.2de293a / numpy / random / _generator / Generator / multinomial
cython_function_or_method
numpy.random._generator:Generator.multinomial
Signature
def multinomial ( n , pvals , size = None ) Summary
Draw samples from a multinomial distribution.
Extended Summary
The multinomial distribution is a multivariate generalization of the binomial distribution. Take an experiment with one of p possible outcomes. An example of such an experiment is throwing a dice, where the outcome can be 1 through 6. Each sample drawn from the distribution represents n such experiments. Its values, X_i = [X_0, X_1, ..., X_p], represent the number of times the outcome was i.
Parameters
n: int or array-like of intsNumber of experiments.
pvals: array-like of floatsProbabilities of each of the
pdifferent outcomes with shape(k0, k1, ..., kn, p). Each elementpvals[i,j,...,:]must sum to 1 (however, the last element is always assumed to account for the remaining probability, as long assum(pvals[..., :-1], axis=-1) <= 1.0. Must have at least 1 dimension where pvals.shape[-1] > 0.size: int or tuple of ints, optionalOutput shape. If the given shape is, e.g.,
(m, n, k), thenm * n * ksamples are drawn each withpelements. Default is None where the output size is determined by the broadcast shape ofnand all by the final dimension ofpvals, which is denoted asb=(b0, b1, ..., bq). If size is not None, then it must be compatible with the broadcast shapeb. Specifically, size must haveqor more elements and size[-(q-j):] must equalbj.
Returns
out: ndarrayThe drawn samples, of shape size, if provided. When size is provided, the output shape is size + (p,) If not specified, the shape is determined by the broadcast shape of
nandpvals,(b0, b1, ..., bq)augmented with the dimension of the multinomial,p, so that that output shape is(b0, b1, ..., bq, p).Each entry
out[i,j,...,:]is ap-dimensional value drawn from the distribution.
Examples
Throw a dice 20 times:rng = np.random.default_rng()
✓rng.multinomial(20, [1/6.]*6, size=1)
✗rng.multinomial(20, [1/6.]*6, size=2)
✗rng.multinomial([[10], [20]], [1/6.]*6, size=(2, 2))
✗rng.multinomial(100, [1/7.]*5 + [2/7.])
✗rng.multinomial([10, 20],[[1/4]*4 + [0]*2, [1/6]*6])
✗rng.multinomial(1, [[.1, .5, .4 ], [.3, .7, .0]])
✗pvals = [[.1, .5, .4 ], [.3, .7, .0]] rvs = rng.multinomial(1, pvals, size=(4,2))✓
rvs.argmax(axis=-1)
✗rvs = rng.multinomial([[1]] * 4, pvals)
✓rvs.argmax(axis=-1)
✗rng.multinomial(100, [1.0 / 3, 2.0 / 3]) # RIGHT
✗rng.multinomial(100, [1.0, 2.0]) # WRONG
✓Aliases
-
numpy.random.Generator.multinomial