n_components
- Machine Learning course
- Intermediate
- Lesson 71/112
- 5 min
n_components sets the number of dimensions to reduce to.
Choosing n_components
n_components can be an integer (number of components) or a ratio between 0 and 1. If a ratio is given, PCA itself picks the fewest components that preserve that much variance:
pca = PCA(n_components=0.9).fit(Xs) # 90% of the variance
print(pca.n_components_) # chosen component count2 or 3 for visualisation, 90-95% of the variance for compression before a model are common choices.
Choosing inside a pipeline
The number of components is a hyperparameter; the most accurate choice is made with cross-validation by looking at the next model's success:
pipe = make_pipeline(StandardScaler(), PCA(), LogisticRegression(max_iter=1000))
gs = GridSearchCV(pipe, {"pca__n_components": [2, 4, 6, 8]}, cv=5).fit(X, y)
print(gs.best_params_)A variance ratio isn't a rule but a starting point; a low-variance component might carry the information that separates the classes.
Practice Choice by ratio
Train PCA on the scaled wine data for n_components 0.8, 0.9 and 0.95. On each line print the ratio and the chosen number of components (n_components_).
Starter code
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 yazSample solution
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 oran in [0.8, 0.9, 0.95]:
print(oran, PCA(n_components=oran).fit(Xs).n_components_)Expected output
0.8 5 0.9 8 0.95 10
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does <code>PCA(n_components=0.95)</code> do?
- Picks the fewest components keeping 95% of the variance Correct answer
- Keeps 0.95 components
- Drops 95% of the features
- It errors
Explanation: The chosen number is in the n_components_ attribute after fit.
2 Search the number of components in a grid.
gs = GridSearchCV(pipe, {"____": [2, 4, 6]}, cv=5)
gs = GridSearchCV(pipe, {"pca__n_components": [2, 4, 6]}, cv=5)
Explanation: In make_pipeline the PCA step is named "pca".
3 Match the values.
| Term | Meaning |
|---|---|
n_components=2 |
Visualisation |
n_components=0.95 |
Choice by variance ratio |
n_components_ |
Chosen count |
pca__n_components |
Grid parameter |