Reconstruct a StandardScaler + LogisticRegression Pipeline's decision_function by Hand

Quick Overview

Fit a StandardScaler and LogisticRegression pipeline, then reproduce its decision_function scores by hand from the fitted scaler and classifier attributes. It tests what a fitted pipeline computes, population versus sample standard deviation, coefficient and intercept shapes, and how scores relate to predicted probabilities.

Reconstruct a StandardScaler + LogisticRegression Pipeline's decision_function by Hand

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `manual_pipeline_scores(X_train, y_train, X_test)`. It fits `Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(...))])` on the training data and reproduces `pipe.decision_function(X_test)` by hand, using `mean_` and `scale_` from `pipe.named_steps['scaler']` and `coef_` and `intercept_` from `pipe.named_steps['clf']`. The labels in `y_train` are binary. ```python def manual_pipeline_scores(X_train: np.ndarray, y_train: np.ndarray, X_test: np.ndarray) -> np.ndarray: ... ``` The function must: - pin `LogisticRegression` to `penalty='l2'`, `C=1.0`, `solver='lbfgs'`, `max_iter=1000`, `tol=1e-6`, `fit_intercept=True`; - not call `decision_function`, `predict`, `predict_proba` or `scaler.transform`; - compute `z = ((X_test - mean_) / scale_) @ coef_.ravel() + intercept_[0]`; - divide by the fitted `scale_`, which is the population (`ddof=0`) standard deviation, never by a standard deviation recomputed with `ddof=1`; - return a 1-D `float64` array of shape `(n_test,)`. Example: ```python scores = manual_pipeline_scores(X_train, y_train, X_test) # np.allclose(scores, pipe.decision_function(X_test)) -> True # scores.shape == (X_test.shape[0],) ``` ```hint Use what the scaler stored The pipeline transforms test rows with statistics it learned at fit time. Find the attribute that holds the divisor and check how it was computed. ``` ```hint Shapes and constant columns Look at the shapes of `coef_` and `intercept_` in the binary case. Then ask what the scaler stores for a feature whose training variance is zero. ``` ### Constraints & Assumptions - `X_train` has shape `(n_train, n_features)` and `X_test` has shape `(n_test, n_features)` with the same columns. - `y_train` contains both labels 0 and 1. - The match is checked with `np.allclose` against the pipeline's own `decision_function`. ### What a Strong Answer Covers - Fitting the pipeline with the pinned hyperparameters and reading the four fitted attributes through `named_steps`. - Reproducing the standardization with the stored `mean_` and `scale_`, broadcast over the test rows. - The binary shapes of `coef_` and `intercept_` and how they lead to an `(n_test,)` output. - Why a `ddof=1` standard deviation, or `np.sqrt(var_)` on a constant feature, breaks the match. - What the score means and how it relates to `predict_proba` and `predict`. ### Follow-up Questions - Why does dividing by a `ddof=1` standard deviation break the match, and by how much? - How would you extend this to a multiclass `decision_function`? - How would you turn these scores into the probabilities `predict_proba` returns? - How would you fold the scaler into one raw-scale weight vector and intercept so scoring takes a single dot product?

Overview: Fit a StandardScaler and LogisticRegression pipeline, then reproduce its decision_function scores by hand from the fitted scaler and classifier attributes. It tests what a fitted pipeline computes, population versus sample standard deviation, coefficient and intercept shapes, and how scores relate to predicted probabilities.

Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement manual_pipeline_scores(X_train, y_train, X_test). It fits Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(...))]) on the training data and reproduces pipe.decision_function(X_test) by hand, using mean_ and scale_ from pipe.named_steps['scaler'] and coef_ and intercept_ from pipe.named_steps['clf']. The labels in y_train are binary.

def manual_pipeline_scores(X_train: np.ndarray, y_train: np.ndarray,
                           X_test: np.ndarray) -> np.ndarray:
    ...

The function must:

  • pin LogisticRegression to penalty='l2' , C=1.0 , solver='lbfgs' , max_iter=1000 , tol=1e-6 , fit_intercept=True ;
  • not call decision_function , predict , predict_proba or scaler.transform ;
  • compute z = ((X_test - mean_) / scale_) @ coef_.ravel() + intercept_[0] ;
  • divide by the fitted scale_ , which is the population ( ddof=0 ) standard deviation, never by a standard deviation recomputed with ddof=1 ;
  • return a 1-D float64 array of shape (n_test,) .

Example:

scores = manual_pipeline_scores(X_train, y_train, X_test)
# np.allclose(scores, pipe.decision_function(X_test)) -> True
# scores.shape == (X_test.shape[0],)

Constraints & Assumptions

  • X_train has shape (n_train, n_features) and X_test has shape (n_test, n_features) with the same columns.
  • y_train contains both labels 0 and 1.
  • The match is checked with np.allclose against the pipeline's own decision_function .

What a Strong Answer Covers Guidance

  • Fitting the pipeline with the pinned hyperparameters and reading the four fitted attributes through named_steps .
  • Reproducing the standardization with the stored mean_ and scale_ , broadcast over the test rows.
  • The binary shapes of coef_ and intercept_ and how they lead to an (n_test,) output.
  • Why a ddof=1 standard deviation, or np.sqrt(var_) on a constant feature, breaks the match.
  • What the score means and how it relates to predict_proba and predict .

Follow-up Questions Guidance

  • Why does dividing by a ddof=1 standard deviation break the match, and by how much?
  • How would you extend this to a multiclass decision_function ?
  • How would you turn these scores into the probabilities predict_proba returns?
  • How would you fold the scaler into one raw-scale weight vector and intercept so scoring takes a single dot product?