NearMiss-1 Undersampling From Scratch

Quick Overview

Implement NearMiss-1 undersampling for a binary dataset, keeping every minority row and the majority rows that sit closest to their nearest minority neighbours. It tests distance computation in NumPy, deterministic tie-breaking, input validation, and how distance-based undersampling behaves on real data.

NearMiss-1 Undersampling From Scratch

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `near_miss_1(X, y, k=3, ratio=1.0)` for a binary dataset. Keep every minority row. Score each majority row by its mean Euclidean distance to its `min(k, n_minority)` nearest minority rows, and keep the `floor(n_minority / ratio)` lowest-scoring majority rows (capped at `n_majority`). Return `(X[kept], y[kept], kept)`. Example: ```python X = [[0,1],[0,0],[5,5],[1,1],[1,0],[10,0],[0.5,0.2],[3,0]] y = [0, 1, 0, 0, 1, 0, 0, 0] near_miss_1(X, y, k=2, ratio=1.0) # minority = 1 (rows 1, 4), keep 2 majority rows # scores: row6=0.539, row0=1.207, row3=1.207 (tie -> lower index) # kept = [0, 1, 4, 6], y[kept] = [0, 1, 1, 0] ``` Class 1 has 2 rows, so it is the minority and `floor(2 / 1.0) = 2` majority rows survive. Row 6 has the lowest score. Rows 0 and 3 tie, and row 0 wins because its index is lower. ```hint Distances in bulk Every majority row needs its distances to every minority row. Think about computing them all at once as a matrix, and how to read off the smallest `k` values in each row of it. ``` ```hint Equal scores Two majority rows can have exactly the same score, as rows 0 and 3 do in the example. Check that your ranking step always keeps the lower original index, and which sorting methods guarantee that. ``` ### Constraints & Assumptions - The minority is the class with fewer rows. On a count tie, the lower label is the minority. Labels may be arbitrary values. - Equal scores: the lower original index is kept first. - `kept` is an `int64` array sorted ascending (original row order). - Raise `ValueError` if `X` is not 2-D or row counts mismatch, `ratio <= 0`, `k < 1`, or the number of classes is not exactly 2. ### What a Strong Answer Covers - Choosing the minority by count with the lower-label tie rule, for arbitrary label values. - Computing majority-to-minority Euclidean distances and averaging the `min(k, n_minority)` smallest for each majority row. - Keeping `floor(n_minority / ratio)` majority rows, capped at `n_majority`, with score ties broken by lower original index. - Returning every minority row plus the kept majority rows as an ascending `int64` index array, with the required `ValueError` checks. - The time and memory cost of the full distance matrix. ### Follow-up Questions - How do NearMiss-2 and NearMiss-3 differ from NearMiss-1? - How would you scale this once the full `n_majority × n_minority` distance matrix no longer fits in memory? - What risk does NearMiss-1 carry on noisy data, and what would you do before running it?

Overview: Implement NearMiss-1 undersampling for a binary dataset, keeping every minority row and the majority rows that sit closest to their nearest minority neighbours. It tests distance computation in NumPy, deterministic tie-breaking, input validation, and how distance-based undersampling behaves on real data.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement near_miss_1(X, y, k=3, ratio=1.0) for a binary dataset. Keep every minority row. Score each majority row by its mean Euclidean distance to its min(k, n_minority) nearest minority rows, and keep the floor(n_minority / ratio) lowest-scoring majority rows (capped at n_majority). Return (X[kept], y[kept], kept).

Example:

X = [[0,1],[0,0],[5,5],[1,1],[1,0],[10,0],[0.5,0.2],[3,0]]
y = [0, 1, 0, 0, 1, 0, 0, 0]
near_miss_1(X, y, k=2, ratio=1.0)
# minority = 1 (rows 1, 4), keep 2 majority rows
# scores: row6=0.539, row0=1.207, row3=1.207 (tie -> lower index)
# kept = [0, 1, 4, 6], y[kept] = [0, 1, 1, 0]

Class 1 has 2 rows, so it is the minority and floor(2 / 1.0) = 2 majority rows survive. Row 6 has the lowest score. Rows 0 and 3 tie, and row 0 wins because its index is lower.

Constraints & Assumptions

  • The minority is the class with fewer rows. On a count tie, the lower label is the minority. Labels may be arbitrary values.
  • Equal scores: the lower original index is kept first.
  • kept is an int64 array sorted ascending (original row order).
  • Raise ValueError if X is not 2-D or row counts mismatch, ratio <= 0 , k < 1 , or the number of classes is not exactly 2.

What a Strong Answer Covers Guidance

  • Choosing the minority by count with the lower-label tie rule, for arbitrary label values.
  • Computing majority-to-minority Euclidean distances and averaging the min(k, n_minority) smallest for each majority row.
  • Keeping floor(n_minority / ratio) majority rows, capped at n_majority , with score ties broken by lower original index.
  • Returning every minority row plus the kept majority rows as an ascending int64 index array, with the required ValueError checks.
  • The time and memory cost of the full distance matrix.

Follow-up Questions Guidance

  • How do NearMiss-2 and NearMiss-3 differ from NearMiss-1?
  • How would you scale this once the full n_majority × n_minority distance matrix no longer fits in memory?
  • What risk does NearMiss-1 carry on noisy data, and what would you do before running it?