Collect the data: Start with a dataset which contains the relevant features.
Prepare the data: Clean the dataset and handle missing values or inconsistent observations.
Select features: Choose the variables that will be used to identify similarities between observations.
Choose a clustering algorithm: Select an appropriate method based on the dataset and objective.
Calculate similarity or distance: The clustering algorithms in machine learning determine how close or similar different observations are.
Create clusters: The similar observations are then grouped together.
Evaluate the results: Examine whether the resulting clusters are meaningful and useful.
For example, K means clustering in machine learning assigns observations to nearby cluster centres and repeatedly updates those centres until the groups stabilise.
Now let’s look at the different clustering in machine learning types to understand the clustering more clearly.
Also Read: Supervised vs Unsupervised Learning: Differences, Examples, and Applications
Types of Clustering in Machine Learning
There are different types of clustering in Machine Learning, and each type uses a different approach to group similar data points.
Let’s explore each of them in detail.
1. Partitioning Clustering
Partitioning clustering divides a dataset into a fixed number of non-overlapping groups, where every data point belongs to exactly one cluster. The number of clusters, usually called K, has to be specified in advance, and the algorithm then works to find the partition of the data that best satisfies its internal objective.
K Means clustering in machine learning is a common example of partitioning clustering. It requires the number of clusters to be specified before the algorithm is trained.
2. Hierarchical Clustering in Machine Learning
Hierarchical clustering in machine learning creates a hierarchy of clusters by progressively combining similar observations or dividing larger groups into smaller ones.
The resulting hierarchy can be represented using a tree-like diagram called a dendrogram, which helps visualise relationships between observations and determine suitable cluster groupings.
Best for: Biological data analysis, document grouping, customer segmentation, and small to medium-sized datasets.
3. Density-Based Clustering
Density-based clustering groups data points based on how closely they are packed together. It looks for areas with many nearby data points and treats these as clusters.
Unlike methods that expect clusters to be round or circular, density-based methods such as DBSCAN can find clusters of different shapes. They can also identify data points that are far away from other points as noise or outliers instead of forcing them into a cluster.
4. Model-Based Clustering
Model-based clustering is a types of clustering in machine learning that assumes that the data comes from different underlying statistical groups. The algorithm builds a model for these groups and calculates how likely each data point is to belong to each one.
Gaussian Mixture Model (GMM) is a common example of model-based clustering. Unlike methods that simply assign a data point to the nearest cluster centre, GMM gives each point a probability of belonging to different clusters.
5. Centroid-Based Clustering
Centroid-based clustering groups data points based on their distance from a central point called a centroid. Each data point is assigned to the centroid that is closest to it.
K Means clustering in machine learning is the most common example. It starts with some centroids, assigns data points to their nearest centroid, then moves the centroids based on the assigned points. This process continues until the centroids and clusters become stable.
Now after discussing the different types of clustering in machine learning, let's explore what major clustering algorithms in machine learning are available for different use cases.
Also Read: Random Forest Algorithm in Machine Learning: Working, Types, Pseudocode
Major Clustering Algorithms in Machine Learning
Different clustering algorithms in machine learning are designed for different types of datasets and clustering requirements. Some focus on distance from a centre, while others identify hierarchical relationships, density patterns, or probability distributions.
Let’s understand each of them in detail.
1. K-Means Clustering
K means clustering in machine learning is a centroid-based, partitioning algorithm that groups data into a specified number of clusters, K, by repeatedly assigning points to their nearest centroid and then recalculating each centroid as the mean position of the points assigned to it.
It is fast and scales well to large datasets, and tends to work especially well when clusters in the data are roughly spherical and similarly sized.
2. DBSCAN
DBSCAN, short for Density-Based Spatial Clustering of Applications with Noise, is the most widely used density-based clustering algorithms in machine learning. It groups together points that are closely packed, based on two parameters, eps, the maximum distance between two points for them to be considered neighbors, and min_samples, the minimum number of neighboring points required for a region to count as dense.
Unlike K means clustering, DBSCAN does not require the number of clusters to be specified in advance, and it can naturally identify outliers as noise points that do not belong to any cluster at all.
3. Gaussian Mixture Model
A Gaussian Mixture Model, or GMM, is a model-based clustering algorithm that assumes the data was generated from a mixture of several Gaussian distributions, each representing one cluster. Rather than assigning each point to exactly one cluster the way K means clustering does, a GMM calculates the probability that each point belongs to each cluster, a concept known as soft clustering.
This makes GMM a good fit for datasets where cluster boundaries genuinely overlap, or where understanding the uncertainty of a cluster assignment matters as much as the assignment itself.
Also Read: Gradient Boosting Algorithm in Machine Learning: How It Works, and Practical Examples
Now let’s understand how to perform clustering in machine learning using Python.
How to Perform Clustering in Machine Learning Using Python?
In this section you will walk through a complete clustering in machine learning python example using scikit-learn, comparing K-means clustering, hierarchical clustering, DBSCAN, and a Gaussian Mixture Model on the same dataset.
1. Load and Explore the Dataset
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans, AgglomerativeClustering, DBSCAN
from sklearn.mixture import GaussianMixture
from sklearn.metrics import silhouette_score
np.random.seed(42)
n = 300
cluster1 = np.random.normal(loc=[20, 30000], scale=[3, 5000], size=(n // 3, 2))
cluster2 = np.random.normal(loc=[35, 60000], scale=[4, 8000], size=(n // 3, 2))
cluster3 = np.random.normal(loc=[50, 90000], scale=[5, 10000], size=(n // 3, 2))
X = np.vstack([cluster1, cluster2, cluster3])
df = pd.DataFrame(X, columns=["Age", "AnnualIncome"])
print(df.head())
print(df.describe())
This creates a sample dataset of 300 customers spread across three natural age and income groupings, the same kind of customer segmentation scenario used throughout this series.
Also Read: Support Vector Machine (SVM) in Machine Learning: Algorithm, Types, Kernels, and Examples
2. Prepare and Scale the Data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
Scaling matters for every distance-based clustering algorithm, since features on very different numeric scales, like age and annual income here, would otherwise distort how "close" two points really are to each other.
3. Select a Clustering Algorithm
kmeans = KMeans(n_clusters=3, init="k-means++", n_init=10, random_state=42)
hierarchical = AgglomerativeClustering(n_clusters=3, linkage="ward")
dbscan = DBSCAN(eps=0.25, min_samples=5)
gmm = GaussianMixture(n_components=3, random_state=42)
Each algorithm is set up to look for three clusters, except DBSCAN, which discovers the number of clusters on its own based on density rather than being told a target number directly.
4. Train the Clustering Model
kmeans_labels = kmeans.fit_predict(X_scaled)
hierarchical_labels = hierarchical.fit_predict(X_scaled)
dbscan_labels = dbscan.fit_predict(X_scaled)
gmm_labels = gmm.fit_predict(X_scaled)
5. Evaluate the Clusters
print("K-Means silhouette score:", silhouette_score(X_scaled, kmeans_labels))
print("Hierarchical silhouette score:", silhouette_score(X_scaled, hierarchical_labels))
n_clusters_db = len(set(dbscan_labels)) - (1 if -1 in dbscan_labels else 0)
n_noise = list(dbscan_labels).count(-1)
mask = dbscan_labels != -1
print("DBSCAN clusters found:", n_clusters_db, "| noise points:", n_noise)
print("DBSCAN silhouette score (excluding noise):", silhouette_score(X_scaled[mask], dbscan_labels[mask]))
print("GMM silhouette score:", silhouette_score(X_scaled, gmm_labels))
Running this comparison on the sample dataset produces silhouette scores of roughly 0.66 for K means clustering, 0.65 for hierarchical clustering in machine learning, and 0.66 for the Gaussian Mixture Model, all very close to each other since the underlying data genuinely contains three well-separated groups. DBSCAN, with its parameters tuned for this dataset, correctly finds 3 clusters while also flagging 14 points as noise, a result none of the other three algorithms are able to produce, since they force every point into a cluster regardless of how well it actually fits.
6. Visualise the Results
fig, axes = plt.subplots(1, 3, figsize=(18, 5))
axes[0].scatter(df["Age"], df["AnnualIncome"], c=kmeans_labels, cmap="viridis")
axes[0].set_title("K-Means Clustering")
axes[1].scatter(df["Age"], df["AnnualIncome"], c=hierarchical_labels, cmap="viridis")
axes[1].set_title("Hierarchical Clustering")
axes[2].scatter(df["Age"], df["AnnualIncome"], c=dbscan_labels, cmap="viridis")
axes[2].set_title("DBSCAN (noise in dark purple)")
for ax in axes:
ax.set_xlabel("Age")
ax.set_ylabel("Annual Income")
plt.tight_layout()
plt.show()
Expected Output: