Compute sample-weighted log loss and accuracy for a fitted logistic regression
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `weighted_logistic_scores(X, y, sample_weight)`. It fits `LogisticRegression(solver='lbfgs', max_iter=1000, tol=1e-6, C=1.0, class_weight=None, random_state=0)` on `X` and the binary labels `y`, then returns a dict of two metrics weighted by `sample_weight`.
The function must:
- Not pass `sample_weight` to `fit`. The weights are used for scoring only.
- Return `'weighted_log_loss'`: `-sum(w_i * [y_i*log(p_i) + (1-y_i)*log(1-p_i)]) / sum(w_i)`, with `p_i` taken from `predict_proba(X)[:, 1]`.
- Return `'weighted_accuracy'`: `sum(w_i * 1[pred_i == y_i]) / sum(w_i)`.
- Match `log_loss(..., sample_weight=w)` and `accuracy_score(..., sample_weight=w)` within `rtol=1e-5`.
- Return both values as Python `float`, with no other keys in the dict.
```hint Weighted means
Write out what `np.average(values, weights=w)` computes and compare its denominator with yours. Then ask what `log` returns for a probability of exactly 0.
```
### Constraints & Assumptions
- `X` is a float array of shape `(n, 2)`.
- `y` is a 1-D array of binary labels of length n.
- `sample_weight` is a 1-D float array of shape `(n,)`.
- Results are compared with the sklearn metrics at `rtol=1e-5`.
### What a Strong Answer Covers
- Fitting with the exact hyperparameters given while keeping the weights out of `fit`.
- The normalization of both weighted metrics.
- Keeping the log loss finite when a predicted probability reaches 0 or 1, in a way that stays consistent with sklearn.
- Class predictions that agree with `predict`, including at the 0.5 boundary.
- The return contract: exactly two keys, both Python floats.
### Follow-up Questions
- Why might you clip `p_i` before taking `log`?
- How does passing `sample_weight` to `fit` differ from using it only for scoring?
Overview: Fit a scikit-learn logistic regression and compute weighted log loss and weighted accuracy by hand, using sample weights only for scoring. It tests how weighted metrics are normalized, how to keep the log loss finite, and how weighting the evaluation differs from weighting the fit.