composeml.LabelTimes.sample

LabelTimes.sample(n=None, frac=None, random_state=None, replace=False, per_instance=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.

  • per_instance (bool) – Whether to apply sampling to each group. Default is False.

Returns

Random sample of labels.

Return type

LabelTimes

Examples

Create a label times object.

>>> entity = [0, 0, 1, 1]
>>> labels = [True, False, True, False]
>>> data = {'entity': entity, 'labels': labels}
>>> lt = LabelTimes(data=data, target_dataframe_index='entity', target_columns=['labels'])
>>> lt
   entity  labels
0       0    True
1       0   False
2       1    True
3       1   False

Sample a number of the examples.

>>> lt.sample(n=3, random_state=0)
   entity  labels
1       0   False
2       1    True
3       1   False

Sample a fraction of the examples.

>>> lt.sample(frac=.25, random_state=0)
   entity  labels
2       1    True

Sample a number of the examples for specific labels.

>>> n = {True: 1, False: 1}
>>> lt.sample(n=n, random_state=0)
   entity  labels
2       1    True
3       1   False

Sample a fraction of the examples for specific labels.

>>> frac = {True: .5, False: .5}
>>> lt.sample(frac=frac, random_state=0)
   entity  labels
2       1    True
3       1   False

Sample a number of the examples from each entity group.

>>> lt.sample(n={True: 1}, per_instance=True, random_state=0)
   entity  labels
0       0    True
2       1    True

Sample a fraction of the examples from each entity group.

>>> lt.sample(frac=.5, per_instance=True, random_state=0)
   entity  labels
1       0   False
3       1   False