The elbow method estimates the k value for k-means when a scatterplot does not reveal clear groups. Run k-means across a range of cluster counts, calculate the sum of squared errors (SSE), or distortion score, for each run, and plot SSE against k. Choose the bend where SSE stops dropping sharply: additional clusters then provide comparatively little improvement. section 8.2.3
Use the method as a repeatable selection workflow:
- Choose a plausible range of k values, such as 2 through 9 or 2 through 12. section 8.2.3 section 8.7
- Fit k-means for every candidate k and collect each run’s intracluster SSE. section 8.2.3
- Inspect the SSE-versus-k plot for the elbow, where the earlier rapid decline levels off. section 8.2.3
- Treat the result as an estimate: an elbow can be visually unclear, and changing the tested range can change the value selected by a visualizer. section 8.2.3 section 8.7
- Compare the choice with Silhouette analysis when confidence in k matters; the elbow method can be substantially faster because it uses intracluster rather than intra- and intercluster distances. section 8.3.2 section 8.7
Listing 8.15 Applying the elbow method to df_features_one_hot_enc
from yellowbrick.cluster import KElbowVisualizer #1 from sklearn.cluster import KMeans #2 model = KMeans() #3 visualizer = KElbowVisualizer(model, k=(2,10)) #4 visualizer.fit(df_features_one_hot_enc) #5 visualizer.show() #6
This Python workflow couples scikit-learn KMeans with Yellowbrick’s KElbowVisualizer, fits encoded features over k=2–9, and renders the decision plot. section 8.2.3
Selecting clusters for network-traffic investigation
Applied to one-hot-encoded network features, the elbow plot identifies a k value before cluster investigation begins. Using four clusters enabled analysis of small clusters that exposed malicious beaconing activity, while also illustrating that k-means can distribute malicious connections across multiple clusters. section 8.2.3 section 8.3.2
Use the elbow as evidence rather than an absolute answer. With k values 2–9, one run selected k=4; over 2–11, the visualizer selected k=5 after comparing score differences. A same-range exercise measured the elbow method at 2.88 seconds versus 36 minutes 57 seconds for Silhouette analysis on one system. section 8.7




