The basic step for any unsupervised algorithm is to determine the optimal number of clusters into which the data can be clustered. The elbow method is one of the most popular methods for determining this optimal k-value.
We will now demonstrate this method using the K-Means clustering technique using the Python Sklearn .
Step 1: Import required libraries
|
Step 2: Create and visualize data
|
From the above visualization, we can see that the optimal number of clusters should be about 3. But data visualization alone may not always provide the correct answer. From here, we demonstrate the following steps.
We now define the following:
- Distortion: is calculated as the mean square of the distance from the cluster centers of the respective clusters. Typically, the Euclidean distance metric is used.
- Inertia: is the sum of the squared distances of the samples to their closest cluster center.
We iterate over the k values from 1 to 9 and calculate the distortion values for each k-value and calculate the distortion and inertia for each k-value in the given range.
Step 3: Build the clustering model and calculate the distortion and inertia values
|
Step 4 : Tabulating and visualizing results
a) Using different distortion values
|
| < / tr>
b) Using different inertia values
|
|
To determine the optimal number of clusters, we must select the k value in the "knee", then is at the point after which distortion / inertia begins to decrease linearly. So for the given data, we conclude that the optimal number of clusters for the data is 3 .
The clustered data points to a different k value: —
1. k = 1
2. k = 2
3. k = 3
4. k = 4