Binary Logistic Regression Margin Scores

Quick Overview

Implement a function that fits a binary scikit-learn logistic regression with pinned hyperparameters and returns the raw log-odds score for each test row. It tests whether you can tell decision scores from probabilities and know the shape of the binary output.

Binary Logistic Regression Margin Scores

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def margin_scores(X_train: np.ndarray, y_train: np.ndarray, X_test: np.ndarray) -> np.ndarray` that fits a binary `LogisticRegression` and returns the raw log-odds `z = Xw + b` for each row of `X_test`, exactly `model.decision_function(X_test)`. Requirements: - Pin the estimator to `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`. - Return a 1-D `np.ndarray` of shape `(n_test,)` with dtype `float64` (not `(n_test, 1)` or `(n_test, 2)`). - Do not apply the sigmoid. Scores are unbounded reals, and a score `> 0` means `classes_[1]` would be predicted. ```hint Check the output shape Before you reshape or slice anything, look at what the fitted model's scoring method returns when there are only two classes. ``` ### Constraints & Assumptions - `y_train` contains only the integer labels `0` and `1`. - Use `sklearn.linear_model.LogisticRegression`. No scaling or preprocessing is required. ### What a Strong Answer Covers - Builds the estimator with exactly the pinned hyperparameters and fits it on the training data. - Returns the scores from before the sigmoid, without going through `predict_proba`. - Explains why the binary output is 1-D with shape `(n_test,)` and dtype `float64`. - States what the sign of a score means in terms of `classes_`. - Relates the scores to the fitted `coef_` and `intercept_`. ### Follow-up Questions - How would you convert these scores into calibrated probabilities? - Why is `decision_function` 1-D for a binary problem but 2-D for multiclass?

Overview: Implement a function that fits a binary scikit-learn logistic regression with pinned hyperparameters and returns the raw log-odds score for each test row. It tests whether you can tell decision scores from probabilities and know the shape of the binary output.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def margin_scores(X_train: np.ndarray, y_train: np.ndarray, X_test: np.ndarray) -> np.ndarray that fits a binary LogisticRegression and returns the raw log-odds z = Xw + b for each row of X_test, exactly model.decision_function(X_test).

Requirements:

  • Pin the estimator to penalty='l2' , C=1.0 , solver='lbfgs' , max_iter=1000 , tol=1e-6 , fit_intercept=True .
  • Return a 1-D np.ndarray of shape (n_test,) with dtype float64 (not (n_test, 1) or (n_test, 2) ).
  • Do not apply the sigmoid. Scores are unbounded reals, and a score > 0 means classes_[1] would be predicted.

Constraints & Assumptions

  • y_train contains only the integer labels 0 and 1 .
  • Use sklearn.linear_model.LogisticRegression . No scaling or preprocessing is required.

What a Strong Answer Covers Guidance

  • Builds the estimator with exactly the pinned hyperparameters and fits it on the training data.
  • Returns the scores from before the sigmoid, without going through predict_proba .
  • Explains why the binary output is 1-D with shape (n_test,) and dtype float64 .
  • States what the sign of a score means in terms of classes_ .
  • Relates the scores to the fitted coef_ and intercept_ .

Follow-up Questions Guidance

  • How would you convert these scores into calibrated probabilities?
  • Why is decision_function 1-D for a binary problem but 2-D for multiclass?