819. Create synthetic minority samples like SMOTE
Why it matters Fraud, defect and rare-intent classifiers often train on SMOTE-balanced data.
SMOTE balances a dataset by inventing minority-class points on the line between a real minority point and one of its nearest minority neighbours.
Write smote_points(X, k, picks). X is (n, d) minority samples. For each (i, nb, lam) in picks: find point i's k nearest neighbours among the OTHER rows of X by Euclidean distance (ties: lower index), take the nb-th of them (0-based, nearest first) as z, and create X[i] + lam * (z - X[i]). Return an array of shape (len(picks), d).
Inputs can be Python lists or NumPy arrays, so convert them with np.asarray first.
def smote_points(X, k, picks)Example 1
Input: smote_points([[0.0, 0.0], [1.0, 0.0], [0.0, 2.0], [5.0, 5.0]], 2, [(0, 0, 0.5)])
Output: [[0.5, 0.0]]
Example 2
Input: X = [[0.0, 0.0], [1.0, 0.0], [0.0, 2.0], [5.0, 5.0]], k = 2, picks = [(0, 1, 0.25), (3, 0, 1.0)]
Output: [[0.0, 0.5], [0.0, 2.0]]
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1
Compute distances from X[i] to every row and sort by (distance, index).
Hint 2
Skip row i itself before taking the first k.
Hint 3
Interpolate with lam between X[i] and the chosen neighbour.
Common mistakes
- Including the point itself as its own nearest neighbour.
- Interpolating toward majority-class points.
Understand the idea
Each synthetic point lies on the segment between a real minority point and a nearby minority point, so it stays inside the minority region instead of copying points exactly.
point (0, 0) has nearest neighbour (1, 0); lam 0.5 gives (0.5, 0).
new = x + lam (z - x), z one of x's k nearest minority neighbours