Implement a label powerset classifier for multi-label classification
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Complete `LabelPowerset` so that it maps each distinct 0/1 label row of Y to one compound class, trains a single multi-class estimator on those codes, and decodes predictions back to label vectors. The estimator comes from `estimator_factory()`, which defaults to `LogisticRegression(random_state=0, max_iter=1000)`.
Requirements:
- `fit`: set `classes_` to the sorted unique rows (`np.unique(Y, axis=0)`), with code = row index. Call the factory once and fit it on the codes. Raise `ValueError` for non-2-D input, a row-count mismatch or non-0/1 Y.
- `transform` returns int codes, with `-1` for unseen label sets. `inverse_transform` raises `ValueError` for any code outside `[0, k)`.
- `predict_compound_proba` returns shape `(n, k)`, with columns placed using the estimator's `classes_` order (absent codes get 0).
- `predict` decodes the argmax (ties go to the lowest index). `predict_proba` returns per-label marginals `P @ classes_`.
- With a single compound class, do not fit an estimator. Compound proba is `ones((n, 1))` and `predict` returns that constant row.
Example:
```python
Y = [[1, 0, 1], [0, 1, 0], [1, 0, 1], [0, 0, 0], [0, 1, 0]]
lp = LabelPowerset().fit(X, Y) # X is any feature matrix with 5 rows
lp.classes_ # [[0, 0, 0], [0, 1, 0], [1, 0, 1]]
lp.transform(Y) # [2, 1, 2, 0, 1]
lp.transform([[1, 1, 1]]) # [-1]
# For a row whose compound proba is [0.2, 0.5, 0.3]:
# predict -> [0, 1, 0]
# predict_proba -> [0.3, 0.5, 0.3]
```
```hint Encoding and lookup
NumPy can return the unique rows and each input row's index into them in a single call. For `transform`, think about what hashable key a label row can become so unseen rows are easy to detect.
```
```hint Probabilities
Check which attribute tells you the order of the estimator's probability columns, and write a label's marginal probability as a sum over the compound classes that contain it.
```
### Constraints & Assumptions
- `X` has shape (n, d) and `Y` has shape (n, L) with every entry equal to 0 or 1.
- k is the number of distinct label rows seen during `fit`.
- Factory estimators expose `fit(X, y)`, `predict_proba(X)` and `classes_`.
- Use NumPy and scikit-learn.
### What a Strong Answer Covers
- Input validation in `fit` and the sorted unique-row encoding, including the single-class case that skips the estimator.
- `transform` and `inverse_transform` behavior for unseen label sets and out-of-range codes.
- Placing the estimator's probability columns by its `classes_` rather than by position.
- Argmax decoding with lowest-index ties, and why `P @ classes_` gives the per-label marginals.
- Cost of encoding, prediction and marginals in terms of n, k and L.
### Follow-up Questions
- How does label powerset scale as the number of labels grows?
- The model can never predict an unseen label combination. When is this a problem?
- Which metrics would you use to evaluate this classifier?
Overview: Implement a label powerset classifier that turns each distinct 0/1 label row into one compound class, trains a single multi-class model on the codes, and decodes predictions back to label vectors. It tests NumPy encoding, estimator column ordering, and computing per-label marginals from compound probabilities.