Cluster evaluation metrics help us check how good our groups (clusters) are. They tell us if the data points in each group are close and if different groups are well separated.
Cluster evaluation metrics in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SciPy
from sklearn.metrics import silhouette_score, adjusted_rand_score, davies_bouldin_score # silhouette_score(X, labels) # adjusted_rand_score(true_labels, predicted_labels) # davies_bouldin_score(X, labels)
These functions need your data points (X) and cluster labels.
Some metrics need true labels to compare, others work without them.
Examples
SciPy
silhouette_score(X, labels)
SciPy
adjusted_rand_score(true_labels, predicted_labels)
SciPy
davies_bouldin_score(X, labels)
Sample Program
This code creates fake data with 3 groups, clusters it, and then checks how good the clustering is using three metrics.
SciPy
from sklearn.datasets import make_blobs from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score, adjusted_rand_score, davies_bouldin_score # Create sample data with 3 clusters X, true_labels = make_blobs(n_samples=300, centers=3, cluster_std=0.60, random_state=0) # Cluster data using KMeans kmeans = KMeans(n_clusters=3, random_state=0) predicted_labels = kmeans.fit_predict(X) # Calculate metrics sil_score = silhouette_score(X, predicted_labels) ari_score = adjusted_rand_score(true_labels, predicted_labels) db_score = davies_bouldin_score(X, predicted_labels) print(f"Silhouette Score: {sil_score:.3f}") print(f"Adjusted Rand Index: {ari_score:.3f}") print(f"Davies-Bouldin Score: {db_score:.3f}")
Important Notes
Silhouette score ranges from -1 to 1; closer to 1 is better.
Adjusted Rand Index needs true labels; if unknown, use unsupervised metrics like silhouette.
Davies-Bouldin score is better when smaller.
Summary
Cluster evaluation metrics help measure how well your data is grouped.
Use silhouette score and Davies-Bouldin score when true labels are unknown.
Use Adjusted Rand Index to compare clustering with known labels.
Practice
1. Which cluster evaluation metric is best used when you do NOT have true labels for your data?
easy
Solution
Step 1: Understand the role of true labels
Adjusted Rand Index requires true labels to compare clusters, so it is not suitable without labels.Step 2: Identify metrics for unknown labels
Silhouette Score measures how well clusters are separated without needing true labels.Final Answer:
Silhouette Score -> Option BQuick Check:
Unknown labels = Silhouette Score [OK]
Hint: Use silhouette score when labels are unknown [OK]
Common Mistakes:
- Confusing Adjusted Rand Index as label-free
- Choosing accuracy score which needs labels
- Using mean squared error for clustering
2. Which of the following is the correct way to import the silhouette_score function for cluster evaluation?
easy
Solution
Step 1: Check common library modules
Silhouette score is available in sklearn.metrics module, not in scipy.cluster or spatial.distance.Step 2: Verify import syntax
The correct import is from sklearn.metrics import silhouette_score.Final Answer:
from sklearn.metrics import silhouette_score -> Option DQuick Check:
Correct import = sklearn.metrics [OK]
Hint: Silhouette score is in sklearn.metrics module [OK]
Common Mistakes:
- Importing from scipy.cluster directly
- Using scipy.spatial.distance for silhouette_score
- Confusing hierarchy module with vq
3. What is the output of the following code snippet?
from scipy.cluster.vq import kmeans, vq, whiten import numpy as np data = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) whitened = whiten(data) centroids, _ = kmeans(whitened, 2) cluster_labels, _ = vq(whitened, centroids) from sklearn.metrics import silhouette_score score = silhouette_score(whitened, cluster_labels) print(round(score, 2))
medium
Solution
Step 1: Understand the code flow
The code whitens data, runs kmeans for 2 clusters, assigns labels, then calculates silhouette score.Step 2: Interpret silhouette score meaning
Data clearly forms two groups; silhouette score is around 0.75 indicating good cluster separation.Final Answer:
0.75 -> Option AQuick Check:
Well-separated clusters ≈ 0.75 silhouette [OK]
Hint: Silhouette near 0.75 means good cluster separation [OK]
Common Mistakes:
- Expecting silhouette score of 1.0 always
- Confusing whitened data with original scale
- Misreading cluster labels
4. Identify the error in this code snippet for calculating Davies-Bouldin score:
from scipy.spatial.distance import davies_bouldin_score labels = [0, 0, 1, 1] data = [[1, 2], [1, 4], [10, 2], [10, 4]] score = davies_bouldin_score(data, labels) print(score)
medium
Solution
Step 1: Check import source
Davies-Bouldin score is in sklearn.metrics, not scipy.spatial.distance.Step 2: Validate data and labels
Data and labels lengths match and data as list works with sklearn, so no error there.Final Answer:
Importing davies_bouldin_score from wrong module -> Option CQuick Check:
Correct import is sklearn.metrics [OK]
Hint: Davies-Bouldin score is in sklearn.metrics, not scipy [OK]
Common Mistakes:
- Importing from scipy.spatial.distance
- Assuming data must be numpy array
- Thinking Davies-Bouldin needs true labels
5. You have true labels and predicted cluster labels for a dataset. Which metric from scipy or sklearn should you use to evaluate clustering quality by comparing these labels?
hard
Solution
Step 1: Identify metrics needing true labels
Adjusted Rand Index compares predicted clusters with true labels to measure similarity.Step 2: Exclude label-free metrics
Silhouette, Davies-Bouldin, and Calinski-Harabasz do not use true labels for evaluation.Final Answer:
Adjusted Rand Index -> Option AQuick Check:
True vs predicted labels = Adjusted Rand Index [OK]
Hint: Use Adjusted Rand Index to compare true and predicted labels [OK]
Common Mistakes:
- Using silhouette score with true labels
- Confusing Davies-Bouldin as label-based
- Choosing Calinski-Harabasz for label comparison
