Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Cluster Evaluation Metrics
📖 Scenario: You have grouped customers into clusters based on their shopping behavior. Now, you want to check how good these clusters are by comparing them to known customer groups.
🎯 Goal: Build a small program to calculate cluster evaluation metrics using sklearn. You will create true labels and predicted cluster labels, then compute the Adjusted Rand Index and Normalized Mutual Information scores.
📋 What You'll Learn
Create two lists: true_labels and predicted_labels with exact values
Import adjusted_rand_score and normalized_mutual_info_score from sklearn.metrics
Calculate ari_score using adjusted_rand_score(true_labels, predicted_labels)
Calculate nmi_score using normalized_mutual_info_score(true_labels, predicted_labels)
Print both scores with descriptive text
💡 Why This Matters
🌍 Real World
Cluster evaluation metrics help businesses check if their customer groups or product categories are meaningful and useful.
💼 Career
Data scientists and analysts use these metrics to validate clustering results and improve machine learning models.
Progress0 / 4 steps
1
Create true and predicted cluster labels
Create a list called true_labels with values [0, 0, 1, 1, 2, 2] and a list called predicted_labels with values [0, 0, 2, 1, 2, 2].
SciPy
Hint
Use square brackets to create lists with the exact numbers given.
2
Import cluster evaluation functions
Import adjusted_rand_score and normalized_mutual_info_score from sklearn.metrics.
SciPy
Hint
Use from sklearn.metrics import adjusted_rand_score, normalized_mutual_info_score.
3
Calculate ARI and NMI scores
Calculate ari_score by calling adjusted_rand_score(true_labels, predicted_labels) and calculate nmi_score by calling normalized_mutual_info_score(true_labels, predicted_labels).
SciPy
Hint
Call the functions with the two lists as arguments and save results in ari_score and nmi_score.
4
Print the cluster evaluation scores
Print the text "Adjusted Rand Index:" followed by ari_score and print the text "Normalized Mutual Information:" followed by nmi_score.
SciPy
Hint
Use two print statements with the exact text and variables.
Practice
(1/5)
1. Which cluster evaluation metric is best used when you do NOT have true labels for your data?
easy
A. Adjusted Rand Index
B. Silhouette Score
C. Accuracy Score
D. Mean Squared Error
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 B
Quick 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
A. from scipy.cluster import silhouette_score
B. from scipy.spatial.distance import silhouette_score
C. from scipy.cluster.hierarchy import silhouette_score
D. from sklearn.metrics import silhouette_score
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 D
Quick 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?
C. Importing davies_bouldin_score from wrong module
D. Davies-Bouldin score requires true labels
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 C
Quick 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
A. Adjusted Rand Index
B. Davies-Bouldin Score
C. Silhouette Score
D. Calinski-Harabasz Index
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 A
Quick Check:
True vs predicted labels = Adjusted Rand Index [OK]
Hint: Use Adjusted Rand Index to compare true and predicted labels [OK]