multinomial#
- ivy.multinomial(population_size, num_samples, /, *, batch_size=1, probs=None, replace=True, device=None, seed=None, out=None)[source]#
Draws samples from a multinomial distribution. Specifically, returns a tensor where each row contains num_samples indices sampled from the multinomial probability distribution located in the corresponding row of tensor input.
- Parameters:
population_size (
int
) – The size of the population from which to draw samples.num_samples (
int
) – Number of independent samples to draw from the population.batch_size (
int
) – Number of tensors to generate. Default is 1. (default:1
)probs (
Optional
[Union
[Array
,NativeArray
]]) – The unnormalized probabilities for all elements in population, (default:None
) default is uniform [batch_shape, population_size]replace (
bool
) – Whether to replace samples once they’ve been drawn. Default isTrue
. (default:True
)device (
Optional
[Union
[Device
,NativeDevice
]]) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (default:None
) (Default value = None)seed (
Optional
[int
]) – A python integer. Used to create a random seed distribution (default:None
)out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape that the (default:None
) inputs broadcast to.
- Return type:
- Returns:
ret – Drawn samples indices from the multinomial distribution.
Examples
>>> y = ivy.multinomial(10, 5) >>> print(y) ivy.array([[1, 8, 7, 8, 3]])
>>> y = ivy.multinomial(10, 5, batch_size=2, seed=42) >>> print(y) ivy.array([[3, 9, 7, 5, 1], [1, 0, 8, 6, 7]])
>>> y = ivy.multinomial(10, 5, replace=False) >>> print(y) ivy.array([[2, 6, 4, 7, 0]])
With
ivy.Array
input:>>> y = ivy.multinomial(10, 5, probs=ivy.array([1/10]*10)) >>> print(y) ivy.array([5, 2, 7, 6, 9])
>>> y = ivy.multinomial(7, 5, batch_size=2, probs=ivy.array([[1/7]*7, [1/7]*7])) >>> print(y) ivy.array([[0, 4, 3, 4, 5], [1, 1, 0, 3, 2]])
>>> y = ivy.multinomial(7, 5, batch_size=2, probs=ivy.array([[1/7]*7, [1/7]*7]), ... replace=False) >>> print(y) ivy.array([[2, 6, 1, 0, 3], [1, 0, 2, 5, 6]])
With
ivy.NativeArray
input:>>> y = ivy.multinomial(10, 5, probs=ivy.native_array([1/10]*10)) >>> print(y) ivy.array([5, 7, 4, 2, 1])
>>> y = ivy.multinomial(10, 5, batch_size=2, ... probs=ivy.native_array([[1/10]*10, [1/10]*10])) >>> print(y) ivy.array([[8, 0, 4, 1, 7], [2, 3, 4, 9, 3]])
>>> y = ivy.multinomial(10, 5, batch_size=2, ... probs=ivy.native_array([[1/10]*10, [1/10]*10]), ... replace=False) >>> print(y) ivy.array([[0, 2, 6, 9, 1], [6, 7, 2, 4, 3]])
- Array.multinomial(self, population_size, num_samples, /, *, batch_size=1, replace=True, device=None, seed=None, out=None)#
ivy.Array instance method variant of ivy.multinomial. This method simply wraps the function, and so the docstring for ivy.multinomial also applies to this method with minimal changes.
- Parameters:
self (
Array
) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]population_size (
int
) – The size of the population from which to draw samples.num_samples (
int
) – Number of independent samples to draw from the population.batch_size (
int
) – Number of tensors to generate. Default is 1. (default:1
)replace (
bool
) – Whether to replace samples once they’ve been drawn. Default isTrue
. (default:True
)device (
Optional
[Union
[Device
,NativeDevice
]]) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (default:None
) (Default value = None)seed (
Optional
[int
]) – A python integer. Used to create a random seed distribution (default:None
)out (
Optional
[Array
]) – optional output array, for writing the result to. It must have a shape (default:None
) that the inputs broadcast to.
- Return type:
Array
- Returns:
ret – Drawn samples from the parameterized normal distribution.
- Container.multinomial(self, population_size, num_samples, /, *, batch_size=1, replace=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, device=None, seed=None, out=None)#
ivy.Container instance method variant of ivy.multinomial. This method simply wraps the function, and so the docstring for ivy.multinomial also applies to this method with minimal changes.
- Parameters:
self (
Container
) – The unnormalized probabilities for all elements in population, default is uniform [batch_shape, population_size]population_size (
int
) – The size of the population from which to draw samples.num_samples (
int
) – Number of independent samples to draw from the population.batch_size (
int
) – Number of tensors to generate. Default is 1. (default:1
)replace (
bool
) – Whether to replace samples once they’ve been drawn. Default isTrue
. (default:True
)key_chains (
Optional
[Union
[List
[str
],Dict
[str
,str
]]]) – The key-chains to apply or not apply the method to. Default isNone
. (default:None
)to_apply (
bool
) – If True, the method will be applied to key_chains, otherwise key_chains (default:True
) will be skipped. Default isTrue
.prune_unapplied (
bool
) – Whether to prune key_chains for which the function was not applied. (default:False
) Default isFalse
.map_sequences (
bool
) – Whether to also map method to sequences (lists, tuples). (default:False
) Default isFalse
.device (
Optional
[Union
[Device
,NativeDevice
]]) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. (default:None
) (Default value = None)seed (
Optional
[int
]) – A python integer. Used to create a random seed distribution (default:None
)out (
Optional
[Container
]) – optional output array, for writing the result to. It must have a shape (default:None
) that the inputs broadcast to.
- Return type:
Container
- Returns:
ret – Drawn samples from the parameterized normal distribution.