Binary Relevance Multi-Label Wrapper
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `BinaryRelevance`, which fits one independent clone of `base_estimator` per column of a 2-D label indicator matrix `Y`. It exposes per-label probabilities and a thresholded 0/1 prediction matrix. The constructor is `BinaryRelevance(base_estimator=None, threshold=0.5)`.
Requirements:
- `fit(X, Y)` returns `self` and sets `estimators_` (length `n_labels`): a distinct fitted `clone` per label, default `LogisticRegression(random_state=0)`.
- A label column that is constant in training gets `None` in `estimators_` and always predicts its constant (proba `1.0` or `0.0`).
- `predict_proba(X)` returns a float array `(n_samples, n_labels)` of P(label = 1), using the column of `classes_` equal to `1`.
- `predict(X)` returns an int 0/1 matrix: `1` where proba `>= threshold` (inclusive).
- Non-2-D `Y` or a row-count mismatch with `X` raises `ValueError`.
Example:
```python
X = [[0.2], [0.5], [0.7], [0.9]]
Y = [[0, 1], [1, 0], [0, 1], [1, 0]]
# base estimator whose P(1) == X[:, 0]
BinaryRelevance(est, threshold=0.5).fit(X, Y).predict(X)
# [[0, 0], [1, 1], [1, 1], [1, 1]]
BinaryRelevance(est, threshold=0.7).fit(X, Y).predict(X)
# [[0, 0], [0, 0], [1, 1], [1, 1]]
```
```hint One model per label
Think about what happens if the loop fits the same estimator object for every column. sklearn's `clone` returns an unfitted copy with the same parameters.
```
### Constraints & Assumptions
- `X` has shape `(n_samples, n_features)` and `Y` has shape `(n_samples, n_labels)` with 0/1 entries.
- With the default estimator and the default threshold of 0.5, predictions must match `sklearn`'s `MultiOutputClassifier(LogisticRegression(random_state=0))` on label columns that contain both classes in training.
### What a Strong Answer Covers
- A fresh `clone` per label, so the fitted estimators are distinct and the caller's base estimator stays unfitted.
- Handling of label columns that are constant in training, in both `fit` and `predict_proba`.
- Selecting the positive-class probability through `classes_` and stacking the results into an `(n_samples, n_labels)` array.
- The inclusive threshold, the integer output type and the `ValueError` checks.
- The cost of training and prediction as the number of labels grows.
### Follow-up Questions
- What does Binary Relevance ignore, and how would you address it?
- How would you choose a per-label threshold instead of a global one?
- How would you train the per-label models in parallel, and what would you watch for?
Overview: Implement a binary relevance wrapper that trains one scikit-learn classifier per label column of a multi-label indicator matrix and returns per-label probabilities and thresholded predictions. It tests estimator cloning, handling of constant label columns, class lookup and input validation.