Undersampling methods for imbalanced learning.
Classes
Random under-sampling by removing majority samples.
__init__( self, sampling_strategy: Union[float, str, dict] = 'auto', random_state: Optional[int] = None, replacement: bool = False, )
Parameters
sampling_strategy
Sampling strategy:
- •'auto': reduce all classes to match minority
- •'majority': only undersample majority class
- •dict: {class_label: target_count}
random_state
replacement
>>> from tuiml.preprocessing.sampling import RandomUnderSampler
>>> rus = RandomUnderSampler(sampling_strategy='auto')
>>> X_res, y_res = rus.fit_resample(X, y)
Methods
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
Tomek Links under-sampling.
__init__( self, sampling_strategy: str = 'auto', )
Parameters
sampling_strategy
Which samples to remove:
- •'auto' or 'majority': Remove majority samples
- •'all': Remove both samples from Tomek links
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
Edited Nearest Neighbours (ENN) under-sampling.
__init__( self, n_neighbors: int = 3, kind_sel: str = 'all', sampling_strategy: str = 'auto', )
Parameters
n_neighbors
kind_sel
- •'all': All neighbors must agree
- •'mode': Majority of neighbors must agree
sampling_strategy
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
Condensed Nearest Neighbour (CNN) under-sampling.
__init__( self, n_neighbors: int = 1, random_state: Optional[int] = None, )
Parameters
n_neighbors
random_state
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
NearMissSampler under-sampling.
__init__( self, version: int = 1, n_neighbors: int = 3, sampling_strategy: Union[str, dict] = 'auto', )
Parameters
version
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
sampling_strategy
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
class preprocessing.sampling.undersampling.HardnessThresholdSampler(Transformer)
Instance Hardness Threshold under-sampling.
__init__( self, estimator = None, cv: int = 5, sampling_strategy: Union[str, dict] = 'auto', random_state: Optional[int] = None, )
Parameters
estimator
cv
sampling_strategy
random_state
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str