Build a StandardScaler and logistic regression pipeline that avoids test-set leakage

Quick Overview

Build a scikit-learn pipeline that standardizes features and fits an L2 logistic regression, then predict integer labels for a test set. It tests where scaling statistics should be learned, how test-set leakage happens, and why feature scale matters for a penalized model.

Build a StandardScaler and logistic regression pipeline that avoids test-set leakage

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `scaled_pipeline_predict(X_train, y_train, X_test)` that fits a `Pipeline` of `StandardScaler` then `LogisticRegression` on the training data and returns predicted labels for `X_test`. The scaler is fit on `X_train` only and reused, without refitting, on `X_test`. - Build `Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(...))])`. - Keep `StandardScaler` at its defaults: `with_mean=True`, `with_std=True`. - Pin `LogisticRegression` 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 integer dtype and labels in `{0, 1}`. ```hint Where the statistics come from Think about which pipeline call learns the mean and standard deviation, and which calls only apply them. ``` ### Constraints & Assumptions - `y_train` holds labels in `{0, 1}`, and both classes appear. - `X_train` and `X_test` are dense numeric 2-D arrays with the same columns in the same order. ### What a Strong Answer Covers - A two-step pipeline with the exact hyperparameters, fit once on the training data. - Test predictions that pass through the already fitted scaler, returned as a 1-D integer array. - Why fitting the scaler on anything beyond `X_train` leaks information. - Why standardizing the features matters for an L2-penalized logistic regression. ### Follow-up Questions - Why is putting the scaler in a `Pipeline` safer than scaling manually before `train_test_split`? - How would you tune `C` without leaking? - How would you extend the pipeline when some columns are categorical?

Overview: Build a scikit-learn pipeline that standardizes features and fits an L2 logistic regression, then predict integer labels for a test set. It tests where scaling statistics should be learned, how test-set leakage happens, and why feature scale matters for a penalized model.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement scaled_pipeline_predict(X_train, y_train, X_test) that fits a Pipeline of StandardScaler then LogisticRegression on the training data and returns predicted labels for X_test. The scaler is fit on X_train only and reused, without refitting, on X_test.

  • Build Pipeline([('scaler', StandardScaler()), ('clf', LogisticRegression(...))]) .
  • Keep StandardScaler at its defaults: with_mean=True , with_std=True .
  • Pin LogisticRegression 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 integer dtype and labels in {0, 1} .

Constraints & Assumptions

  • y_train holds labels in {0, 1} , and both classes appear.
  • X_train and X_test are dense numeric 2-D arrays with the same columns in the same order.

What a Strong Answer Covers Guidance

  • A two-step pipeline with the exact hyperparameters, fit once on the training data.
  • Test predictions that pass through the already fitted scaler, returned as a 1-D integer array.
  • Why fitting the scaler on anything beyond X_train leaks information.
  • Why standardizing the features matters for an L2-penalized logistic regression.

Follow-up Questions Guidance

  • Why is putting the scaler in a Pipeline safer than scaling manually before train_test_split ?
  • How would you tune C without leaking?
  • How would you extend the pipeline when some columns are categorical?