Random Forest Proximity-Based Outlier Scores
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Given an `n × n` forest proximity matrix and `n` class labels, implement `raw_outlyingness`, which returns `1 / Σ_j P[i,j]²` over samples `j ≠ i` with the same label as `i`. Also implement `proximity_outlier_scores`, which standardizes raw values within each class as `(raw - median) / MAD`, where `MAD = median(|raw - median|)` (no scaling constant). Both return a float array of shape `(n,)`.
```hint Selecting the pairs
Each pair that counts toward a row sum meets one condition on the labels and one on the indices. Build a boolean matrix for both at once with broadcasting so you avoid a Python double loop.
```
```hint Two different zeros
Both the squared-proximity sum and the MAD can be zero. Check the constraints for which one is an error and which one gets a fallback value.
```
### Example
```python
P = [[1.0, 0.5, 0.4, 0.1, 0.0],
[0.5, 1.0, 0.3, 0.0, 0.2],
[0.4, 0.3, 1.0, 0.0, 0.0],
[0.1, 0.0, 0.0, 1.0, 0.8],
[0.0, 0.2, 0.0, 0.8, 1.0]]
labels = [0, 0, 0, 1, 1]
raw_outlyingness(P, labels)
# [100/41, 50/17, 4.0, 1.5625, 1.5625] (about [2.439, 2.941, 4.0, 1.5625, 1.5625])
proximity_outlier_scores(P, labels)
# [-1.0, 0.0, 738/350, 0.0, 0.0] (738/350 is about 2.109)
# class 1 has MAD 0, so MAD is replaced by 1
```
Sample 0 shares class 0 with samples 1 and 2, so its sum is `0.5² + 0.4² = 0.41` and its raw value is `1 / 0.41 = 100/41`.
### Constraints & Assumptions
- `proximity` is an `n × n` array-like and `labels` has one entry per row.
- The diagonal and cross-class entries are ignored.
- If a class's MAD is `0`, use `1` instead.
- Raise `ValueError` if any sample's within-class squared-proximity sum is `0` (including singleton classes), if `proximity` is not square, or if `labels` is not 1-D with length `n`.
- Labels may be any comparable type (for example ints or strings). Proximities are finite and non-negative.
### What a Strong Answer Covers
- A vectorized mask that keeps same-class pairs, drops the diagonal, and is applied to the squared proximities.
- Input validation and the zero-sum error, including the singleton-class case.
- Per-class median and MAD with the zero-MAD fallback, with scores written back in the original sample order.
- Time and memory cost in terms of `n` and the number of classes.
- Why a large raw value signals an outlier, and why the median and MAD suit this standardization.
### Follow-up Questions
- How do you build the proximity matrix from a fitted forest?
- The full proximity matrix is `O(n²)` memory. How would you scale this?
- Why standardize with the median and MAD instead of the mean and standard deviation?
Overview: Implement two NumPy functions that turn a random forest proximity matrix into outlier scores: the inverse of each sample's within-class sum of squared proximities, and a per-class median and MAD standardization of those values. It tests vectorized masking, input validation and edge cases such as singleton classes and a zero MAD.