What is dimensionality reduction?
- Machine Learning course
- Intermediate
- Lesson 75/112
- 5 min
Dimensionality reduction reduces feature count while preserving information.
What is dimensionality reduction?
Dimensionality reduction means representing many-feature data with fewer features while keeping most of the information. There are two families:
- Feature selection: keeps a subset of the original columns (interpretable).
- Feature extraction: produces new columns (PCA, t-SNE, UMAP).
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
print(X.shape) # (1797, 64): 8x8-pixel digit images64 pixels are more than needed to describe a digit; neighbouring pixels are strongly correlated.
Constant and correlated columns
In image data, edge pixels are almost always 0; their variance is near zero and they carry no information. Correlated columns repeat the same information:
print((X.var(axis=0) == 0).sum()) # pixels that never change
C = np.corrcoef(X[:, X.var(axis=0) > 0], rowvar=False)
print((np.abs(C) > 0.8).sum()) # strongly correlated pairsBoth cases show why dimensionality reduction is possible.
Practice Get to know the digit data
Print the shape of the digit data, the number of pixels that never change and the number of classes (len(np.unique(y))) on separate lines.
Starter code
import numpy as np
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
# buraya yazSample solution
import numpy as np
from sklearn.datasets import load_digits
X, y = load_digits(return_X_y=True)
print(X.shape)
print((X.var(axis=0) == 0).sum())
print(len(np.unique(y)))Expected output
(1797, 64) 3 10
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which family does PCA belong to?
- Feature extraction Correct answer
- Feature selection
- Clustering
- Classification
Explanation: PCA produces linear combinations of the original columns, not the columns themselves.
2 Count the columns that never change.
print((X.____(axis=0) == 0).sum())
print((X.var(axis=0) == 0).sum())
Explanation: A column with zero variance is constant.
3 Match the concepts.
| Term | Meaning |
|---|---|
feature selection |
Subset of original columns |
feature extraction |
New columns |
constant column |
Carries no information |
correlated columns |
Repeated information |