SMOTE Oversampling
Background
SMOTE (Synthetic Minority Over-sampling Technique) generates new minority-class samples by interpolating between existing ones along their k-nearest-neighbour edges. The classic fix for class imbalance in tabular fraud / medical screening tasks — strictly better than random duplication because it spreads the minority mass through the feature space.
Problem statement
Implement smote(X_minority, n_synth, k=5, seed=0). For each of n_synth synthetic rows:
- Pick a random minority row .
- Find its nearest neighbours within .
- Pick one neighbour uniformly at random from those .
- Sample .
- Synthetic row = .
Input
X_minority—np.ndarrayshape , the minority-class examples.n_synth—int >= 0, number of synthetic rows to generate.k—int >= 1, neighbours per source (default5).seed—int, RNG seed.
Output
Returns np.ndarray shape of synthetic minority samples.
Examples
Example 1 — output shape
Input: X=(20, 4), n_synth=50
Output: shape=(50, 4)
Example 2 — synthetic points stay in the feature range
Input: X drawn from Uniform(0, 1)
Output: every synthetic row lies in [min - eps, max + eps] elementwise
Example 3 — reproducibility
Input: same seed
Output: same array
Constraints
k < n(need at least minority rows to find neighbours).- Use
np.random.default_rng(seed)for reproducibility. - Returns
np.ndarrayoffloat.
Notes
- Why interpolate. Random oversampling duplicates the same points; SMOTE creates new points that fill the convex region between existing minorities — preventing the classifier from over-fitting to specific examples.
- Borderline / ADASYN. Borderline-SMOTE samples only from minorities near the decision boundary; ADASYN focuses on the hardest examples. Same kernel, different sampling weights.
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference: two-point interpolation
- •Sample: paired points
- •Example: unit-square grid