CodeBlanks Logo Icon CodeBlanks

explained_variance_

explained_variance_ gives the variance ratio of each component.

explained_variance_ratio_

explained_variance_ratio_ gives how much of the total variance each component explains. Its cumulative sum answers "what percentage of the information do the first k components keep?":

pca = PCA().fit(Xs)
print(pca.explained_variance_ratio_[:3].round(3))
kum = np.cumsum(pca.explained_variance_ratio_)
print(np.argmax(kum >= 0.9) + 1)   # components needed for 90%

When these values are drawn as a line plot it is called a "scree plot"; the elbow is a hint for the number of components.

Reading component loadings

The rows of components_ are the components; the values in each row (loadings) show which features that component is made of. Loadings with large absolute values give the component's meaning:

d = load_wine()
yuk = pca.components_[0]
for i in np.argsort(-abs(yuk))[:3]:
    print(d.feature_names[i], round(yuk[i], 2))

The sign of a loading alone isn't meaningful (a component can be flipped); what matters is the features' signs relative to each other within the same component.

Practice Explained variance

Train a full PCA on the scaled wine data. Print the variance ratios of the first three components with round(3), then the number of components needed to reach 80% of the variance with np.argmax(kum >= 0.8) + 1.

Starter code

model.py
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 yaz

Sample solution

model.py
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)

pca = PCA().fit(Xs)
print(pca.explained_variance_ratio_[:3].round(3))
kum = np.cumsum(pca.explained_variance_ratio_)
print(np.argmax(kum >= 0.8) + 1)

Expected output

[0.362 0.192 0.111]
5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the sum of <code>explained_variance_ratio_</code> over all components?

  • 0
  • The total variance
  • 1 Correct answer
  • The number of features

Explanation: The ratios are values divided by the total variance; they sum to 1.

2 Take the cumulative sum of the explained variance.

Question
kum = np.____(pca.explained_variance_ratio_)
Answer
kum = np.cumsum(pca.explained_variance_ratio_)

Explanation: cumsum gives the running total at each position.

3 Match the attributes.

Term Meaning
explained_variance_ Component variance
explained_variance_ratio_ Variance ratio
components_ Loadings
mean_ Centring mean

All lessons in Intermediate