RandomCombinations

giant.utilities.random_combination:

class giant.utilities.random_combination.RandomCombinations(population, combo_length, number_of_combos)[source]

Iterate over number_of_combos random combinations of combo_length from population.

This iterator ensures unique combinations are returned. If more combinations are requested than are possible, then an exhaustive list is returned. This is particularly useful for RANSAC analysis, where we need to generate multiple unique subsets of data points to fit models.

The population that combinations are coming from can either be provided directly, or an integer can be provided to create a range based population.

For example:

>>> from giant.utilities.random_combination import RandomCombination
>>> # works for an integer sequence
>>> for sample in RandomCombination(10, 2, 3): print(sample)
(2, 6)
(1, 7)
(1, 8)
>>> # works for any sequence like
>>> for sample in RandomCombination('abcdefghijk', 2, 3): print(sample)
('c', 'k')
('d', 'j')
('g', 'j')
>>> # returns an exhaustive set if there are more combos requested than can be made uniquely
>>> for sample in RandomCombination(10, 2, 3): print(sample)
(0, 1)
(0, 2)
(1, 2)

Note that the type of whatever is contained in the sequence like object must support ordering.

Parameters:
  • population (BasicSequenceProtocol) – The population to choose from. If specified as an integer then the population will be range(int).

  • combo_length (int) – The length for each combination as an integer

  • number_of_combos (int) – the number of unique combinations you want as an integer