devnotes.

Principal Component Analysis (PCA)

PCA compresses many correlated features into a smaller number of new, uncorrelated features that retain as much of the original information as possible — useful for visualization, speeding up training, and reducing noise from redundant features.

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

X_scaled = StandardScaler().fit_transform(X)   # PCA is also distance/variance-based — scale first

pca = PCA(n_components=2)          # reduce to 2 dimensions, for visualization
X_pca = pca.fit_transform(X_scaled)

print(pca.explained_variance_ratio_)   # fraction of original variance each new component captures
print(sum(pca.explained_variance_ratio_))   # total variance retained after reduction

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=y)   # visualize high-dimensional data in 2D
plt.xlabel("Principal Component 1")
plt.ylabel("Principal Component 2")

pca_full = PCA(n_components=0.95)   # keep enough components to retain 95% of the variance
X_reduced = pca_full.fit_transform(X_scaled)
print(X_reduced.shape)                 # fewer columns than the original, chosen automatically

PCA finds new axes (principal components) that are linear combinations of the original features, chosen specifically to capture the maximum possible variance in the data — the first component captures the most variance, the second captures the most remaining variance while being uncorrelated with the first, and so on. explained_variance_ratio_ reports what fraction of the original data's total variability each component preserves, letting you judge how much information was retained versus discarded by the reduction.

A dataset with dozens or hundreds of features can't be visualized directly, but reducing it to 2 or 3 principal components makes a scatter plot possible — often revealing cluster structure, outliers, or class separability that would otherwise be invisible in the original high-dimensional space. Beyond visualization, PCA is also used purely as a preprocessing step before modeling: reducing dimensionality speeds up training, and by keeping only the components that explain most of the variance (n_components=0.95 above), noise from less-informative dimensions is discarded, sometimes measurably improving a downstream model's performance and reducing overfitting risk.

Principal Component Analysis (PCA) in scikit-learn | DevNotes