cascade.utils.samplers#
- class cascade.utils.samplers.OverSampler(dataset: Dataset[Tuple[Any, Any]], *args: Any, **kwargs: Any)[source]#
-
Accepts datasets which return tuples of objects and labels in the respected order. Isn’t lazy - runs through all the items ones to determine key order. Doesn’t store values afterwards.
To oversample it repeats items with minority labels for the amount of times needed to make equal distribution. Works for any number of classes.
Labels are considered to be in the second place of each item that a dataset returns.
Important
Sampler orders the items in the dataset. Consider shuffling the dataset after sampling if label order is important.
- class cascade.utils.samplers.UnderSampler(dataset: Dataset[Tuple[Any, Any]], *args: Any, **kwargs: Any)[source]#
-
Accepts datasets which return tuples of objects and labels. Isn’t lazy - runs through all the items ones to determine key order. Doesn’t store values in memory afterwards.
To undersample it removes items of majority class for the amount of times needed to make equal distribution. Works for any number of classes.
Labels are considered to be in the second place of each item that a dataset returns.
Important
Sampler orders the items in the dataset. Consider shuffling the dataset after sampling if label order is important.
- class cascade.utils.samplers.WeighedSampler(dataset: Dataset[Tuple[Any, Any]], partitioning: Optional[Dict[Any, int]] = None)[source]#
-
Samples each class certain amount of times.
Important
Sampler orders the items in the dataset in such way that items with each label go in row. Consider shuffling the dataset after sampling if label order is important.
Example
>>> from cascade import data as cdd >>> from cascade.utils.samplers import WeighedSampler >>> ds = cdd.Wrapper([('item1', 0), ('item2', 1)]) >>> ds = WeighedSampler(ds, {0: 2, 1: 1}) >>> assert [item for item in ds] == [('item1', 0), ('item1', 0), ('item2', 1)]
See also
cascade.utils.OverSampler,cascade.utils.UnderSampler,cascade.data.RandomSampler- __init__(dataset: Dataset[Tuple[Any, Any]], partitioning: Optional[Dict[Any, int]] = None) None[source]#