ADASYN Oversampling From Scratch in NumPy
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Given a binary dataset `X` (`n × d`) and labels `y`, implement ADASYN in three NumPy functions. The minority class is the label with the smaller count (any label values). All distances are Euclidean, and neighbour ties break by lower row index.
- `adasyn_ratios(X, y, k=5)`: for each minority row (original order), take its `k` nearest neighbours among all other rows and set `r_i = (#majority neighbours) / k`, normalised to sum 1. If every `r_i` is 0, return uniform `1 / n_min`.
- `adasyn_sample_counts(X, y, beta=1.0, k=5)`: `G = floor((n_maj - n_min) * beta)`, `g = floor(r_hat * G)`. Give leftover units to the largest fractional parts (ties go to the lower index). Returns an int array summing to `G`.
- `adasyn(X, y, beta=1.0, k=5, seed=0)`: use `rng = np.random.default_rng(seed)` and the `k_min = min(k, n_min - 1)` nearest minority-only neighbours (`nn[i]` holds positions within the minority rows, nearest first). For each minority row `i` in order, repeat `g[i]` times: `j = nn[i, rng.integers(k_min)]`, `gap = rng.random()`, emit `x_i + gap * (x_j - x_i)`.
- Return `(X_res, y_res)`: the original rows first, then the synthetic rows labelled with the minority label. If the total count is 0, return copies of the inputs.
- Raise `ValueError` when the number of classes is not 2, `k < 1` or `k > n - 1`, `beta < 0`, or samples are needed but `n_min < 2`.
Example:
```python
X = [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], # class 0
[0, 1], [10, 10], [11, 10]] # class 1 (minority)
y = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
adasyn_ratios(X, y, k=2) # -> [0.5, 0.25, 0.25]
adasyn_sample_counts(X, y, 1.0, k=2) # -> [2, 1, 1] (G = 4)
adasyn_sample_counts(X, y, 0.5, k=2) # -> [1, 1, 0] (G = 2, tie goes to the lower index)
adasyn(X, y, 1.0, k=2, seed=3)[0].shape # -> (14, 2)
```
```hint Two different neighbour sets
The difficulty ratio uses neighbours among all rows, and the interpolation uses neighbours among minority rows only. Keep the two searches separate, and make sure a point never counts as its own neighbour.
```
```hint Deterministic ties
Check how your sort orders equal distances. A default argsort or an argpartition does not promise to keep equal values in index order.
```
```hint Rounding to an exact total
Flooring each share loses part of a unit per row. When you hand the missing units back, ask whether floating-point fractional parts can make two remainders that are equal on paper compare as unequal.
```
### Constraints & Assumptions
- `n` is at most about 500, features are finite floats, and `len(X) == len(y)`.
- NumPy only (no scikit-learn or imbalanced-learn).
- Labels can be any values that `np.unique` can sort. If both classes have the same count, the label that sorts first is treated as the minority, and `G` is then 0.
### What a Strong Answer Covers
- Difficulty ratios from neighbours over all rows with the point itself excluded, the lower-index tie rule, normalisation and the uniform fallback.
- Sample counts that sum exactly to `G` through largest-remainder rounding with the lower-index tie rule.
- Generation from minority-only neighbours with `k_min = min(k, n_min - 1)` and the exact RNG call order, so a seed reproduces the output.
- Every listed `ValueError` case, and the zero-sample path that returns copies.
- The time and memory cost of the brute-force distance computations.
### Follow-up Questions
- How would you scale the neighbour search beyond the O(n²) distance matrix?
- What failure mode does ADASYN have with noisy minority points, and how would you mitigate it?
- How would you extend this to multiclass or categorical features?
Overview: Implement ADASYN oversampling for an imbalanced binary dataset using only NumPy, from difficulty ratios to per-point sample counts and seeded interpolation. It tests nearest-neighbour logic, exact rounding to a fixed total, deterministic tie-breaking and input validation.