Borderline-SMOTE From Scratch

Quick Overview

Implement Borderline-SMOTE in NumPy by labelling each minority sample as noise, danger or safe from its nearest neighbours and creating synthetic rows only from danger points. It tests exact nearest-neighbour search with tie-breaking, seeded random sampling, and how boundary-focused oversampling handles class imbalance.

Borderline-SMOTE From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement Borderline-SMOTE (variant 1) from scratch with NumPy as two functions: - `classify_minority_samples(X, y, m_neighbors=10)` returns the indices of the minority samples and a category for each one. - `borderline_smote(X, y, k_neighbors=5, m_neighbors=10, n_synthetic=None, seed=0)` returns `(X_res, y_res)`, the data with synthetic minority rows appended. For each minority sample, let $m$ be `m_neighbors` and let $m'$ be the number of non-minority labels among its $m$ nearest neighbours in the full dataset. The sample is `"noise"` if $m' = m$, `"danger"` if $m/2 \le m' < m$, and `"safe"` otherwise. Synthesize new minority rows only from danger points. Example: ```python X = [[0.0],[1.0],[2.0],[3.0],[0.5],[3.4],[3.9],[5.0],[10.0],[11.0]] y = [0, 0, 0, 0, 1, 1, 1, 1, 0, 0] classify_minority_samples(X, y, m_neighbors=3) # -> ([4, 5, 6, 7], ['noise', 'danger', 'safe', 'safe']) borderline_smote(X, y, k_neighbors=2, m_neighbors=3) # -> X_res.shape == (12, 1), y_res[10:] == [1, 1] ``` ```hint Excluding the point itself Compute every minority-to-row distance in one broadcast, and make sure a point can never show up in its own neighbour list before you sort. ``` ```hint Reproducible output The expected result depends on how distance ties are broken and on the order of the three random draws. Check which `np.argsort` kind keeps equal distances in index order. ``` ### Constraints & Assumptions - The minority class is the label with the fewest samples, and a tie goes to the lower label. Return minority indices in ascending order. - Neighbour search uses Euclidean distance, excludes the point itself, and breaks distance ties by the lowest index. A danger point's `k_neighbors` nearest neighbours are searched among all minority samples. - The default `n_synthetic` is the largest class count minus the minority count. - Use `rng = np.random.default_rng(seed)`. For each synthetic sample, draw in this order: `b = rng.integers(n_danger)` (a position in the danger list sorted by index), `j = rng.integers(k_neighbors)`, then `gap = rng.random()`. With `d` the b-th danger point and `nn_j` its j-th nearest minority neighbour, the new row is `X[d] + gap * (X[nn_j] - X[d])`. - Return the original rows first, then the synthetic rows labelled with the minority class. If there are no danger points or `n_synthetic == 0`, return copies of the inputs. - You may assume `m_neighbors` is smaller than the number of rows and `k_neighbors` is smaller than the minority count. ### What a Strong Answer Covers - Picks the minority label with the stated tie rule and finds neighbours with the point excluded and distance ties broken by lowest index. - Applies the noise, danger and safe thresholds exactly, including odd values of `m_neighbors`. - Searches each danger point's neighbours among all minority samples and makes the three random draws per sample in the specified order from one generator. - Handles the default `n_synthetic`, the no-danger and zero-sample cases, and keeps the original rows ahead of the synthetic ones. - States the time and memory cost of the dense distance computation. ### Follow-up Questions - How does Borderline-SMOTE2 differ from this variant? - How would you scale the neighbour search beyond a dense pairwise distance matrix? - Why does the method skip noise points when it generates samples?

Overview: Implement Borderline-SMOTE in NumPy by labelling each minority sample as noise, danger or safe from its nearest neighbours and creating synthetic rows only from danger points. It tests exact nearest-neighbour search with tie-breaking, seeded random sampling, and how boundary-focused oversampling handles class imbalance.

PracHub logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement Borderline-SMOTE (variant 1) from scratch with NumPy as two functions:

  • classify_minority_samples(X, y, m_neighbors=10) returns the indices of the minority samples and a category for each one.
  • borderline_smote(X, y, k_neighbors=5, m_neighbors=10, n_synthetic=None, seed=0) returns (X_res, y_res) , the data with synthetic minority rows appended.

For each minority sample, let mm be m_neighbors and let m′m' be the number of non-minority labels among its mm nearest neighbours in the full dataset. The sample is "noise" if m′=mm' = m, "danger" if m/2≤m′<mm/2 \le m' < m, and "safe" otherwise. Synthesize new minority rows only from danger points.

Example:

X = [[0.0],[1.0],[2.0],[3.0],[0.5],[3.4],[3.9],[5.0],[10.0],[11.0]]
y = [0, 0, 0, 0, 1, 1, 1, 1, 0, 0]
classify_minority_samples(X, y, m_neighbors=3)
# -> ([4, 5, 6, 7], ['noise', 'danger', 'safe', 'safe'])
borderline_smote(X, y, k_neighbors=2, m_neighbors=3)
# -> X_res.shape == (12, 1), y_res[10:] == [1, 1]

Constraints & Assumptions

  • The minority class is the label with the fewest samples, and a tie goes to the lower label. Return minority indices in ascending order.
  • Neighbour search uses Euclidean distance, excludes the point itself, and breaks distance ties by the lowest index. A danger point's k_neighbors nearest neighbours are searched among all minority samples.
  • The default n_synthetic is the largest class count minus the minority count.
  • Use rng = np.random.default_rng(seed) . For each synthetic sample, draw in this order: b = rng.integers(n_danger) (a position in the danger list sorted by index), j = rng.integers(k_neighbors) , then gap = rng.random() . With d the b-th danger point and nn_j its j-th nearest minority neighbour, the new row is X[d] + gap * (X[nn_j] - X[d]) .
  • Return the original rows first, then the synthetic rows labelled with the minority class. If there are no danger points or n_synthetic == 0 , return copies of the inputs.
  • You may assume m_neighbors is smaller than the number of rows and k_neighbors is smaller than the minority count.

What a Strong Answer Covers Guidance

  • Picks the minority label with the stated tie rule and finds neighbours with the point excluded and distance ties broken by lowest index.
  • Applies the noise, danger and safe thresholds exactly, including odd values of m_neighbors .
  • Searches each danger point's neighbours among all minority samples and makes the three random draws per sample in the specified order from one generator.
  • Handles the default n_synthetic , the no-danger and zero-sample cases, and keeps the original rows ahead of the synthetic ones.
  • States the time and memory cost of the dense distance computation.

Follow-up Questions Guidance

  • How does Borderline-SMOTE2 differ from this variant?
  • How would you scale the neighbour search beyond a dense pairwise distance matrix?
  • Why does the method skip noise points when it generates samples?