Tomek Link Detection and Removal
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `find_tomek_links(X, y)`, returning every pair `(i, j)` with `i < j` whose two points are each other's nearest neighbour (Euclidean, self excluded) and have different labels. Then implement `remove_tomek_links(X, y, strategy="majority")`, returning `(X_resampled, y_resampled, removed_indices)`.
Example:
```python
X = [[0.0], [1.0], [3.0], [3.5], [10.0], [10.4], [20.0]]
y = [0, 0, 0, 1, 0, 1, 0]
find_tomek_links(X, y) -> [[2, 3], [4, 5]]
remove_tomek_links(X, y) -> removed [2, 4], y_resampled [0, 0, 1, 1, 0]
remove_tomek_links(X, y, "both") -> removed [2, 3, 4, 5], y_resampled [0, 0, 0]
```
Points 2 and 3 (distance 0.5) and points 4 and 5 (distance 0.4) are mutual nearest neighbours with different labels. Points 0 and 1 are also mutual neighbours but share a label, and point 6's nearest neighbour is 5, whose own nearest neighbour is 4. Label 1 is the minority (2 rows against 5), so the default strategy removes only the label-0 members, rows 2 and 4.
```hint Self-matches
Every point is at distance 0 from itself. Deal with that before you look for each point's nearest neighbour.
```
```hint Checking reciprocity
Once you have an array holding each point's nearest-neighbour index, think about what indexing that array with itself gives you.
```
### Constraints & Assumptions
- `X` is `(n, d)` or 1-D (one feature). Nearest-neighbour ties go to the lowest index.
- Links are returned as an `int64` array of shape `(m, 2)`, sorted by `i`. No links, or `n < 2`, gives shape `(0, 2)`.
- `"majority"` removes the link members that are not of the minority label. The minority is the label with the fewest samples, with ties going to the lowest label. `"both"` removes all link members.
- `removed_indices` is a sorted, unique `int64` array. Kept rows preserve their original order.
- Any other `strategy` raises `ValueError`.
### What a Strong Answer Covers
- A vectorised pairwise distance computation that excludes self-matches and breaks ties toward the lowest index.
- The mutual nearest-neighbour test, reporting each pair once with `i < j` and only when the labels differ.
- The two removal strategies, including the minority definition and its tie rule.
- The exact output contract: dtypes, the empty `(0, 2)` shape, sorted unique removed indices and order-preserving filtering.
- The time and memory cost of the full distance matrix.
### Follow-up Questions
- The pairwise distance matrix is ${O(n^2)}$ memory. How would you scale this?
- Why is a single Tomek pass often insufficient, and what would you do?
- How should feature scaling affect Tomek link detection?
Overview: Implement Tomek link detection by finding pairs of points that are mutual nearest neighbours with different labels, then remove link members with a majority-only or a remove-both strategy. It tests vectorised NumPy distance code, tie-breaking rules and how undersampling cleans class boundaries in imbalanced data.