Explained Variance
Part of the Unsupervised Learning section of Coddy's Machine Learning journey. Lesson 27 of 51.
How much did 2 components keep? Each component holds a share of the total spread, its explained variance ratio. For all 13 wine components:
PCA() with no n_components keeps them all. np.cumsum gives the running total:
pca = PCA().fit(scaled)
ratio = pca.explained_variance_ratio_
print(ratio[:3].round(3))
total = np.cumsum(ratio)
print((total < 0.9).sum() + 1)This prints [0.362 0.192 0.111] and 8: the totals still under 0.9, plus the one that crosses. So the 2D wine map kept 55%.
Challenge
MediumA chocolate lab measures 9 things per bar. How many components keep 90% of the spread? scaled is ready.
- Fit
PCA()and take each component's share. - Print how many components reach 90%.
- Draw the shares as bars at 1, 2, 3..., labelled
each component; the running total as a line withmarker="o", labelledrunning total; a dashed gray line at 0.9; a legend. Show it.
For another lab:
Components for 90%: 4Try it yourself
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
# A chocolate maker's lab: 9 measurements for each of 150 bars
bars = pd.read_csv("chocolate.csv")
scaled = StandardScaler().fit_transform(bars)
fig, ax = plt.subplots()
ax.set_title("Spread Kept by Each Component")
ax.set_xlabel("Component")
ax.set_ylabel("Share of spread")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Unsupervised Learning
1Finding Groups
Learning Without LabelsMeasuring SimilarityK-Means by HandK-Means in scikit-learnRecap: Gym Members7PCA in Practice
Seeing 64 DimensionsComponents as PatternsCompress and RebuildPCA Before a ModelRecap: Face Scanner10Project: Digit Explorer
Look at the DigitsShrink with PCACluster the DigitsName the ClustersFind the Odd OnesPractice on your own: Online Python compiler