CodeBlanks Logo Icon CodeBlanks

fit_transform

fit_transform trains PCA and transforms the data.

fit_transform and inverse_transform

fit_transform learns the components and moves the data into component space. inverse_transform brings it back; since the dropped components' information is lost, the result is an approximation of the original data:

pca = PCA(n_components=4)
Z = pca.fit_transform(Xs)            # (178, 4)
X_geri = pca.inverse_transform(Z)     # (178, 13), approximate
hata = ((Xs - X_geri) ** 2).mean()
print(round(hata, 3))

The reconstruction error is proportional to the dropped variance; it is also used in anomaly detection: examples that are hard to reconstruct are unusual.

A model in component space

PCA is often used before a model: fewer dimensions, faster training and combining correlated features. The right use is again inside a pipeline so that PCA learns only on training data:

pipe = make_pipeline(StandardScaler(), PCA(n_components=5), LogisticRegression(max_iter=1000))
print(cross_val_score(pipe, X, y, cv=5).mean())

The components are no longer original features; they are abstract columns like "component 1", which makes interpretation harder.

Practice Reconstruction error

For component counts 2, 6 and 13, reduce the scaled data with PCA and transform it back. On each line print the component count and the mean squared reconstruction error with round(..., 3).

Starter code

model.py
import numpy as np
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

X, y = load_wine(return_X_y=True)
Xs = StandardScaler().fit_transform(X)

# buraya yaz

Sample solution

model.py
import numpy as np
from sklearn.datasets import load_wine
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

X, y = load_wine(return_X_y=True)
Xs = StandardScaler().fit_transform(X)

for k in [2, 6, 13]:
    pca = PCA(n_components=k)
    X_geri = pca.inverse_transform(pca.fit_transform(Xs))
    print(k, round(((Xs - X_geri) ** 2).mean(), 3))

Expected output

2 0.446
6 0.149
13 0.0

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 If all components are kept, what does <code>inverse_transform(transform(X))</code> give?

  • A zero matrix
  • An approximate X
  • An error
  • X itself (to floating-point precision) Correct answer

Explanation: A full rotation is lossless; so is the inverse.

2 Go back from component space to the original space.

Question
X_geri = pca.____(Z)
Answer
X_geri = pca.inverse_transform(Z)

Explanation: inverse_transform computes Z · components_ + mean_.

3 Match the methods.

Term Meaning
fit_transform Learn and reduce
transform Reduce new data
inverse_transform Approximate inverse
reconstruction error Dropped information

All lessons in Intermediate