0
0
ML Pythonml~10 mins

Cluster evaluation metrics in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the function that calculates the silhouette score.

ML Python
from sklearn.metrics import [1]

score = [1](X, labels)
Drag options to blanks, or click blank then click option'
Asilhouette_score
Bmean_squared_error
Caccuracy_score
Dconfusion_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using accuracy_score which is for classification.
Using mean_squared_error which is for regression.
2fill in blank
medium

Complete the code to calculate the Davies-Bouldin index for clustering evaluation.

ML Python
from sklearn.metrics import davies_bouldin_score

score = davies_bouldin_score(X, [1])
Drag options to blanks, or click blank then click option'
Apredictions
Blabels
Cfeatures
Dtargets
Attempts:
3 left
💡 Hint
Common Mistakes
Passing features or targets instead of cluster labels.
Using predictions which is ambiguous here.
3fill in blank
hard

Fix the error in the code to compute the adjusted Rand index correctly.

ML Python
from sklearn.metrics import adjusted_rand_score

score = adjusted_rand_score([1], true_labels)
Drag options to blanks, or click blank then click option'
Alabels
BX
Cfeatures
Dpredicted_labels
Attempts:
3 left
💡 Hint
Common Mistakes
Passing feature data instead of predicted labels.
Using generic variable names that don't represent predicted clusters.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each cluster label to its count.

ML Python
cluster_counts = {label: [1] for label in set(labels) if [2]
Drag options to blanks, or click blank then click option'
Alabels.count(label)
Blabel > 0
Clabel >= 0
Dlen(labels)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(labels) inside the comprehension which is unrelated.
Filtering labels with label > 0 which excludes zero.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each cluster label to the average silhouette score of its points.

ML Python
silhouette_avg = {label: sum(scores[i] for i, l in enumerate(labels) if l == [1]) / [2] for label in set(labels) if [3]
Drag options to blanks, or click blank then click option'
Alabel
Bsum(1 for i, l in enumerate(labels) if l == label)
Clabel >= 0
Dlen(scores)
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(scores) as denominator which is total points, not per cluster.
Filtering labels with label > 0 which excludes zero.