Label Noise Versus kNN Capacity: Zero-One Loss Bias-Variance Experiment
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement four NumPy functions that inject binary label noise, fit a from-scratch kNN classifier, and decompose zero-one loss over repeated refits:
```python
def flip_labels(y, rate, rng): ...
def knn_predict(X_train, y_train, X_test, k): ...
def zero_one_decomposition(preds, y_clean, noise_rate): ...
def label_noise_experiment(X_pool, y_pool, X_test, y_test_clean, noise_rates,
ks, n_refits, n_train, seed): ...
```
`label_noise_experiment` returns a dict with keys `expected_error`, `bias`, `variance`, `net_variance`, `noise`. Each value is an array of shape `(len(noise_rates), len(ks))`.
Requirements:
- `flip_labels` draws `u = rng.random(len(y))` once and flips each label where `u < rate`. It returns a new int array and does not mutate `y`.
- `knn_predict` uses Euclidean distance and a stable argsort, so distance ties go to the lower training index. It predicts `1` if and only if the mean vote is `> 0.5`, so a vote tie gives `0`.
- `zero_one_decomposition` takes `preds` of shape `(refits, n)`. Write $\eta$ for `noise_rate`.
- The main prediction is `mean > 0.5` per column (a tie gives `0`).
- `bias` is the fraction of test points where the main prediction differs from the clean label. `variance` is the mean disagreement of each refit with the main prediction.
- The clean error is the fraction of all entries of `preds` that differ from the clean label, and `net_variance = clean_error - bias`.
- `noise` is $\eta$ and `expected_error` is $\eta + (1 - 2\eta) \cdot \text{clean error}$. The function returns a dict with the same five keys, every value a float.
- `label_noise_experiment` creates one `rng = np.random.default_rng(seed)`.
- It loops over rates, then over refits. In each refit it calls `rng.choice(n_pool, n_train, replace=False)`, then `flip_labels` on that subset's labels, then predicts for every `k` on the same training set.
- It decomposes each set of predictions (one per rate and k) against `y_test_clean`, with that rate as $\eta$.
Example:
```python
preds = [[1,0,1,0],[1,1,0,0],[0,1,1,0]]; y_clean = [1,1,0,1]; noise_rate = 0.1
zero_one_decomposition(preds, y_clean, noise_rate)
# bias=0.5, variance=0.25, net_variance=1/12, noise=0.1, expected_error=17/30
```
```hint Random stream order
Every call on the shared generator advances it, so the order of the `choice` and `random` calls inside each refit decides whether the grids reproduce.
```
```hint Where net variance comes from
Split the test points by whether the main prediction is right there, and ask what a refit that disagrees with the main prediction does to the error in each group.
```
### Constraints & Assumptions
- Labels are binary (0 or 1). `X_pool` is a 2-D array of shape `(n_pool, d)` and `X_test` is a 2-D array of shape `(m, d)`.
- `n_train` is at most `len(y_pool)` because sampling is without replacement, and every k is between 1 and `n_train`.
- Only the training labels are flipped. `y_test_clean` stays clean, and the test-label noise enters through the formula for `expected_error`.
- `preds` holds 0/1 predictions, one row per refit.
### What a Strong Answer Covers
- Label flipping and kNN prediction that follow the stated tie rules and leave the inputs unmodified.
- The main prediction, bias, variance and net variance, all measured against the clean labels.
- Where $\eta + (1 - 2\eta) \cdot \text{clean error}$ comes from, and why the noise floor is $\eta$ for every k.
- A seeded experiment loop that makes its draws in the stated order and fills the `(len(noise_rates), len(ks))` grids.
- How the grids should move with noise rate and k.
### Follow-up Questions
- The squared-error decomposition just adds the noise variance. Why does zero-one loss under symmetric label flips give $\eta + (1 - 2\eta) \cdot \text{clean error}$ instead?
- Why can `net_variance` be negative?
- How would you pick `k` when the noise rate is unknown?
Overview: Write NumPy code that flips binary training labels, fits a from-scratch kNN classifier and splits zero-one loss into bias, variance, net variance and noise across many refits. It tests seeded experiment design, tie rules and how label noise interacts with model capacity.