Menu
CoddyTech

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:

Bars for 13 wine components shrinking from 0.36 to almost 0, and an orange line adding them up, climbing from 0.36 to 1. It crosses the dashed 90% line at component 8

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 icon

Challenge

Medium

A chocolate lab measures 9 things per bar. How many components keep 90% of the spread? scaled is ready.

  1. Fit PCA() and take each component's share.
  2. Print how many components reach 90%.
  3. Draw the shares as bars at 1, 2, 3..., labelled each component; the running total as a line with marker="o", labelled running total; a dashed gray line at 0.9; a legend. Show it.

For another lab:

Components for 90%: 4

A chart titled Spread Kept by Each Component for 7 components: bars shrinking from 0.42, a running total climbing to 1 that crosses the dashed 0.9 line at component 4, and a legend

Try 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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unsupervised Learning

Practice on your own: Online Python compiler