Reconstruct logistic regression predict labels from predict_proba and classes_
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `predict_from_proba(X, y, X_new)`, where `y` holds three string labels. Fit the model, then reconstruct `predict` from `predict_proba(X_new)` and return a 1-D ndarray of the original string labels.
Requirements:
- Fit `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)` on `X`, `y` (no `multi_class`).
- Return `classes_[np.argmax(proba, axis=1)]` with `proba = model.predict_proba(X_new)`, shape `(X_new.shape[0],)`.
- Do not call `model.predict`. The result must match it under `assert_array_equal`.
For example:
```python
proba = model.predict_proba(X_new) # shape (n, 3)
# columns align with SORTED classes_: ['bird', 'cat', 'dog']
idx = np.argmax(proba, axis=1) # e.g. [1, 2, 0]
labels = model.classes_[idx] # ['cat', 'dog', 'bird']
```
```hint Test it with a trap
Train on labels whose first-appearance order in y differs from alphabetical order, then compare your output with model.predict on a few rows.
```
### What a Strong Answer Covers
- Fitting the model with the exact hyperparameters given
- How the columns of `predict_proba` map to labels, and the order of `classes_`
- Why `predict` and the argmax over probabilities agree row by row
- Returning string labels of shape `(n_new,)` without calling `predict`
### Follow-up Questions
- Why can't you use the argmax index directly as the label?
- How does this mapping change for binary classification?
- How would you change the decision rule if misclassifying one class costs more than the others?
Overview: Fit a multiclass logistic regression on string labels and rebuild its predict output from predict_proba without calling predict. It tests how probability columns map to the sorted classes_ array and why an argmax index is a column position rather than a label.