API reference
Random — multivariate distributions (Generator)
Multivariate samplers on np.random.Generator. All methods return an NDArray (never a scalar, since the output is always multi-dimensional).
| Name | Summary |
|---|---|
np.multinomial | Draw samples from a multinomial distribution: n experiments, each with outcome probabilities pvals. |
np.dirichlet | Draw samples from a Dirichlet distribution. |
np.multivariateHypergeometric | Draw samples from a multivariate hypergeometric distribution: take nsample items without replacement from groups of sizes colors. |
np.permuted | Return a copy of x with the lanes along axis independently and randomly shuffled. |
np.multinomial
#rng.multinomial(n, pvals, size?)
Draw samples from a multinomial distribution: n experiments, each with outcome probabilities pvals. Returns int64 NDArray of shape (*size, d). Equivalent to numpy.random.Generator.multinomial.
Parameters
nnumber- Number of experiments (non-negative integer).
pvalsnumber[]- Outcome probabilities (sum <= 1).
[size]number | number[]- Number of samples. Default: one sample.
Returns
NDArray<int64>
Example
const rng = np.random.defaultRng(42);
rng.multinomial(10, [0.5, 0.3, 0.2]).toArray(); // => [6, 3, 1]TypeScript declaration
rng.multinomial(n: number, pvals: readonly number[], size?: Size | null | undefined): NDArraynp.dirichlet
#rng.dirichlet(alpha, size?)
Draw samples from a Dirichlet distribution. Returns float64 NDArray of shape (*size, d). Equivalent to numpy.random.Generator.dirichlet.
Parameters
alphanumber[]- Concentration parameters (all > 0).
[size]number | number[]- Number of samples. Default: one sample.
Returns
NDArray<float64>
Example
const rng = np.random.defaultRng(42);
const d = rng.dirichlet([1, 1, 1]);
d.shape; // => [3]TypeScript declaration
rng.dirichlet(alpha: readonly number[], size?: Size | null | undefined): NDArraynp.multivariateHypergeometric
#rng.multivariateHypergeometric(colors, nsample, size?, method?)
Draw samples from a multivariate hypergeometric distribution: take nsample items without replacement from groups of sizes colors. Returns int64 NDArray of shape (*size, len(colors)). Equivalent to numpy.random.Generator.multivariate_hypergeometric.
Parameters
colorsnumber[]- Number of items per color group.
nsamplenumber- Number of items to sample.
[size]number | number[]- Number of samples.
[method]"count" | "marginals"- Sampling algorithm. Default
"marginals".
Returns
NDArray<int64>
Example
const rng = np.random.defaultRng(42);
rng.multivariateHypergeometric([3, 5, 2], 4).toArray(); // => [2, 1, 1]TypeScript declaration
rng.multivariateHypergeometric(colors: readonly number[], nsample: number, size?: Size | null | undefined, method?: "count" | "marginals" | undefined): NDArraynp.permuted
#rng.permuted(x, axis?)
Return a copy of x with the lanes along axis independently and randomly shuffled. Unlike shuffle (which shuffles axis-0 slices jointly), permuted shuffles each lane independently. Equivalent to numpy.random.Generator.permuted.
Parameters
xNDArray- Input array (at least 1-dimensional).
[axis]number- Axis along which to permute. Default
0.
Returns
NDArray
Example
const rng = np.random.defaultRng(0);
const a = np.arange(6).reshape(2, 3);
const p = rng.permuted(a, 1);
p.shape; // => [2, 3]TypeScript declaration
rng.permuted(x: NDArray, axis?: number | undefined): NDArray