Clustering / Unsupervised Learning
K-Means Clustering
2023-12-20
K-means is a widely used unsupervised clustering method. Although the number of clusters must be specified in advance, the silhouette or elbow method can help select a suitable number. K-means is simple and efficient.
The basic concept is continuously updating the position of each cluster's centroid until the cluster converges (i.e., the contents of the cluster do not change). The progression of the algorithm follows the steps as shown in the figure below.
Step 0. Assign the initial number of clusters
Step 1. Randomly select the initial Centroid locations (or according to the K means++ method to increase convergence speed)
Step 2. Each data is assigned to the nearest Centroid
Step 3. Update the position of the Centroid using the assigned data
Step 4. Repeat Steps 2 and 3 until there is no change in each cluster.


The typical definition used for calculating distances between each data from the centroid is the Euclidean distance, which is defined as follows in three dimension case.
If the position of the centroid is (xm, ym, zm), the distance d is
\(d=\sqrt{(x-x_m)^2+(y-y_m)^2+(z-z_m)^2}\)
In the Excel worksheet attached, K-means clustering is illustrated by using Iris dataset.
Step 0. Assumed the number of clusers = 3 (demonstration purpose)
Step 1. Generate random numbers as the initial positions of the three centroids,
Step 2. Calculate the distance per sample per Centroid and rearrange Clusters.
=SQRT(SUMXMY2(Centroid, X))

Update clusters by using MATCH function
INDEX(range, row, column) return the value at range(row,column)
MATCH(value, range, 0) return the position of value exactly matching in the cell range

Step 3. Update the location of Centroids
=AVERAGEIFS(Target Range, Condition Range, Condition) return the average of the samples satisfying condition = Cells in Condition Range.

Step 4. Copy and Paste the Step 2 & 3 until no change in the Cluster column.
The term so-called "inertia" is used to evaluate the performance of K-Means clustering. It is defined by the following equation: The sum of the squares of the distance of all the data to its corresponding centroid.
\(L=\sum _{\ }^{\ }d^2\)
The optimal number of clusters can be inferred through the elbow method, where the elbow-like bend in the change of 'L' relative to the number of clusters is observed.
Below is a demonstration using the Iris dataset which is included in the Excel file attached, showing the relationship between the number of clusters and Inertia. As the correct answer is given, i.e. the number of classes in the dataset equals 3, but the elbow method inclined to 4 clusters.

The other metric to use to find optimal number of clusters is so-called "silhouette score", which known to be more accurately.
A silhouette score is the overall mean of the silhouette coefficients defined as below.
Silhouette Coefficient = (b-a)/max(b,a)
where a is the average distance between samples in the same cluster, and b is the average distance to samples in the closest other cluster.
A silhouette diagram represents the silhouette coefficient values for all samples in each cluster. The higher the silhouette score, the better the cluster.
<Note> Following is the simple copy and paste from the Wiki page for K-means++ for your reference.
The exact algorithm is as follows:
- Choose one center uniformly at random among the data points.
- For each data point x not chosen yet, compute D(x), the distance between x and the nearest center that has already been chosen.
- Choose one new data point at random as a new center, using a weighted probability distribution where a point x is chosen with probability proportional to D(x)2.
- Repeat Steps 2 and 3 until k centers have been chosen.
- Now that the initial centers have been chosen, proceed using standard k-means clustering.