Chapter 4 · Section 6 practice
Hierarchical Clustering: One Customer Story
Optional practice — Section 4.6 is not covered in the final exam.
Work from question 1 to 10. Edit your Python box, click Run Code, review the result, then click Submit attempt. Reference answers unlock after all ten attempts are submitted. This records completion, not correctness. Each box runs independently. Progress is saved in this browser.
1. Count merge rows
Warm-up. Print Z.shape.
Write your answer, then run it.
2. Read the first height
Warm-up. Print the first merge height.
Write your answer, then run it.
3. Read the final height
Warm-up. Print the last merge height.
Write your answer, then run it.
4. Read the final count
Build your skills. Print the final customer count.
Write your answer, then run it.
5. Cut before any merge
Build your skills. At height1, print the number of groups.
Write your answer, then run it.
6. Make three groups
Build your skills. At height4, print one group label per customer.
Write your answer, then run it.
7. Make two groups
Build your skills. At height6, print one group label per customer.
Write your answer, then run it.
8. Join everyone
Challenge. At height11, print the number of groups.
Write your answer, then run it.
9. Compare the three rules
Challenge. For {A,B} versus C, print single, complete and average linkage using gaps6and5.
Write your answer, then run it.
10. Read a group merge
Challenge. Print the third row of Z and explain it.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Count merge rows
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
print(Z.shape)Expected output
(4, 4)Five customers require four merges; each merge stores four entries.
2. Read the first height
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
print(Z[0, 2])Expected output
2.0A+B merge at2; Python index2 selects the third column.
3. Read the final height
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
print(Z[-1, 2])Expected output
10.0The final two groups join at10; -1 selects the last row.
4. Read the final count
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
print(int(Z[-1, 3]))Expected output
5The last group contains all five customers.
5. Cut before any merge
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
labels = fcluster(Z, t=1, criterion="distance")
print(pd.Series(labels).nunique())Expected output
5All five customers remain separate.
6. Make three groups
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
labels = fcluster(Z, t=4, criterion="distance")
print(pd.Series(labels, index=distance_table.index))Expected output
Customer
A 1
B 1
C 3
D 2
E 2
dtype: int32The groups are {A,B}, {C}, {D,E}; numeric labels are arbitrary.
7. Make two groups
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
labels = fcluster(Z, t=6, criterion="distance")
print(pd.Series(labels, index=distance_table.index))Expected output
Customer
A 1
B 1
C 2
D 2
E 2
dtype: int32The groups are {A,B} and {C,D,E}.
8. Join everyone
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
labels = fcluster(Z, t=11, criterion="distance")
print(pd.Series(labels).nunique())Expected output
1Above the last merge, one group contains all five customers.
9. Compare the three rules
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
gaps = pd.Series([6, 5])
print(gaps.min(), gaps.max(), gaps.mean())Expected output
5 6 5.5The three group comparisons are5,6and5.5.
10. Read a group merge
import pandas as pd
import numpy as np
from scipy.spatial.distance import squareform
from scipy.cluster.hierarchy import linkage, fcluster
distance_table = pd.read_csv(
"https://busanalytics-book.pages.dev/data/chapter4-five-customer-distances.csv",
index_col="Customer")
pair_distances = squareform(distance_table.to_numpy())
Z = linkage(pair_distances, method="complete")
print(Z[2])Expected output
[2. 6. 5. 3.][2,6,5,3] joins C with {D,E}, at height5, containing3customers.