← All questions
Python & DataMedium· Data Preprocessing

What's the actual difference between fit(), transform()...

fit() learns parameters from the data it’s given — for a StandardScaler, that’s the mean and standard deviation of each feature — and it should only ever see the training set. transform() takes those already-learned parameters and applies them to whatever data you hand it, train or test, without learning anything new. fit_transform() is just those two calls chained for convenience, and like fit() alone, it belongs on training data only.

The reason mixing this up matters: if you call scaler.fit(X) on the full dataset, or scaler.fit_transform(X_test) on the test split, the model has indirectly absorbed test-set statistics before you’ve evaluated it. Your validation metrics come back better than what production will actually deliver, because the “test” was never a genuinely held-out set. The structural fix that prevents this by construction is an sklearn Pipeline — pipeline.fit(X_train, y_train) guarantees every step inside it learns only from training data, and pipeline.predict(X_test) applies those same learned parameters consistently.

Code playground (Python)