Weighted Rank Averaging With Ties
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `rank_average(scores, weights=None)` where `scores` has shape `(n_members, n_samples)`. Rank each row 1 to N (tied values get the average of their positions), normalize via `(rank - 1) / (N - 1)`, and return the weighted mean of normalized ranks per sample as a 1-D array of length `n_samples`.
For example:
```python
scores = [[5.0, 1.0, 5.0, 3.0]]
rank_average(scores)
# ranks [3.5, 1, 3.5, 2] -> [0.8333, 0.0, 0.8333, 0.3333]
rank_average([[4, 2, 9], [1, 2, 3]], [1.0, 0.0])
# -> [0.5, 0.0, 1.0]
```
```hint Group the ties
Sort each row once, then handle each run of equal values as a group instead of ranking elements one by one.
```
### Constraints & Assumptions
- `weights` defaults to equal weights, otherwise one weight per member, normalized to sum to 1.
- Any negative weight or a non-positive weight sum raises `ValueError`.
- If `n_samples == 1`, every normalized rank is `0.5`.
- A higher score gets a higher rank. Scores are finite.
### What a Strong Answer Covers
- Average ranks for ties that stay correct for runs longer than two and for ties at either end of the order.
- The scaling to `[0, 1]` and the single-sample case where `N - 1` is zero.
- Weight validation and normalization, including what a zero-weight member contributes.
- Time and space complexity, and whether the per-row ranking avoids an element-by-element Python loop.
### Follow-up Questions
- Why rank-average instead of averaging raw probabilities?
- How would you score new test samples one at a time?
Overview: Implement weighted rank averaging in NumPy: rank each model's scores with average ranks for ties, scale the ranks to the range 0 to 1, and blend them with normalized weights. It tests tie handling, edge cases such as a single sample, weight validation and when rank blending beats averaging raw scores.