Reducing dimensions with PCA.
See how principal components rotate numeric data and what information a projection loses.
Start with the meaning.
A feature table can have many columns. Some may carry overlapping information: height measured in centimeters and inches, for example, are two columns describing nearly one thing. Dimensionality reduction creates a smaller set of new coordinates that preserves some structure of the original data. Principal component analysis, PCA, chooses directions of high variation in centered numeric training data. The first component is the direction with the most variance; later components are perpendicular directions that capture remaining variance.
PCA is a rotation and projection of a numeric feature space. A component is a weighted combination of original columns, not usually a field with a simple everyday name. Keeping only the first few components discards some information. The explained variance ratio reports how much variation each retained direction accounts for in the data used to fit PCA. High retained variance does not guarantee that the retained directions preserve the information needed to predict a particular target.
How the idea works.
Scaling changes PCA. If one feature has values in thousands and another in fractions, the first can dominate variance merely because of its units. Standardizing features first may be appropriate, though it changes the question to variation measured in standard-deviation units. PCA is unsupervised: it does not look at y when choosing directions. A low-variance direction could still be highly predictive of the target.
PCA can help visualisation by projecting many features onto two components, reduce noise in some settings, or make correlated inputs easier for a later method. It is not a universal performance boost. A two-dimensional picture can hide separation that lives in later components and can exaggerate apparent overlap or distance. The axes are learned from the dataset, so projections from different fits may not be directly comparable.
PCA must be fitted inside each training fold during evaluation. Computing components from the entire dataset before splitting lets validation and test rows influence the representation. The fitted centering, scaling, and component directions belong together in a pipeline. New rows are projected using those same learned directions; refitting PCA on a single live row makes no sense.
Follow the code.
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
X = [[1, 2], [2, 4], [3, 6], [4, 8]]
projection = make_pipeline(StandardScaler(), PCA(n_components=1))
X_small = projection.fit_transform(X)
print(X_small.shape)
print(X_small)The two columns move together: the second is twice the first. StandardScaler centers and scales each column. PCA then finds one direction that contains essentially all of the variation in these four rows. n_components=1 keeps one coordinate per row, so X_small.shape is (4, 1). The printed coordinates can have either overall sign because reversing a component's direction describes the same line. Their relative positions, not the sign convention, carry the structure.
This is a specially constructed case of near-perfect redundancy. Most real tables do not collapse so neatly. The result is a representation of X, not a prediction of y. If a target existed, a separate evaluation would decide whether this reduced representation helps or harms the downstream task.
What the result does not say.
Other methods answer other questions. Non-negative matrix factorisation builds additive components for nonnegative data. t-SNE and UMAP can create useful visual maps of local relationships but do not preserve every distance or make clusters objective facts. A visually separated plot is exploratory evidence, not a performance metric.
Dimensionality reduction is also a compression choice: fewer coordinates simplify storage or later fitting at the cost of information. State what the compressed representation is intended to preserve before choosing a method.
One clear idea.
PCA preserves directions of training-data variation, not necessarily the information a target needs.
The ML cookbook
Complete, explained Python workflows sit alongside this reading course.