0
0
SciPydata~10 mins

Cluster evaluation metrics in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cluster evaluation metrics
Start with true labels and predicted clusters
Choose evaluation metric
Calculate metric value
Interpret metric: higher or lower is better
Use metric to compare clustering quality
End
We start with true and predicted cluster labels, pick a metric, calculate it, then interpret the result to judge clustering quality.
Execution Sample
SciPy
from sklearn.metrics import adjusted_rand_score
true = [0, 0, 1, 1, 2, 2]
pred = [0, 0, 2, 1, 2, 2]
score = adjusted_rand_score(true, pred)
print(score)
Calculate Adjusted Rand Index to compare true and predicted cluster labels.
Execution Table
StepActionInputIntermediate ResultOutput
1Input true labels[0, 0, 1, 1, 2, 2]-Stored true labels
2Input predicted labels[0, 0, 2, 1, 2, 2]-Stored predicted labels
3Calculate contingency matrixtrue & pred[[2, 0, 0], [0, 1, 1], [0, 0, 2]]Contingency matrix computed
4Compute index componentscontingency matrixSum combinations for pairsPairs counted
5Calculate Adjusted Rand Indexindex componentsAdjusted for chanceScore = 0.5757575757575757
6Print scorescore-0.5757575757575757
7End--Execution complete
💡 All steps completed, Adjusted Rand Index calculated and printed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
trueNone[0, 0, 1, 1, 2, 2][0, 0, 1, 1, 2, 2][0, 0, 1, 1, 2, 2][0, 0, 1, 1, 2, 2][0, 0, 1, 1, 2, 2]
predNoneNone[0, 0, 2, 1, 2, 2][0, 0, 2, 1, 2, 2][0, 0, 2, 1, 2, 2][0, 0, 2, 1, 2, 2]
contingency_matrixNoneNoneNone[[2, 0, 0], [0, 1, 1], [0, 0, 2]][[2, 0, 0], [0, 1, 1], [0, 0, 2]][[2, 0, 0], [0, 1, 1], [0, 0, 2]]
scoreNoneNoneNoneNone0.57575757575757570.5757575757575757
Key Moments - 3 Insights
Why is the Adjusted Rand Index not 1 even though some clusters match?
Because the metric adjusts for chance grouping, partial mismatches lower the score as shown in step 5 of the execution table.
What does the contingency matrix represent in clustering evaluation?
It counts how many points fall into each pair of true and predicted clusters, as shown in step 3 of the execution table.
Why do we need both true and predicted labels as inputs?
Because cluster evaluation metrics compare these two label sets to measure similarity, as shown in steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, what is the Adjusted Rand Index score calculated?
A0.0
B1.0
C0.5757575757575757
D0.85
💡 Hint
Refer to the 'Output' column in step 5 of the execution_table.
At which step is the contingency matrix computed?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Action' column in the execution_table for when the contingency matrix is created.
If the predicted labels were identical to true labels, what would the Adjusted Rand Index be?
ANegative
BExactly 1
CClose to 0
DUndefined
💡 Hint
Adjusted Rand Index equals 1 when clustering matches perfectly, as explained in key_moments.
Concept Snapshot
Cluster evaluation metrics compare true and predicted cluster labels.
Common metrics: Adjusted Rand Index, Silhouette Score, Homogeneity.
Input: true labels and predicted labels.
Output: score indicating clustering quality.
Higher score usually means better clustering.
Use metrics to choose or tune clustering methods.
Full Transcript
Cluster evaluation metrics help us measure how well our clustering matches the true groups. We start with two lists: true labels and predicted cluster labels. We pick a metric like Adjusted Rand Index, which compares these labels and adjusts for chance. The code calculates a contingency matrix counting overlaps between true and predicted clusters. Then it computes the score, which ranges from -1 to 1, where 1 means perfect match. The example shows a score of about 0.58, meaning partial agreement. This process helps us judge clustering quality and improve models.