K-means needs a number of clusters, k, before it can run. The elbow method helps choose a candidate: fit the model at several k values, plot a within-cluster measure against k, and look for a sharp bend. That bend suggests a useful cluster count, not an automatic final answer. section 2.3.3
A practical selection workflow:
- Inspect the data and standardize the features before comparing clusters. Features on different scales can otherwise dominate distance calculations. section 2.3.5
- Fit K-means across candidate k values and plot the resulting scores. The conceptual illustration uses within-cluster sum of squares (WCSS); the supplied Python example instead plots average Euclidean distortion, so its values should not be labeled WCSS. section 2.3.3 section 2.3.5
- Inspect the bend, then check cluster sizes, computation cost, and whether business logic calls for merging or splitting clusters. The silhouette coefficient offers another way to assess the choice. section 2.3.3
Vehicle clustering
A Python workflow standardizes vehicle features, compares candidate values of k, and identifies a sharp bend at k = 3. It then fits a three-cluster K-means model and examines the resulting centroids and labels. Three suits this dataset; it is not a universal setting. section 2.3.5
from sklearn.cluster import KMeans
from scipy.spatial.distance import cdist
clusters=range(1,10)
meanDistortions=[]
for k in clusters:
model=KMeans(n_clusters=k)
model.fit(X_standard)
prediction=model.predict(X_standard)
meanDistortions.append(sum(np.min(cdist(X_standard,
model.cluster_centers_, 'euclidean'), axis=1)) / X_standard
.shape[0])
plt.plot(clusters, meanDistortions, 'bx-')
plt.xlabel('k')
plt.ylabel('Average distortion')
plt.title('Selecting k with the Elbow Method')
The Python listing fits KMeans repeatedly and plots average distortion against k; inspect the plotted measure before interpreting its elbow. section 2.3.5
Hierarchical clustering handles this decision differently: it does not require k as an algorithm input, but a cut through its dendrogram still determines how many final clusters to retain. section 2.4.3




