SMOTE family of oversampling algorithms.

This module contains all SMOTE variants:
  • SMOTE: Original Synthetic Minority Over-sampling Technique
  • BorderlineSMOTESampler: SMOTE for borderline instances
  • ADASYN: Adaptive Synthetic Sampling
  • SVMSMOTE: SMOTE using SVM to find support vectors
  • KMeansSMOTE: SMOTE with K-Means clustering

Classes

SMOTESampler

class preprocessing.sampling.smote.SMOTESampler(Transformer)

Synthetic Minority Over-sampling Technique (SMOTE).

Generates synthetic samples for the minority class(es) by interpolating between existing instances and their nearest neighbors.
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    k_neighbors: int = 5,
    random_state: Optional[int] = None,
)

Overview

SMOTE addresses class imbalance by creating "plausible" synthetic examples rather than simply duplicating existing ones. This helps the model generalize better by expanding the minority class decision regions.

Theory

For a minority sample x_i, a neighbor \hat{x}_i is randomly chosen from its k nearest minority neighbors. A new sample x_{new} is generated as:

x_{new} = x_i + \lambda \cdot (\hat{x}_i - x_i)

where \lambda is a random number in [0, 1].

Parameters

sampling_strategy
float, str or dict = "auto"

Determines which classes to resample and by how much:

  • "auto" / "not majority": Resample all classes except the majority.
  • "minority": Resample only the minority class.
  • "all": Resample all classes to match the majority.
  • Dict: {class_label: n_samples} specifying exact counts.
k_neighbors
int = 5
Number of nearest neighbors to use for interpolation.
random_state
int
Seed for the random number generator to ensure reproducibility.

Attributes

sampling_strategy_
dict
The resolved mapping of class labels to the number of samples to generate.

Oversample the minority class:

python
>>> from tuiml.preprocessing.sampling import SMOTESampler
>>> import numpy as np
>>> X = np.array([[1, 2], [2, 1], [8, 9], [7, 8], [8, 8]])
>>> y = np.array([0, 0, 1, 1, 1])  # 0 is minority
>>> smote = SMOTESampler(k_neighbors=1)
>>> X_res, y_res = smote.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) -> 'SMOTE'

Fit and compute sampling strategy.

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

Fit and resample the dataset.

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

BorderlineSMOTESampler

class preprocessing.sampling.smote.BorderlineSMOTESampler(SMOTESampler)

Borderline-SMOTE for oversampling near decision boundaries.

A variant of SMOTE that only generates synthetic samples for minority instances that are "at risk" of being misclassified (i.e., near the boundary with the majority class).
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    k_neighbors: int = 5,
    m_neighbors: int = 10,
    kind: str = 'borderline-1',
    random_state: Optional[int] = None,
)

Overview

Borderline-SMOTE identifies minority instances whose neighbors are mostly from the majority class and prioritizes them for oversampling. This focuses the reinforcement where the classification task is hardest.

Parameters

sampling_strategy
float, str or dict = "auto"
Sampling strategy (see SMOTESampler).
k_neighbors
int = 5
Number of nearest neighbors used for SMOTE interpolation.
m_neighbors
int = 10
Number of nearest neighbors used to determine if a sample is on the borderline.
kind
{"borderline-1", "borderline-2"} = "borderline-1"

The type of Borderline-SMOTE:

  • "borderline-1": Interpolates between borderline samples and

their minority neighbors.

  • "borderline-2": Interpolates between borderline samples and
any of their nearest neighbors (minority or majority).
random_state
int
Seed for reproducibility.

Oversample near the decision boundary:

python
>>> from tuiml.preprocessing.sampling import BorderlineSMOTESampler
>>> import numpy as np
>>> X = np.random.rand(100, 2)
>>> y = (X[:, 0] + X[:, 1] > 1).astype(int)
>>> sampler = BorderlineSMOTESampler(m_neighbors=5)
>>> X_res, y_res = sampler.fit_resample(X, y)

Methods

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

Return JSON Schema for parameters.

__repr__ (self) -> str

ADASYNSampler

class preprocessing.sampling.smote.ADASYNSampler(SMOTESampler)

Adaptive Synthetic Sampling (ADASYN).

Generates synthetic samples by focusing on minority instances that are "harder" to learn, based on the density of majority class neighbors.
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    k_neighbors: int = 5,
    random_state: Optional[int] = None,
)

Overview

Unlike SMOTE, ADASYN uses a weighted distribution for minority classes according to their level of difficulty in learning. More synthetic data is generated for minority class samples that are harder to learn compared to those that are easier to learn.

Theory

The number of samples to generate for a minority instance x_i is proportional to its difficulty ratio r_i:

r_i = \frac{\Delta_i}{k}

where \Delta_i is the number of majority class neighbors among the k nearest neighbors of x_i.

Parameters

sampling_strategy
float, str or dict = "auto"
Sampling strategy (see SMOTESampler).
k_neighbors
int = 5
Number of nearest neighbors used to compute the difficulty ratio and for interpolation.
random_state
int
Seed for reproducibility.

Adaptive oversampling:

python
>>> from tuiml.preprocessing.sampling import ADASYNSampler
>>> import numpy as np
>>> X = np.random.rand(100, 2)
>>> y = (X[:, 0] > 0.8).astype(int) # Highly imbalanced
>>> sampler = ADASYNSampler(k_neighbors=5)
>>> X_res, y_res = sampler.fit_resample(X, y)

Methods

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

Return JSON Schema for parameters.

__repr__ (self) -> str

SVMSMOTESampler

class preprocessing.sampling.smote.SVMSMOTESampler(SMOTESampler)

SVM-SMOTE for oversampling using SVM support vectors.

Uses an SVM classifier to identify the decision boundary and generates synthetic samples around the minority class support vectors.
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    k_neighbors: int = 5,
    svm_estimator = None,
    random_state: Optional[int] = None,
)

Overview

By focusing on support vectors, SVMSMOTE concentrates oversampling in the region where the minority and majority classes are most likely to overlap, effectively strengthening the decision boundary.

Parameters

sampling_strategy
float, str or dict = "auto"
Sampling strategy (see SMOTESampler).
k_neighbors
int = 5
Number of nearest neighbors used for interpolation.
svm_estimator
object
The SVM estimator used to find support vectors. If None, a default SVC is used.
random_state
int
Seed for reproducibility.

Oversample using SVM support vectors:

python
>>> from tuiml.preprocessing.sampling import SVMSMOTESampler
>>> import numpy as np
>>> X = np.random.rand(100, 2)
>>> y = (X[:, 1] > 0.7).astype(int)
>>> sampler = SVMSMOTESampler()
>>> X_res, y_res = sampler.fit_resample(X, y)

Methods

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

Return JSON Schema for parameters.

__repr__ (self) -> str

KMeansSMOTESampler

class preprocessing.sampling.smote.KMeansSMOTESampler(SMOTESampler)

K-Means SMOTE for oversampling in "safe" clusters.

Combines K-Means clustering with SMOTE to avoid generating noise and to focus oversampling on dense minority regions.
Constructor
__init__(
    self,
    sampling_strategy: Union[float, str, dict] = 'auto',
    k_neighbors: int = 5,
    n_clusters: int = None,
    cluster_balance_threshold: float = 0.5,
    random_state: Optional[int] = None,
)

Overview

The algorithm performs three steps:
  1. Cluster the entire dataset using K-Means.
  2. Filter clusters, keeping only those with a high proportion of minority
samples ("safe" clusters).
  1. Apply SMOTE within each safe cluster.

Parameters

sampling_strategy
float, str or dict = "auto"
Sampling strategy (see SMOTESampler).
k_neighbors
int = 5
Number of nearest neighbors used for SMOTE.
n_clusters
int
Number of clusters for K-Means. If None, defaults to :math:`\sqrt{n_{minority}}`.
cluster_balance_threshold
float = 0.5
The minimum ratio of minority samples in a cluster for it to be considered "safe" for oversampling.
random_state
int
Seed for reproducibility.

Cluster-based oversampling:

python
>>> from tuiml.preprocessing.sampling import KMeansSMOTESampler
>>> import numpy as np
>>> X = np.random.rand(200, 2)
>>> y = (np.linalg.norm(X - 0.5, axis=1) < 0.2).astype(int)
>>> sampler = KMeansSMOTESampler(n_clusters=10)
>>> X_res, y_res = sampler.fit_resample(X, y)

Methods

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

Return JSON Schema for parameters.

__repr__ (self) -> str