composeml.LabelTimes.sample

LabelTimes.sample(n=None, frac=None, random_state=None, replace=False)[source]

Return a random sample of labels.

Parameters:
  • n (int or dict) – Sample number of labels. A dictionary returns the number of samples to each label. Cannot be used with frac.
  • frac (float or dict) – Sample fraction of labels. A dictionary returns the sample fraction to each label. Cannot be used with n.
  • random_state (int) – Seed for the random number generator.
  • replace (bool) – Sample with or without replacement. Default value is False.
Returns:

Random sample of labels.

Return type:

LabelTimes

Examples

Create mock data:

>>> labels = {'labels': list('AABBBAA')}
>>> labels = LabelTimes(labels, name='labels')
>>> labels
  labels
0      A
1      A
2      B
3      B
4      B
5      A
6      A

Sample number of labels:

>>> labels.sample(n=3, random_state=0).sort_index()
  labels
1      A
2      B
6      A

Sample number per label:

>>> n_per_label = {'A': 1, 'B': 2}
>>> labels.sample(n=n_per_label, random_state=0).sort_index()
  labels
3      B
4      B
5      A

Sample fraction of labels:

>>> labels.sample(frac=.4, random_state=2).sort_index()
  labels
1      A
3      B
4      B

Sample fraction per label:

>>> frac_per_label = {'A': .5, 'B': .34}
>>> labels.sample(frac=frac_per_label, random_state=2).sort_index()
  labels
4      B
5      A
6      A