Reproduce scikit-learn logistic regression predict from decision_function and classes_
Quick Overview
Fit a binary scikit-learn logistic regression and reproduce the labels predict returns using only decision_function scores and classes_. It tests how linear classifiers turn a score into a class, how a score of exactly zero is resolved, and safe NumPy indexing into the class array.
Reproduce scikit-learn logistic regression predict from decision_function and classes_
Role: Data Scientist
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `predict_via_decision_function(X_train, y_train, X_test)`: fit a binary scikit-learn `LogisticRegression` and return exactly what `model.predict(X_test)` would return, using only `model.decision_function(X_test)` and `model.classes_`.
Requirements:
- Fit with `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`.
- Do not call `predict` or `predict_proba`.
- Use strict `> 0` thresholding, then map via `classes_[indices]`: a score of exactly `0` yields `classes_[0]`.
- Return a 1-D `np.ndarray` of shape `(n_test,)` with labels drawn from `classes_`.
Example:
```python
scores = model.decision_function(X_test) # e.g. [-1.2, 0.0, 3.4]
indices = (scores > 0).astype(int) # [0, 0, 1]
labels = model.classes_[indices] # -> shape (3,)
```
```hint Indexing pitfall
NumPy treats a boolean array and an integer array very differently when you use them to index. Check which one you pass to `classes_`.
```
### Constraints & Assumptions
- `y_train` contains only the integer labels 0 and 1.
- `decision_function` returns a 1-D array in the binary case.
- `X_train` and `X_test` share the same features in the same order.
### What a Strong Answer Covers
- Fitting the estimator with exactly the pinned hyperparameters.
- What the decision function score means for a binary model and which class a positive score selects.
- The strict threshold and how a score of exactly 0 is resolved.
- Mapping indices through `classes_` so the output matches `predict` in labels, shape and dtype.
### Follow-up Questions
- How does this generalize to the multiclass case?
- Why is thresholding the score at 0 equivalent to thresholding the probability at 0.5?
- How would you predict with a probability cutoff other than 0.5, still using only the scores?
Overview: Fit a binary scikit-learn logistic regression and reproduce the labels predict returns using only decision_function scores and classes_. It tests how linear classifiers turn a score into a class, how a score of exactly zero is resolved, and safe NumPy indexing into the class array.
Reproduce scikit-learn logistic regression predict from decision_function and classes_
General
Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0
Implement predict_via_decision_function(X_train, y_train, X_test): fit a binary scikit-learn LogisticRegression and return exactly what model.predict(X_test) would return, using only model.decision_function(X_test) and model.classes_.
Requirements:
Fit with
penalty='l2'
,
C=1.0
,
solver='lbfgs'
,
max_iter=1000
,
tol=1e-6
,
fit_intercept=True
.
Do not call
predict
or
predict_proba
.
Use strict
> 0
thresholding, then map via
classes_[indices]
: a score of exactly
0
yields
classes_[0]
.
Return a 1-D
np.ndarray
of shape
(n_test,)
with labels drawn from
classes_
.