Sub topic 13
Dimensionality Reduction: Summarizing Many Features with Fewer
This chapter assumes you know what features are, you can read basic NumPy and scikit-learn code, and you understand variance as a measure of spread. It follows the chapter on clustering. Both are unsupervised methods, so neither uses labels.
Why reduce dimensions
Each feature is one dimension. The iris data has four. Real datasets can have hundreds or thousands. Many features carry overlapping information. Petal length and petal width, for example, tend to rise and fall together.
Dimensionality reduction replaces the original features with a smaller set of new ones that keep most of the useful information. People use it for three reasons:
- To plot data. You can only draw two or three dimensions.
- To cut storage and training time for later steps.
- To remove noise and redundancy, which sometimes helps a downstream model.
Principal component analysis
Principal component analysis (PCA) is the standard method. It finds new axes, called principal components, built as weighted combinations of the original features. The first component points in the direction where the data varies most. The second component is perpendicular to the first and captures the most remaining variance. Each later component continues the pattern.
You keep the first few components and drop the rest. The data is projected onto the kept axes, so each example becomes a short list of coordinates.
A worked example
This code compresses the iris measurements to two components. It never looks at the species labels.
from sklearn.datasets import load_iris
from sklearn.decomposition import PCA
X, _ = load_iris(return_X_y=True)
pca = PCA(n_components=2)
X_2d = pca.fit_transform(X)
print(X_2d.shape)
print(pca.explained_variance_ratio_)The shape is (150, 2): the same 150 examples with two features instead of four. The array explained_variance_ratio_ gives the fraction of total variance each component keeps. For iris, the first component keeps about 92 percent and the second about 5 percent, so two components retain roughly 97 percent of the variance. That is why a 2D plot of iris still shows the structure well.
Reading the result
The new features are not the original measurements. Component 1 is a blend of all four. You gain compactness and lose direct interpretability. You can inspect pca.components_ to see the weights, but the meaning is rarely as clean as "petal length".
To choose how many components to keep, add up the explained variance ratios and stop when the total is high enough for your purpose. A common target is 90 to 95 percent.
Common errors
- Not scaling features. PCA follows variance, so a feature measured in large units dominates. Standardize features first when units differ, for example with
StandardScaler. - Fitting on all the data before splitting. If PCA feeds a supervised model, fit it on the training set only, then apply
transformto the test set. Fitting on everything leaks test information. - Assuming high variance means useful. PCA ignores labels. A direction with large variance may be irrelevant to your prediction target, and a small-variance direction may be the one that matters.
- Expecting nonlinear structure. PCA only finds straight-line directions. Data curled into a spiral needs a nonlinear method.
Where it fits
Clustering groups examples. Dimensionality reduction summarizes features. They combine well: reduce to a few components, then cluster or plot the result.
Exercise
A PCA fit on a dataset gives explainedvarianceratio_ of [0.70, 0.20, 0.06, 0.04]. How many components do you need to keep at least 90 percent of the variance, and what changes if one feature is measured in much larger units than the others?
Show answer
Two components, because 0.70 + 0.20 = 0.90. If one feature uses much larger units, its variance dominates and the first component will mostly track that feature. Standardize the features before running PCA.
Exercise
You run PCA on all your data, then split it into training and test sets and train a classifier. What is wrong with this order?
Show answer
PCA was fit using test examples, so information from the test set leaked into the features. Split first, fit PCA on the training set, and use transform on the test set.