Split the data first, fit the scaler on the training set, and use that same scaler to transform both sets. For standardization, this means saving the training mean and standard deviation of each feature. The model learned what a feature value means on that scale. Use it again when you make a prediction, even if you have just one new sample.

Here are three ways you might see scikit-learn’s StandardScaler applied to a feature matrix X. Only the third gives the model and its test data a common scale learned entirely from the training set.

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()

Scenario 1. Fit before splitting

X_scaled = scaler.fit_transform(X)
X_train_scaled, X_test_scaled = train_test_split(X_scaled, random_state=0)

Here the test examples influence the mean and standard deviation used during training. This leaks information from the held-out set into the model evaluation.

Scenario 2. Fit separately on each split

X_train, X_test = train_test_split(X, random_state=0)
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.fit_transform(X_test)

A standardized test value of zero now means “equal to the test mean.” The model learned that zero means “equal to the training mean.” If those means differ, the same measurement can end up on different sides of the model’s decision boundary.

Scenario 3. Fit on the training split

X_train, X_test = train_test_split(X, random_state=0)
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

During cross-validation, repeat this procedure inside each fold. Fit the scaler on that fold’s training examples and use it to transform the validation examples. In scikit-learn, passing a pipeline containing the scaler and estimator to cross-validation takes care of this. scikit-learn’s preprocessing examples

For a concrete case where refitting the scaler changes a prediction, see the worked example with three length measurements.