Hierarchical clustering groups data points based on similarity without predefined labels. Because it is unsupervised, common metrics like accuracy do not apply. Instead, we use cophenetic correlation coefficient to measure how well the clustering preserves the original distances between points. Another key metric is the silhouette score, which shows how well each point fits within its cluster compared to other clusters. These metrics help us understand if the clusters are meaningful and well-separated.
Hierarchical clustering in ML Python - Model Metrics & Evaluation
Hierarchical clustering does not produce a confusion matrix because it is unsupervised and has no true labels. Instead, we visualize results using dendrograms and silhouette plots.
Dendrogram example: | _______ | | | | ____ | |____ | | || | |___|____||____________|____ This tree-like diagram shows how clusters merge step-by-step. Silhouette plot example: Silhouette score ranges from -1 to 1. Higher values mean better clustering. | * * * * * * * * * * | * * * * * * * * * * | * * * * * * * * * * |______________________________
In hierarchical clustering, precision and recall do not directly apply because there are no true labels. Instead, the tradeoff is between cluster tightness and cluster separation.
- Tight clusters mean points inside a cluster are very similar (high cohesion), but this can cause many small clusters (over-segmentation).
- Well-separated clusters mean clusters are distinct from each other, but forcing separation can group dissimilar points together (under-segmentation).
Choosing the right level to cut the dendrogram balances these. For example, in customer segmentation, too many small groups may be hard to act on, while too few groups may hide important differences.
Good values:
- Cophenetic correlation coefficient close to 1 (e.g., > 0.8) means the clustering preserves original distances well.
- Silhouette score near 0.5 or higher indicates clear, well-separated clusters.
Bad values:
- Cophenetic correlation below 0.5 means poor representation of data structure.
- Silhouette score near 0 or negative means clusters overlap or points are assigned poorly.
- Ignoring scale: Features with different scales can distort distance calculations, hurting cluster quality.
- Overfitting dendrogram cuts: Cutting too deep creates many tiny clusters that may not be useful.
- Using silhouette score blindly: It can be misleading if clusters have very different sizes or shapes.
- Data leakage: Using labels or future information to evaluate clusters breaks unsupervised assumptions.
Your hierarchical clustering model has a cophenetic correlation coefficient of 0.9 but a silhouette score of 0.2. Is this good?
Answer: The high cophenetic correlation means the clustering preserves the original distances well, which is good. However, the low silhouette score indicates clusters are not well separated or points may be assigned poorly. This suggests the clusters may not be meaningful or distinct enough. You might need to adjust the clustering method or how you cut the dendrogram to improve cluster quality.
Practice
hierarchical clustering in machine learning?Solution
Step 1: Understand hierarchical clustering concept
Hierarchical clustering creates a tree-like structure by either merging or splitting data points stepwise.Step 2: Compare with other clustering methods
Unlike fixed cluster methods, it does not require a preset number of clusters and builds a hierarchy instead.Final Answer:
It builds a tree of clusters by joining or splitting data points step by step. -> Option CQuick Check:
Hierarchical clustering = stepwise tree building [OK]
- Confusing hierarchical clustering with k-means clustering
- Thinking it requires a fixed number of clusters upfront
- Believing it assigns clusters randomly
Solution
Step 1: Identify correct import for hierarchical clustering
The functionlinkagefromscipy.cluster.hierarchyis used for hierarchical clustering.Step 2: Check other options for errors
A has invalid syntax, B uses wrong module name (sklearn.cluster.hierarchical does not exist), C imports kmeans (not hierarchical).Final Answer:
from scipy.cluster.hierarchy import linkage -> Option AQuick Check:
Correct import = from scipy.cluster.hierarchy import linkage [OK]
- Using sklearn.cluster.hierarchical which does not exist
- Confusing kmeans import with hierarchical clustering
- Incorrect import syntax
print(Z)?
from scipy.cluster.hierarchy import linkage import numpy as np X = np.array([[1, 2], [1, 4], [5, 2], [5, 4]]) Z = linkage(X, method='single') print(Z)
Solution
Step 1: Understand linkage output format
Thelinkagefunction returns a matrix with rows showing which clusters merged, their distance, and cluster size.Step 2: Check method and output shape
Method 'single' is valid. For 4 points, output is (4-1)=3 rows and 4 columns.Final Answer:
A 3x4 array showing the order and distance of cluster merges -> Option AQuick Check:
linkage output = cluster merges matrix [OK]
- Expecting cluster labels instead of merge matrix
- Confusing linkage output with distance matrix
- Thinking 'single' method is invalid
from scipy.cluster.hierarchy import dendrogram import numpy as np X = np.array([[1, 2], [3, 4], [5, 6]]) dendrogram(X)What is the problem and how to fix it?
Solution
Step 1: Identify dendrogram input requirements
dendrogram() expects a linkage matrix, not raw data points.Step 2: Fix by computing linkage first
Use linkage() on X to get the linkage matrix, then pass it to dendrogram().Final Answer:
You must pass a linkage matrix, not raw data, to dendrogram(). Use linkage() first. -> Option BQuick Check:
dendrogram input = linkage matrix [OK]
- Passing raw data directly to dendrogram()
- Thinking dendrogram needs labels argument
- Importing dendrogram from wrong library
Solution
Step 1: Understand hierarchical clustering flexibility
Hierarchical clustering builds a tree and allows cutting at any distance to form clusters.Step 2: Use distance threshold to decide clusters
By choosing a height to cut the dendrogram, you get clusters without presetting their number.Final Answer:
Cut the dendrogram at a chosen distance threshold to form clusters. -> Option DQuick Check:
Distance threshold cutting = flexible cluster count [OK]
- Thinking hierarchical clustering needs fixed cluster count upfront
- Confusing with k-means clustering method
- Ignoring dendrogram visualization for cluster choice
Ready to prove it?
Pass the test and this pattern moves unknown
