#429SMOTE OversamplingMedium

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:

  1. Pick a random minority row ii.
  2. Find its kk nearest neighbours within XminorityX_{\text{minority}}.
  3. Pick one neighbour jj uniformly at random from those kk.
  4. Sample λUniform(0,1)\lambda \sim \text{Uniform}(0, 1).
  5. Synthetic row = Xi+λ(XjXi)X_i + \lambda (X_j - X_i).

Input

  • X_minoritynp.ndarray shape (n,d)(n, d), the minority-class examples.
  • n_synthint >= 0, number of synthetic rows to generate.
  • kint >= 1, neighbours per source (default 5).
  • seedint, RNG seed.

Output

Returns np.ndarray shape (n_synth,d)(n\_synth, d) 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 k+1k+1 minority rows to find kk neighbours).
  • Use np.random.default_rng(seed) for reproducibility.
  • Returns np.ndarray of float.

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.
Python
Loading...

▶ 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