Back to Practice
#0105

Fit and Apply Standard Scaler

MediumML25 min20 XP

Problem

Given train and test lists, return scaled_train and scaled_test using mean and population standard deviation computed from train only. If std is zero, use 1.

Why This Matters

Preprocessing must be fit on training data only. This is one of the most important leakage prevention habits.

Function Signature

def standard_scale_train_test(train, test):

Examples

Example 1
Inputtrain=[10,20,30], test=[40]
Outputtest scaled using train mean 20 and train std

The test value must not influence mean or std.

Constraints

  • Return the exact structure requested.
  • Handle the edge cases mentioned in the prompt.
  • Prefer clear logic over clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputtrain=[10,20,30], test=[40]
Expectedtest scaled using train mean 20 and train std

The test value must not influence mean or std.

Hidden Test Categories
constant train valuesnegative valuessingle test value