SMOTE family of oversampling algorithms.
- 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
Synthetic Minority Over-sampling Technique (SMOTE).
__init__( self, sampling_strategy: Union[float, str, dict] = 'auto', k_neighbors: int = 5, random_state: Optional[int] = None, )
Overview
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:
where \lambda is a random number in [0, 1].
Parameters
sampling_strategy
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
random_state
Attributes
sampling_strategy_
Oversample the minority class:
>>> 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
transform
(self, X: np.ndarray) -> np.ndarray
__repr__
(self) -> str
Borderline-SMOTE for oversampling near decision boundaries.
__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
Parameters
sampling_strategy
SMOTESampler).
k_neighbors
m_neighbors
kind
The type of Borderline-SMOTE:
- •
"borderline-1": Interpolates between borderline samples and
their minority neighbors.
- •
"borderline-2": Interpolates between borderline samples and
random_state
Oversample near the decision boundary:
>>> 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)
Adaptive Synthetic Sampling (ADASYN).
__init__( self, sampling_strategy: Union[float, str, dict] = 'auto', k_neighbors: int = 5, random_state: Optional[int] = None, )
Overview
Theory
The number of samples to generate for a minority instance x_i is proportional to its difficulty ratio r_i:
where \Delta_i is the number of majority class neighbors among the k nearest neighbors of x_i.
Parameters
sampling_strategy
SMOTESampler).
k_neighbors
random_state
See Also
Adaptive oversampling:
>>> 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)
SVM-SMOTE for oversampling using SVM support vectors.
__init__( self, sampling_strategy: Union[float, str, dict] = 'auto', k_neighbors: int = 5, svm_estimator = None, random_state: Optional[int] = None, )
Overview
Parameters
sampling_strategy
SMOTESampler).
k_neighbors
svm_estimator
None, a default SVC is used.
random_state
Oversample using SVM support vectors:
>>> 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)
K-Means SMOTE for oversampling in "safe" clusters.
__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
- Cluster the entire dataset using K-Means.
- Filter clusters, keeping only those with a high proportion of minority
- Apply SMOTE within each safe cluster.
Parameters
sampling_strategy
SMOTESampler).
k_neighbors
n_clusters
None, defaults to :math:`\sqrt{n_{minority}}`.
cluster_balance_threshold
random_state
Cluster-based oversampling:
>>> 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)