I ran fit_transform on my training data and then again on my test data. My model's predictions make no sense." / "I got a feature-count mismatch error when I passed the reduced test data to PCA.
Why this happens
Calling fit_transform on the test set fits a new PCA with different components. The test data then ends up in a different coordinate system from the training data, so the model sees meaningless inputs. The mismatch error is a different mistake from the same family: passing data with a different number of columns than the PCA was fit on, often the already-reduced data or a test set that lost a column during preprocessing.
Answer 1: Fit on the training data, transform both
from sklearn.model_selection import train_test_split
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2,
random_state=0)
scaler = StandardScaler().fit(X_tr)
pca = PCA(n_components=10).fit(scaler.transform(X_tr))
X_tr_pca = pca.transform(scaler.transform(X_tr))
X_te_pca = pca.transform(scaler.transform(X_te)) # transform
only
The rule is the same as for any preprocessing step: fit only on training data, then transform everything else with the fitted object.
