Prerequisites: K-Means Clustering
A fundamental step for any unsupervised algorithm is to determine the optimal number of clusters into which the data may be clustered. The Elbow Method is one of the most popular methods to determine this optimal value of k.
We now demonstrate the given method using the K-Means clustering technique using the Sklearn library of python.
Step 1: Importing the required libraries
Step 2: Creating and Visualizing the data
From the above visualization, we can see that the optimal number of clusters should be around 3. But visualizing the data alone cannot always give the right answer. Hence we demonstrate the following steps.
We now define the following:-
- Distortion: It is calculated as the average of the squared distances from the cluster centers of the respective clusters. Typically, the Euclidean distance metric is used.
- Inertia: It is the sum of squared distances of samples to their closest cluster center.
We iterate the values of k from 1 to 9 and calculate the values of distortions for each value of k and calculate the distortion and inertia for each value of k in the given range.
Step 3: Building the clustering model and calculating the values of the Distortion and Inertia
Step 4: Tabulating and Visualizing the results
a) Using the different values of Distortion
b) Using the different values of Inertia
To determine the optimal number of clusters, we have to select the value of k at the “elbow” ie the point after which the distortion/inertia start decreasing in a linear fashion. Thus for the given data, we conclude that the optimal number of clusters for the data is 3.
The clustered data points for different value of k:-
1. k = 1
2. k = 2
3. k = 3
4. k = 4
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- ML | Determine the optimal value of K in K-Means Clustering
- Silhouette Algorithm to determine the optimal value of k
- Choose optimal number of epochs to train a neural network in Keras
- Python - Extract ith Key's Value of K's Maximum value dictionary
- PyQt5 QSpinBox – Step up the value using stepUp method
- PyQt5 QSpinBox – Step down the value using stepDown method
- PyQt5 QSpinBox – Changing value using stepBy method
- What is the maximum possible value of an integer in Python ?
- Python | Extract key-value of dictionary in variables
- Mahotas - Getting Mean Value of Image
- Python | Check if all the values in a list that are greater than a given value
- Python Regex to extract maximum numeric value from a string
- Python | Check if all the values in a list are less than a given value
- Python | Ways to Spilt the list by some value
- Python | Sort Python Dictionaries by Key or Value
- Python | Select random value from a list
- Python - Tuple key detection from value list
- Python | Accessing Key-value in Dictionary
- Python | Accessing index and value in list
- Python | Ways to find indices of value in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.