IndiaIndian Nationals
1800 210 2020
ForiegnForeign Nationals
+918068792934
logologo
Home
About
Director's Message
Blogs
HomeAbout
Director's MessageBlogs
Limited Seats Available

K Means Clustering in Machine Learning: Algorithm, Steps, Examples, and Applications

  1. Home
  2. Blog
  3. K Means Clustering in Machine Learning: Algorithm, Steps, Examples, and Applications

Quick Overview:

  • K Means clustering is an unsupervised machine learning algorithm that groups similar data points into a predefined number of clusters based on their distance from centroids.

  • It repeatedly assigns data points to the nearest centroid, recalculates the centroids, and continues until the clusters converge.

  • Techniques such as the elbow method and silhouette score help determine an appropriate number of clusters, while K-Means++ can improve centroid initialisation.

  • This blog covers K-Means algorithm in detail along with its pseudocode, Python implementation, applications, advantages, limitations, and comparisons with KNN.

What Is K Means Clustering?

K Means Clustering in Machine Learning is a type of unsupervised machine learning algorithm that groups data points into a specified number of clusters. The “K” in K Means Clustering is purely based on how similar these data points are to each other.  

Instead of being given the correct groups, the algorithm finds the groups itself by repeatedly assigning data points to nearby centroids (central points) until the groups stop changing. 

Understanding this through a k means clustering example​ or analogy makes the topic easy without going into technical depth first. Let’s understand it through an analogy:

Imagine you are sorting different fruits into 3 baskets.

  • You start by putting one fruit in each basket as a starting point. 

  • Then, you place each new fruit into the basket with the most similar fruit. 

  • After that, you adjust the baskets based on what’s inside them and repeat until the fruits are grouped well.

K-means does the same thing with data, it groups similar data points together based on distance.

Also Read: Support Vector Machine (SVM) in Machine Learning: Algorithm, Types, Kernels, and Examples

Now let’s understand the same with the help of k means clustering diagram below:

k means clustering diagram showing scattered points in three different colors, each color surrounded by a circle around its centroid, with the centroids marked clearly with an X or star symbolk means clustering diagram showing scattered points in three different colors, each color surrounded by a circle around its centroid, with the centroids marked clearly with an X or star symbol

Now you understand how K-Means groups similar data points, let’s see how it is different from KNN,  which is another popular machine learning algorithm.

K Means Clustering vs KNN

Although their names sound similar, K-Means and KNN are fundamentally different machine learning algorithms.

Feature

K-Means Clustering

KNN

Learning type

Unsupervised

Supervised

Main purpose

Group similar data points

Make predictions

Uses labelled data?

No

Yes

Meaning of K

Number of clusters

Number of neighbours

Output

Cluster assignment

Class or numerical prediction

Common use

Customer segmentation

Classification and regression

K Means clustering algorithm discovers patterns in unlabelled data, while KNN uses labelled examples to predict the outcome for a new observation.

Also Read: Gradient Boosting Algorithm in Machine Learning: How It Works, and Practical Examples

Key Concepts in K-Means Clustering

To understand the k means clustering algorithm, it is important to know concepts such as K, centroids, distance, the objective function, WCSS, and convergence. So now let’s discuss all these key concepts.

1. What Is K in K-Means?

K in K means clustering is simply the number of clusters the algorithm is asked to create. Unlike many other parameters in machine learning, K has to be decided before the algorithm runs at all.

For example, if:

K = 3

The algorithm attempts to divide the dataset into three clusters.

2. What Is a Centroid?

A centroid is the central point of a cluster, and it acts as that cluster's representative location in the feature space. Every data point in the dataset is assigned to whichever centroid it is closest to, and the position of each centroid is what actually defines the shape and membership of its corresponding cluster.

How Are Centroids Calculated?

Suppose a cluster contains three points:

  • A = (2, 4)

  • B = (4, 6)

  • C = (6, 8)

The centroid is calculated by taking the average of each coordinate:

Centroid = ((2 + 4 + 6) / 3, (4 + 6 + 8) / 3)

Centroid = (4, 6)

The k means clustering algorithm repeatedly updates these centroids as data points move between clusters.

Also Read: Random Forest Algorithm in Machine Learning: Working, Types, Pseudocode

3. How Does K-Means Calculate Distance?

K-Means commonly uses Euclidean distance to determine how close a data point is to each centroid.

For two points:

A = (x₁, x₂)
B = (y₁, y₂)

The Euclidean distance is: Distance = √[(x₁ − y₁)² + (x₂ − y₂)²]

4. What Is the Objective Function?

The objective function in K means clustering is the specific quantity the algorithm is trying to minimize as it runs. In practical terms, it is trying to minimize the total distance between every data point and the centroid of the cluster it belongs to, since a lower total distance means the clusters are tighter and more internally consistent.

This can be represented as:

J = Σᵢ Σⱼ ||xᵢ − μⱼ||²

where:

  • xᵢ represents a data point

  • μⱼ represents the centroid of its assigned cluster

  • J represents the total clustering error

5. What Is Within-Cluster Sum of Squares (WCSS)?

Within-cluster sum of squares, or WCSS, is the mathematical formula that quantifies how tightly grouped the points within each cluster are. Its formula is:

WCSS = Σᵢ Σₓ∈Cᵢ ||x − μᵢ||²

where Cᵢ represents a cluster and μᵢ represents its centroid.

A lower WCSS means that points are, on average, closer to their assigned centroids. WCSS is also commonly used with the elbow method to help determine a suitable value of K.

6. What Does Convergence Mean in K-Means?

Convergence in K means clustering is the point at which the algorithm stops making meaningful changes between iterations, either because no data points switch cluster assignment anymore, or because the centroids stop moving by more than a very small threshold. 

Once convergence is reached, the algorithm considers the clustering finished and returns the final cluster assignments and centroid positions. 

Also Read: Decision Tree Algorithm in Machine Learning: How It Works, Types, and Examples

Now let’s understand the algorithm more with k means clustering pseudocode​.

K Means Clustering Pseudocode

Understanding K means clustering pseudocode is a useful way to see the entire process of k means clustering in machine learning in the logical steps. 

Given a dataset X and a chosen number of clusters K:

1. Initialize K centroids, typically using K-means++ or a random selection of K data points

2. Repeat until convergence:

       a. Assignment step:

              For each data point in X, calculate its distance to every centroid

              Assign the data point to the cluster with the nearest centroid

       b. Update step:

              For each cluster, recalculate the centroid as the mean of all data points

              currently assigned to that cluster

       c. Check for convergence:

              If cluster assignments did not change, or centroid movement is below

              a small threshold, stop; otherwise repeat from the assignment step

3. Return the final cluster assignments and centroid positions

This pseudocode captures the two-step cycle, assigning points to the nearest centroid, then updating the centroids based on those assignments, that repeats over and over until the clustering stabilizes.

Also Read: Logistic Regression in Machine Learning: Algorithm, Types, Examples, and Applications

After exploring the K means clustering pseudocode and key concepts​, now let’s understand how the K means clustering algorithm​ works.

How Does the K Means Clustering Algorithm Work?

The k means clustering algorithm works by repeatedly assigning data points to their nearest centroid and then recalculating the centroids based on those assignments.

The algorithm follow these key steps:

Step 1: Choose the Number of Clusters

First, select the value of K. For example:

K = 3

This means the algorithm will create three clusters.

Step 2: Initialise the Centroids

The algorithm then selects three initial centroids from the dataset or generates them using an appropriate initialisation strategy.

The initial positions can affect the final clusters, which is one reason methods such as K-Means++ are useful.

Step 3: Calculate the Distance

The algorithm calculates the distance between every data point and each centroid. For example, if a point is closer to Centroid 1 than Centroids 2 and 3, it is assigned to Cluster 1.

Step 4: Assign Data Points to Clusters

Each observation is assigned to the cluster with the nearest centroid. The result might initially look like:

Cluster 1 → Points A, B, C
Cluster 2 → Points D, E, F
Cluster 3 → Points G, H, I

Step 5: Recalculate the Centroids

After assigning the points, K-Means calculates a new centroid for each cluster by taking the mean of the observations belonging to that cluster.

New Centroid = Average of all points assigned to the cluster

Step 6: Repeat the Process

The algorithm again calculates distances using the new centroids and reassigns the observations if necessary.

The process continues:

  • Initial Centroids

  • Calculate Distance

  • Assign Clusters

  • Recalculate Centroids

  • Repeat

Step 7: Stop When the Algorithm Converges

K-Means stops when the cluster assignments or centroids no longer change significantly, or when the maximum number of iterations is reached.

The final result is a set of K clusters where observations within each group are relatively similar according to the selected features.

Also Read: Supervised vs Unsupervised Learning: Differences, Examples, and Applications

Understanding the K Means Clustering working, helps you to understand the algorithm more clearly. Now let’s explore how to implement k means clustering algorithm​ using Python.

How to Implement K Means Clustering in Python

In this section you will get a complete K means clustering example in Python using scikit-learn, segmenting customers into groups based on age and annual income.

Import Required Libraries

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler

from sklearn.cluster import KMeans

from sklearn.metrics import silhouette_score

Load and Explore the Dataset

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 across three natural age and income groupings, a common setup for a k means clustering example in customer segmentation.

Prepare and Scale the Data

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

Scaling is essential here, since K means clustering relies entirely on distance calculations, and without scaling, a feature like annual income, measured in the tens of thousands, would completely dominate a feature like age, measured in single or double digits.

Select the Number of Clusters

wcss = []

for k in range(1, 8):

    km = KMeans(n_clusters=k, init="k-means++", n_init=10, random_state=42)

    km.fit(X_scaled)

    wcss.append(km.inertia_)


plt.plot(range(1, 8), wcss, marker="o")

plt.xlabel("Number of Clusters (K)")

plt.ylabel("WCSS")

plt.title("Elbow Method for Optimal K")

plt.show()

Expected Output:

Elbow plot showing WCSS dropping sharply through K=3, then flattening; K=3 is recommended.Elbow plot showing WCSS dropping sharply through K=3, then flattening; K=3 is recommended.

for k in range(2, 6):

    km = KMeans(n_clusters=k, init="k-means++", n_init=10, random_state=42)

    labels = km.fit_predict(X_scaled)

    score = silhouette_score(X_scaled, labels)

    print(f"k={k}, silhouette score={score:.4f}")

Expected Output: 

k=2, silhouette score=0.6141

k=3, silhouette score=0.6635

k=4, silhouette score=0.5608

k=5, silhouette score=0.5892

The silhouette score offers a second, independent check, measuring how well-separated and internally consistent each cluster is on a scale from -1 to 1, with values closer to 1 indicating better-defined clusters. 

Train the K-Means Model

kmeans = KMeans(n_clusters=3, init="k-means++", n_init=10, random_state=42)

kmeans.fit(X_scaled)

Here, n_clusters=3 reflects the choice confirmed by both the elbow method and the silhouette score above, and init="k-means++" uses the smarter centroid initialization technique covered earlier rather than pure random placement.

Assign Data Points to Clusters

labels = kmeans.predict(X_scaled)

df["Cluster"] = labels

print(df.groupby("Cluster")[["Age", "AnnualIncome"]].mean())

Expected Output: 

Cluster   Age      AnnualIncome                    

0        35.779596  60630.903278

1        49.928639  89112.156199

2        19.676888  30384.317437

Evaluate and Visualise the Clusters

print("Inertia (WCSS):", kmeans.inertia_)

print("Cluster sizes:", np.bincount(labels))

print("Final silhouette score:", silhouette_score(X_scaled, labels))


plt.scatter(df["Age"], df["AnnualIncome"], c=labels, cmap="viridis", alpha=0.7)

centroids_original_scale = scaler.inverse_transform(kmeans.cluster_centers_)

plt.scatter(

    centroids_original_scale[:, 0], centroids_original_scale[:, 1],

    c="red", marker="X", s=200, label="Centroids"

)

plt.xlabel("Age")

plt.ylabel("Annual Income")

plt.title("K-Means Clustering: Customer Segments")

plt.legend()

plt.show()

Expected Output:

K-means scatter plot showing three customer segments: young low-income, middle-aged mid-income, and older high-income customers, with centroids marked.K-means scatter plot showing three customer segments: young low-income, middle-aged mid-income, and older high-income customers, with centroids marked.

This full implementation of k means clustering in machine learning​ using Python make the topic more clear. Now let's understand what are some pros and cons of k means clustering algorithm.

Also Read: Linear Regression in Machine Learning: Types, Algorithm, and Implementation

Advantages and Limitations of K Means Clustering

Like other machine learning algorithms, K Means has both strengths and limitations. Understanding them can help you decide whether it is suitable for a particular clustering problem.

Advantages of K-Means

  1. Simple to understand: The assign-and-update process is relatively easy to understand.

  2. Easy to implement: Libraries such as scikit-learn provide ready-to-use implementations.

  3. Computationally efficient: K-Means can work efficiently on many datasets.

  4. Scalable: It can be applied to relatively large datasets.

  5. Easy to interpret: The resulting clusters and their centroids can often be explained clearly.

Limitations of K-Means

  1. Requires K in advance: The algorithm needs the number of clusters to be specified.

  2. Sensitive to initialisation: Different initial centroid positions can sometimes produce different results.

  3. Sensitive to outliers: Extreme observations can influence the centroid positions.

  4. Works best with suitable cluster shapes: K-Means may perform poorly when clusters have irregular or very different shapes.

  5. Sensitive to feature scaling: Features with larger numerical ranges can disproportionately affect distance calculations.

Real-World Applications of K Means Clustering

K means clustering is used across a wide range of industries wherever grouping similar observations together provides genuine business or analytical value.

Industry

Problem

How K-Means Is Used

Example Outcome

Marketing

Customer segmentation

Groups customers based on purchasing behaviour

High-, medium-, and low-value customer groups

Retail

Customer analysis

Clusters customers using spending patterns

Targeted customer segments

E-commerce

Product analysis

Groups products based on characteristics

Similar product categories

Healthcare

Patient segmentation

Groups patients with similar characteristics

Patient risk or behaviour groups

Finance

Customer profiling

Groups customers based on financial behaviour

Distinct customer segments

Education

Student analysis

Groups students based on learning patterns

Similar learning behaviour groups

Image Processing

Image segmentation

Groups pixels based on similar features

Distinct image region

Conclusion

Now that we have understood this, K means clustering remains one of the most practical starting points for unsupervised learning, especially because its core idea, grouping data around iteratively refined centroids and it is both mathematically simple and genuinely effective across a wide range of real-world datasets.

Understanding centroids, WCSS, the elbow method, and the assignment-and-update cycle covered in this blog gives a solid foundation for applying K means clustering confidently. Whether the goal is customer segmentation, image compression, or simply exploring a new dataset to see what natural groupings it contains.

Frequently Asked Questions

General

Ready to Take the Next Step? Enroll Today!

Ready to Take the Next Step? Enroll Today!

© Copyright 2026 of IITKGP | All Rights Reserved Privacy Policy