liveBook · Manning

Silhouette coefficient

As explained in Cyber Threat Hunting

The silhouette coefficient is a clustering metric that compares an observation's cohesion within its assigned cluster with its separation from neighboring clusters, ranging from -1 to 1.

The silhouette coefficient measures how well each observation fits its assigned cluster by comparing cohesion within that cluster with separation from the nearest cluster. For a point, a is its mean intracluster distance and b is its mean distance from the nearest other cluster; the score is (b − a) / max(a, b). Dataset scores average the point scores and range from −1 to 1. section 8.4

Use silhouette analysis to assess candidate k values in a repeatable workflow:

  • Evaluate each candidate k using average silhouette scores; higher values generally indicate denser, better-separated clusters. section 8.4
  • Interpret scores near 1 as strong separation and scores near −1 as possible misassignment. section 8.4
  • Compare silhouette results with the elbow method, especially when the elbow point is unclear, while accounting for substantially higher computation. section 8.4 section 8.7

Selecting clusters for network-connection analysis

A k-means investigation of network connections used the elbow method to select k while isolating small clusters associated with malicious beaconing. Silhouette analysis provides an alternative or confirmation: one run selected k = 5, although the highest plotted score occurred at k = 10, illustrating why candidate selection and plot interpretation should be compared. section 8.3.2 section 8.7

Listing 8.28 Applying Silhouette analysis to df_features_one_hot_enc
df_features_one_hot_enc = df_features_one_hot_enc.drop(['cluster'],\
    axis='columns')                                                 #1
visualizer = KElbowVisualizer(model, k=(2,10), metric='Silhouette')
 
visualizer.fit(df_features_one_hot_enc)
visualizer.show()
plt.show()

Run silhouette analysis across k = 2 through 10 with Yellowbrick's KElbowVisualizer and inspect the resulting plot. section 8.4

How Cyber Threat Hunting covers it

How deep it goes
Defines per-point distances, gives the formula and range, and explains computational cost without deriving distance implementation.
Examples
Demonstrates k ranges 2–10 and 2–12 with plots, timing comparisons, and a k = 10 cluster-size countplot.
What it assumes
Assumes k-means and candidate cluster counts, while defining cohesion, separation, and coefficient components explicitly.
What you can do after
Evaluate candidate cluster counts with KElbowVisualizer and compare silhouette analysis against the elbow method.
How it connects
Connects k-means, the elbow method, cluster density, intercluster distance, and Yellowbrick KElbowVisualizer.

Also explained in

Compare how 2 books explain silhouette coefficient →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Cyber Threat Hunting. The section links point to the parts of the book it draws on.