Logistic Regression Log-Probability Matrix With predict_log_proba
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `log_proba_matrix(X_train, y_train, X_test)` that fits a binary `LogisticRegression` on the training data and returns `model.predict_log_proba(X_test)`. Return the full 2-D matrix: do not slice a column or exponentiate.
Requirements:
- Pin `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`.
- Return an `np.ndarray` of shape `(n_test, 2)`, dtype `float64`.
- Columns follow `model.classes_`: column 0 is log P(y=0), column 1 is log P(y=1).
- Every entry is `<= 0`, and `np.exp(result).sum(axis=1)` equals 1.0 per row, up to floating-point rounding.
```hint Column order
Check which fitted attribute fixes the order of the columns, and what it contains when the labels are 0 and 1.
```
```hint Checking the invariants
Compare the row sums with a tolerance. Also work out what the smaller probability looks like for a row the model is extremely confident about.
```
### Constraints & Assumptions
- `y_train` contains only integer labels 0 and 1.
### What a Strong Answer Covers
- Builds the estimator with exactly the pinned hyperparameters and returns the method's full output, with no column slicing and no exponentiation.
- Explains how the column order follows `classes_` and what each column holds.
- Explains why every entry is at most 0 and why the exponentiated rows sum to 1 up to rounding.
- Describes how this estimator computes its log-probabilities and what happens to them for very confident predictions.
### Follow-up Questions
- For `LogisticRegression`, is `predict_log_proba` more numerically stable than `np.log(predict_proba(...))`? How would you get finite log-probabilities for rows the model is extremely confident about?
- How would you generalize this to multiclass without changing the return contract per row?
- How would you compute the test log-loss from this matrix, and what does a `-inf` entry do to it?
Overview: Fit a binary scikit-learn logistic regression and return the full two-column matrix of log-probabilities for the test rows. It tests column order by class, the invariants a log-probability matrix must satisfy, and what happens numerically when the model is extremely confident.