0
0
ML Pythonprogramming~20 mins

Choosing K (elbow method, silhouette score) in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
K-Means Clustering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the Elbow Method

When using the elbow method to choose the number of clusters K in K-means clustering, what does the 'elbow' point represent?

AThe point where clusters have equal sizes
BThe point where adding more clusters does not significantly reduce the within-cluster sum of squares (WCSS)
CThe maximum number of clusters allowed by the algorithm
DThe point where the silhouette score is at its lowest value
Attempts:
2 left
🧠 Conceptual
intermediate
2:00remaining
Silhouette Score Interpretation

What does a silhouette score close to 1 indicate about the clustering quality?

AClusters are well separated and points are close to their own cluster center
BClusters overlap heavily and points are far from their cluster center
CThe number of clusters is too high
DThe clustering algorithm failed to converge
Attempts:
2 left
Predict Output
advanced
2:00remaining
Output of Silhouette Score Calculation

What is the output of the following Python code snippet?

ML Python
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

X, _ = make_blobs(n_samples=100, centers=3, cluster_std=0.5, random_state=42)
model = KMeans(n_clusters=3, random_state=42)
labels = model.fit_predict(X)
score = silhouette_score(X, labels)
print(round(score, 2))
A0.00
B0.25
C1.00
D0.75
Attempts:
2 left
Metrics
advanced
2:00remaining
Choosing K Using Silhouette Scores

You run K-means clustering for K values from 2 to 6 and get these silhouette scores: {2: 0.45, 3: 0.62, 4: 0.58, 5: 0.55, 6: 0.50}. Which K should you choose based on silhouette score?

AK = 3
BK = 6
CK = 2
DK = 4
Attempts:
2 left
🔧 Debug
expert
3:00remaining
Identifying the Error in Elbow Method Plot Code

What error will this code raise when trying to plot the elbow curve?

import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

X, _ = make_blobs(n_samples=100, centers=4, random_state=0)

wcss = []
for k in range(1, 6):
    model = KMeans(n_clusters=k)
    model.fit(X)
    wcss.append(model.inertia_)

plt.plot(range(1, 6), wcss)
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.title('Elbow Method')
plt.show()
AValueError because KMeans requires n_init parameter explicitly
BTypeError because model.inertia_ is not a float
CNo error; the code runs and plots the elbow curve correctly
DNameError because matplotlib.pyplot is not imported
Attempts:
2 left