espnet2.layers.augmentation.weighted_sample_without_replacement
Less than 1 minute
espnet2.layers.augmentation.weighted_sample_without_replacement
espnet2.layers.augmentation.weighted_sample_without_replacement(population, weights, k, rng=<module 'random' from '/data/user_data/msomeki/miniconda/lib/python3.10/random.py'>)
Sample k unique elements from a population based on provided weights.
This function samples k elements from a given population without replacement, ensuring that each element is sampled according to its specified weight. It uses a probabilistic method to select elements, which means that elements with higher weights have a greater chance of being selected.
- Parameters:
- population (list) – A list of elements from which to sample.
- weights (list) – A list of weights corresponding to each element in the population. The weights must be positive and have the same length as the population.
- k (int) – The number of unique elements to sample. Must be less than or equal to the length of the population.
- rng (random.Random , optional) – An optional random number generator. If not provided, the default random module will be used.
- Returns: A list of k unique elements sampled from the population.
- Return type: list
- Raises:ValueError – If k is greater than the length of the population.
Examples
>>> population = ['apple', 'banana', 'cherry', 'date']
>>> weights = [0.1, 0.2, 0.3, 0.4]
>>> weighted_sample_without_replacement(population, weights, 2)
['cherry', 'date'] # Example output may vary due to randomness.
>>> weighted_sample_without_replacement(population, weights, 0)
[] # Sampling 0 elements returns an empty list.
NOTE
The function ensures that no element is selected more than once.