Class-Balanced KNN Voting for Minority Recall
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement a brute-force Euclidean KNN classifier whose neighbor votes are either uniform or reweighted by inverse class frequency. Then report minority-class recall under both schemes.
Requirements:
- `class_weights(y_train)` returns `(classes, weights)`: the sorted unique classes and their weights `w_c = n / (n_classes * count_c)`.
- `kneighbors(X_train, X_query, k)` returns `(distances, indices)` of shape `(m, k)`, ascending by distance, with equal distances going to the lower training index. Raise `ValueError` unless `1 <= k <= n_train`.
- `knn_predict(X_train, y_train, X_query, k, weighting)`: a class's score is its neighbor count times its weight (all weights are 1 for `"uniform"`, the class weights for `"balanced"`). Tied top scores go to the smallest label. Any other `weighting` value raises `ValueError`.
- `minority_recall_comparison(X_train, y_train, X_test, y_test, k, minority_label)` returns `{"uniform": float, "balanced": float}`, the recall of `minority_label` on the test set under each scheme.
- If `minority_label` is absent from `y_test`, raise `ValueError`.
Example:
```python
X = [[0], [1], [2], [3], [4], [5], [6], [7], [10], [11]]
y = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # weights: w0 = 0.625, w1 = 2.5
kneighbors(X, [[8.5]], 5)
# distances [[1.5, 1.5, 2.5, 2.5, 3.5]], indices [[7, 8, 6, 9, 5]]
knn_predict(X, y, [[8.5]], 5, "uniform") # [0] (3 votes vs 2)
knn_predict(X, y, [[8.5]], 5, "balanced") # [1] (1.875 vs 5.0)
```
```hint Ties at the k-th distance
Several training points can share a distance, including the k-th one. Think about which kind of sort keeps them in index order without extra work.
```
```hint Comparing weighted scores
Balanced scores are floats. Decide how you will recognize two classes that tie mathematically but differ in the last bit.
```
### Constraints & Assumptions
- Inputs are NumPy arrays: each `X_*` is 2-D numeric, and labels are 1-D sortable values.
- A training point is its own neighbor at distance 0.
- Predictions keep the dtype of `y_train`.
### What a Strong Answer Covers
- Balanced weights computed from the training counts over sorted classes, and what they do to each class's total vote mass.
- A neighbor search that honors the tie rule at the k-th distance, plus the check on `k`.
- Score computation and tie-breaking toward the smallest label, with a plan for floating-point near-ties.
- Recall computed for the minority label only, with the error for a label missing from `y_test`.
- The time and memory cost of brute-force search.
### Follow-up Questions
- How does balanced weighting change precision and recall on the majority and minority classes?
- How would you combine class weights with distance weighting?
- How would this scale to a large training set?
Overview: Implement a brute-force Euclidean k-nearest-neighbor classifier whose votes are either uniform or weighted by inverse class frequency, then compare minority-class recall under both schemes. It tests balanced class weights, deterministic tie-breaking, and the recall trade-off on imbalanced data.