Radius Neighbours Classifier With Outlier Fallback
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `RadiusKNNClassifier` with `fit(X, y)`, `radius_neighbors`, `predict_proba` and `predict`. A query votes using every training point within Euclidean distance `radius` (inclusive: `d <= radius`). Queries with no neighbours use a fallback.
Requirements:
- `radius_neighbors(Q)` returns `(dists, inds)`: lists of per-query arrays sorted by ascending distance (ties by training index).
- `weights` is `"uniform"` or `"distance"` (weight `1/d`, but if any neighbour has `d == 0`, only those count, equally). Anything else raises `ValueError` in `__init__`.
- `predict_proba` returns shape `(n_queries, n_classes)`, columns in sorted `classes_` order, holding the normalized vote weights.
- `predict` returns the class with the highest vote; ties go to the smallest label.
- With no neighbours and `outlier_label` set, predict that label and return a one-hot proba on its column (all zeros if it is not a training class). Otherwise predict the global majority class (ties go to the smallest label) and return the training class priors.
Example:
```python
X = [[-1.0], [1.0], [5.0]]; y = [5, 3, 3]
clf = RadiusKNNClassifier(radius=1.0).fit(X, y)
clf.predict([[0.0]]) # [3] (1-1 tie -> smallest label)
clf.predict_proba([[0.0]]) # [[0.5, 0.5]] columns: [3, 5]
clf = RadiusKNNClassifier(radius=0.6, outlier_label=99).fit([[0],[1],[2],[3]], [0,1,1,2])
clf.predict([[100.0]]) # [99]
clf.predict_proba([[100.0]]) # [[0.0, 0.0, 0.0]]
```
```hint Class sums
Map each training label to its column in the sorted classes once, at fit time. A query's votes are then a weighted count over those column indices.
```
```hint Zero distances and empty rows
With distance weights, a training point equal to the query gives 1/0, and a query with no neighbours gives an empty sum. Trace both through your normalization step before relying on it.
```
### What a Strong Answer Covers
- An inclusive radius search whose output keeps training-index order for equal distances
- Both weighting schemes, including the exact-match rule that avoids dividing by zero
- Per-class vote sums aligned with sorted `classes_`, with `predict` and `predict_proba` agreeing on every row
- Both fallbacks for empty neighbourhoods, including an outlier label that is not a training class
- The time and memory cost of the brute-force distance computation
### Follow-up Questions
- How would you scale `radius_neighbors` to millions of training points?
- How would you choose `radius`, especially when feature scales differ?
- When is the outlier fallback preferable to the majority-class fallback?
Overview: Asks for a radius-based nearest neighbour classifier in NumPy with uniform or inverse-distance voting, class probabilities and a fallback for queries that have no neighbours. Tests inclusive range search, deterministic tie-breaking, the zero-distance weighting rule and outlier handling.