Complete the code to import the KMeans class from scikit-learn.
from sklearn.cluster import [1]
The KMeans class is used for clustering in scikit-learn. Importing it correctly allows us to create KMeans models.
Complete the code to fit a KMeans model with 3 clusters on data X.
model = KMeans(n_clusters=[1])
model.fit(X)Setting n_clusters=3 tells the model to find 3 groups in the data.
Fix the error in the code to compute the silhouette score for clustering labels.
from sklearn.metrics import silhouette_score score = silhouette_score(X, [1])
The silhouette_score function needs the cluster labels, which are stored in model.labels_ after fitting.
Fill both blanks to create a list of inertia values for K from 1 to 5.
inertia_values = [] for k in range(1, 6): model = KMeans(n_clusters=[1]) model.fit(X) inertia_values.append([2])
We loop over k values and set n_clusters=k. The inertia (sum of squared distances) is accessed by model.inertia_.
Fill all three blanks to compute silhouette scores for K from 2 to 6 and store them in a dictionary.
silhouette_scores = {}
for k in range(2, 7):
model = KMeans(n_clusters=[1])
labels = model.fit_predict(X)
silhouette_scores[[2]] = [3](X, labels)We use k for the number of clusters and as the dictionary key. The silhouette score is computed by calling silhouette_score(X, labels).
