EasyDeepLearn

You did StandardScaler().fittransform(X)\mathrm{fit}_{\mathrm{transform}}(X) then split into train/test. Why is this wrong?

easy

Answer

  • The scaler was fit on the full dataset including the test set, so mean and std leak information from test data into training preprocessing.
  • Effect: test metric is slightly optimistic, sometimes a lot on small data.
  • Correct order: split first, fit on train only, transform train and test with the same fitted scaler.
  • In practice: wrap everything in a Pipeline and pass it to crossvalscore\mathrm{cross}_{\mathrm{val}}\mathrm{score} or GridSearchCV — the split-then-fit invariant is automatic.
Check yourself — multiple choice
  • It's fine as long as the metric is high
  • The scaler sees the test set's mean/std — leakage. Split first, fit on train only
  • It doesn't matter for tree models
  • You should always transform before splitting

Fit on full data leaks test statistics. Split → fit-on-train → transform both sets.

#pipelines#data-quality#preprocessing

Practise Supervised Learning

215 interview questions in this topic.

Related questions