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.

__init__(dataset: Dataset[Tuple[Any, Any]], *args: Any, **kwargs: Any) None[source]#

Constructs a Sampler.

Parameters:
  • dataset (Dataset) – A dataset to sample from

  • num_samples (int) – The number of samples to use as a new length

get(index: int) Tuple[Any, Any][source]#

Returns previous Dataset’s item without changes if not overridden

Parameters:

index (Any) – Item’s index

Returns:

Item

Return type:

T

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.

__init__(dataset: Dataset[Tuple[Any, Any]], *args: Any, **kwargs: Any) None[source]#

Constructs a Sampler.

Parameters:
  • dataset (Dataset) – A dataset to sample from

  • num_samples (int) – The number of samples to use as a new length

get(index: int) Tuple[Any, Any][source]#

Returns previous Dataset’s item without changes if not overridden

Parameters:

index (Any) – Item’s index

Returns:

Item

Return type:

T

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]#
Parameters:
  • dataset (Dataset) – A dataset to sample

  • partitioning (Dict[Any, int], optional) – A dictionary with labels as keys and the number of samples as values. If some label omitted, assumes that it should be sampled the same number of times it actually appears in the dataset.

get(index: int) Tuple[Any, Any][source]#

Returns previous Dataset’s item without changes if not overridden

Parameters:

index (Any) – Item’s index

Returns:

Item

Return type:

T

get_meta() List[Dict[Any, Any]][source]#

Overrides base method enabling cascade-like calls to previous datasets. The metadata of a pipeline that consist of several modifiers can be easily obtained with get_meta of the last block.