Determining Optimal K using Silhouette Method

 

Experiment

Determining Optimal K using Silhouette Method


๐ŸŽฏ Objective

To determine the optimal number of clusters (K) using the Silhouette Method.


๐Ÿ“˜ Theory

๐Ÿ”น Why Silhouette Method?

Unlike the Elbow Method, which may be ambiguous, the Silhouette Method provides a quantitative measure of clustering quality.


๐Ÿ”น Silhouette Score

It measures how well each data point fits within its cluster.

  • a(i) = average distance to points in same cluster
  • b(i) = average distance to points in nearest cluster

๐Ÿ”น Silhouette Formula

s(i)=b(i)−a(i)max⁡(a(i),b(i))s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))}


๐Ÿ”น Interpretation

ScoreMeaning
≈ 1    Well clustered
≈ 0    Overlapping clusters
< 0    Misclassified

๐Ÿ”น Working Principle

  1. Run K-Means for different values of K
  2. Compute average silhouette score
  3. Choose K with highest score

๐Ÿงพ Sample Dataset

PointX    Y
P12    3
P23    4
P33    3
P48    7
P57    8
P68    8
P715    16
P8       16    15
P9       15    15

๐Ÿ’ป Program (Python Code)

import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score # Step 1: Dataset
X = np.array([
    [2, 3], [3, 4], [3, 3],
    [8, 7], [7, 8], [8, 8],
    [15, 16], [16, 15], [15, 15]
])
# Step 2: Compute Silhouette Scores sil_scores = [] K_range = range(2, 7) # silhouette requires at least 2 clusters for k in K_range: kmeans = KMeans(n_clusters=k, init='k-means++', random_state=0) labels = kmeans.fit_predict(X) score = silhouette_score(X, labels) sil_scores.append(score) # Step 3: Plot graph plt.plot(K_range, sil_scores, marker='o') plt.title("Silhouette Method") plt.xlabel("Number of Clusters (K)") plt.ylabel("Silhouette Score") plt.show() # Step 4: Print scores for k, score in zip(K_range, sil_scores): print(f"K = {k}, Silhouette Score = {score}")

๐Ÿ“ŠOutput

K = 2, Silhouette Score = 0.7519172781342643 K = 3, Silhouette Score = 0.8503109587689267 K = 4, Silhouette Score = 0.5843521134750496 K = 5, Silhouette Score = 0.33839040555236954 K = 6, Silhouette Score = 0.30584671457309703





๐Ÿ“ˆ Graph Interpretation

  • The value of K with the highest silhouette score is optimal
  • Graph shows peak at a specific K

๐Ÿ‘‰ For this dataset:

✅ Optimal K = 3


๐Ÿ“Œ Result

Using the Silhouette Method, the optimal number of clusters was found to be:

K = 3

  • Silhouette Method provides a clear numerical metric
  • More reliable than Elbow Method in many cases
  • Helps evaluate clustering quality

Comments

Popular posts from this blog

Machine Learning Lab PCCSL508 Semester 5 KTU CS 2024 Scheme manual - Dr Binu V P

Lab Assignment-2

Lab Assignment-1