API Reference / preprocessing / sampling /

undersampling.py

Undersampling methods for imbalanced learning.

Methods to reduce majority class samples to balance datasets.

Classes

RandomUnderSampler

class preprocessing.sampling.undersampling.RandomUnderSampler(Transformer)

Random under-sampling by removing majority samples.

Randomly removes samples from majority class to balance classes.
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    random_state: Optional[int] = None,
    replacement: bool = False,
)

Parameters

sampling_strategy
float or str or dict = 'auto'

Sampling strategy:

  • 'auto': reduce all classes to match minority
  • 'majority': only undersample majority class
  • dict: {class_label: target_count}
random_state
int
Random seed.
replacement
bool = False
Whether to sample with replacement.
python
>>> from tuiml.preprocessing.sampling import RandomUnderSampler
>>> rus = RandomUnderSampler(sampling_strategy='auto')
>>> X_res, y_res = rus.fit_resample(X, y)

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'RandomUnderSampler'

Fit the sampler.

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Fit and resample.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str

TomekLinksSampler

class preprocessing.sampling.undersampling.TomekLinksSampler(Transformer)

Tomek Links under-sampling.

Removes majority class samples that form Tomek links with minority samples. A Tomek link exists if two samples of different classes are each other's nearest neighbors.
Constructor
__init__(
    self,
    sampling_strategy: str = 'auto',
)

Parameters

sampling_strategy
str = 'auto'

Which samples to remove:

  • 'auto' or 'majority': Remove majority samples
  • 'all': Remove both samples from Tomek links
python
>>> from tuiml.preprocessing.sampling import TomekLinksSampler
>>> tl = TomekLinksSampler()
>>> X_res, y_res = tl.fit_resample(X, y)

References

Tomek, I. (1976). Two Modifications of CNN. IEEE Transactions on Systems, Man, and Cybernetics.

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'TomekLinksSampler'

Fit (no-op).

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Find and remove Tomek links.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str

ENNSampler

class preprocessing.sampling.undersampling.ENNSampler(Transformer)

Edited Nearest Neighbours (ENN) under-sampling.

Removes samples whose class differs from the majority of their k nearest neighbors.
Constructor
__init__(
    self,
    n_neighbors: int = 3,
    kind_sel: str = 'all',
    sampling_strategy: str = 'auto',
)

Parameters

n_neighbors
int = 3
Number of nearest neighbors.
kind_sel
{'all', 'mode'} = 'all'
  • 'all': All neighbors must agree
  • 'mode': Majority of neighbors must agree
sampling_strategy
str = 'auto'
Which classes to clean.
python
>>> from tuiml.preprocessing.sampling import ENNSampler
>>> enn = ENNSampler(n_neighbors=3)
>>> X_res, y_res = enn.fit_resample(X, y)

References

Wilson, D. L. (1972). Asymptotic Properties of Nearest Neighbor Rules Using Edited Data. IEEE Transactions on Systems, Man, and Cybernetics.

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'ENNSampler'

Fit (no-op).

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Remove noisy samples.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str

CNNSampler

class preprocessing.sampling.undersampling.CNNSampler(Transformer)

Condensed Nearest Neighbour (CNN) under-sampling.

Iteratively removes samples that do not affect the nearest neighbor classification rule.
Constructor
__init__(
    self,
    n_neighbors: int = 1,
    random_state: Optional[int] = None,
)

Parameters

n_neighbors
int = 1
Number of nearest neighbors.
random_state
int
Random seed.
python
>>> from tuiml.preprocessing.sampling import CNNSampler
>>> cnn = CNNSampler()
>>> X_res, y_res = cnn.fit_resample(X, y)

References

Hart, P. (1968). The Condensed Nearest Neighbor Rule. IEEE Transactions on Information Theory.

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'CNNSampler'

Fit (no-op).

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Condense the dataset.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str

NearMissSampler

class preprocessing.sampling.undersampling.NearMissSampler(Transformer)

NearMissSampler under-sampling.

Selects majority samples based on their distance to minority samples.
Constructor
__init__(
    self,
    version: int = 1,
    n_neighbors: int = 3,
    sampling_strategy: Union[str, dict] = 'auto',
)

Parameters

version
int = 1

Version of NearMissSampler:

  • 1: Select samples with smallest average distance to k nearest minority
  • 2: Select samples with smallest average distance to k farthest minority
  • 3: Select samples with largest average distance to k nearest minority
n_neighbors
int = 3
Number of nearest neighbors.
sampling_strategy
str or dict = 'auto'
Sampling strategy.
python
>>> from tuiml.preprocessing.sampling import NearMissSampler
>>> nm = NearMissSampler(version=1)
>>> X_res, y_res = nm.fit_resample(X, y)

References

Mani, I., & Zhang, I. (2003). kNN approach to unbalanced data distributions: a case study involving information extraction.

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'NearMissSampler'

Fit the sampler.

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Apply NearMissSampler under-sampling.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str

HardnessThresholdSampler

class preprocessing.sampling.undersampling.HardnessThresholdSampler(Transformer)

Instance Hardness Threshold under-sampling.

Removes samples that are hard to classify based on classifier probability estimates.
Constructor
__init__(
    self,
    estimator = None,
    cv: int = 5,
    sampling_strategy: Union[str, dict] = 'auto',
    random_state: Optional[int] = None,
)

Parameters

estimator
object
Classifier to use. If None, uses RandomForest.
cv
int = 5
Cross-validation folds for probability estimation.
sampling_strategy
str or dict = 'auto'
Sampling strategy.
random_state
int
Random seed.
python
>>> from tuiml.preprocessing.sampling import HardnessThresholdSampler
>>> iht = HardnessThresholdSampler()
>>> X_res, y_res = iht.fit_resample(X, y)

References

Smith, M. R., Martinez, T., & Giraud-Carrier, C. (2014). An instance level analysis of data complexity. Machine learning.

Methods

get_parameter_schema (cls) -> Dict[str, Dict]

Return JSON Schema for parameters.

fit (self, X: np.ndarray, y: np.ndarray) -> 'HardnessThresholdSampler'

Fit the sampler.

fit_resample (self, X: np.ndarray, y: np.ndarray) -> Tuple[np.ndarray, np.ndarray]

Remove hard instances.

transform (self, X: np.ndarray) -> np.ndarray
__repr__ (self) -> str