Machine Learning, FoundationsLesson 29 of 92

Module 3 · The data is the job

Fit on train, transform everywhere: the contract that keeps you honest

In this lesson

  • 8 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 10 of 12 in this chapter

This chapter’s goalTake a raw table with mixed types, missing values, high-cardinality categories, dates and free text, and produce a fitted preprocessing pipeline that leaks nothing from the held-out data, handles categories unseen at training time, and whose every transformation you can justify by what the downstream model requires.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:44Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Two verbs, and the rule between them

Every preprocessing object in scikit-learn has two methods, and the whole discipline of this module comes down to when you call which.

  • fit learns parameters from data. A mean. A median. A vocabulary. A set of PCA components.
  • transform applies those learned parameters to data.

The rule: fit sees training data only. transform is applied to everything.

Python
sc = StandardScaler()
X_train_s = sc.fit_transform(X_train)   # learns AND applies
X_test_s  = sc.transform(X_test)        # applies only

Calling fit_transform on the test set is the error. It computes a new mean from the test rows, so training and test are now on different scales, and the test rows have influenced their own transformation.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the How a Language Model Actually Works exam.

Stuck on something?Ask Funke, your AI tutor

How to cite: Addaly (2026) ‘Fit on train, transform everywhere: the contract that keeps you honest’, in Machine Learning, Foundations. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/machine-learning-foundations/the-fit-transform-contract (Accessed: 29 November 2026).