What distance metric is used in K-Means and why?

Updated May 16, 2026

Short answer

K-Means primarily uses Euclidean distance to measure the similarity between data points and cluster centroids. It uses this distance because the algorithm is designed to minimize the squared distance between points and their assigned centroid, producing compact and well-separated clusters. Euclidean distance works best when features are numeric, scaled appropriately, and clusters are roughly spherical.

Deep explanation

K-Means is a distance-based clustering algorithm that groups data points by finding the nearest cluster center (centroid). The standard distance metric used in K-Means is Euclidean distance, specifically the squared Euclidean distance.

For a data point x and centroid μ, the Euclidean distance is:

[ d(x, \mu) = \sqrt{\sum_{i=1}^{n}(x_i-\mu_i)^2} ]

where:

  • x_i is the value of feature i for the data point.
  • μ_i is the value of feature i for the centroid.
  • n is the number of features.

K-Means minimizes the within-cluster sum of squared errors (SSE):

[ SSE = \sum_{k=1}^{K}\sum_{x \in C_k}||x-\mu_k||^2 ]

This means the algorithm tries to make points in each cluster as close as possible to their centroid.

Why Euclidean Distance Is Used

1. It Matches the K-Means Objective Function

K-Means updates centroids by calculating the mean of points assigned to each cluster.

Example:

TEXT
Points in cluster:
(2, 4)
(4, 6)
(6, 8)
Centroid:
((2+4+6)/3, (4+6+8)/3)
= (4, 6)

The mean minimizes the sum of squared Euclidean distances between points and the centroid. This mathematical relationship is the main reason Euclidean distance is used.

2. It Works Well for Compact, Spherical Clusters

Euclidean distance naturally creates circular boundaries in two dimensions and spherical boundaries in higher dimensions.

Example:

TEXT
● ● ●
● C ●
● ● ●

where C is the centroid.

This works well for datasets where clusters are dense and centered around a mean value.

3. It Is Simple and Computationally Efficient

K-Means repeatedly calculates distances between:

  • Every data point.
  • Every centroid.

For a dataset with n points, k clusters, and d dimensions, distance calculations are approximately:

[ O(nkd) ]

Euclidean distance is efficient to compute, making K-Means scalable to large datasets.

Why Squared Euclidean Distance Is Used Instead of Normal Euclidean Distance

K-Means usually minimizes squared Euclidean distance:

[ ||x-\mu||^2 ]

instead of:

[ ||x-\mu|| ]

The square root operation in normal Euclidean distance does not change which centroid is closer, but removing it:

  • Makes calculations faster.
  • Matches the mathematical optimization objective.
  • Penalizes larger errors more strongly.

Example:

TEXT
Distance error:
Small error:
2² = 4
Large error:
10² = 100

Large deviations have a much greater effect, encouraging compact clusters.

Importance of Feature Scaling

Euclidean distance is sensitive to feature magnitudes.

Example:

TEXT
Feature 1: Age
Range: 18 - 80
Feature 2: Income
Range: 10,000 - 1,000,000

Without scaling, income dominates the distance calculation because its values are much larger.

A common solution is standardization:

[ z = \frac{x-\mu}{\sigma} ]

Example using Python:

Python
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

After scaling, features contribute more equally to clustering.

Can Other Distance Metrics Be Used in K-Means?

The standard K-Means algorithm assumes Euclidean distance. Other distance metrics can sometimes be used with modified algorithms, but they may no longer have the same mathematical properties.

Examples:

Manhattan Distance

Manhattan distance is:

[ d(x,y)=\sum_i |x_i-y_i| ]

It measures distance by moving along coordinate axes.

It can be useful for some datasets, but standard K-Means is not designed for it because the centroid update step based on the mean is no longer optimal.

Cosine Distance

Cosine similarity measures the angle between vectors rather than their magnitude.

It is useful for:

  • Text embeddings.
  • High-dimensional sparse data.

However, standard K-Means with cosine distance requires modifications, often called spherical K-Means.

When Euclidean Distance Is a Poor Choice

K-Means with Euclidean distance may perform poorly when:

  • Features have very different scales.
  • Data contains many outliers.
  • Clusters have irregular shapes.
  • Data is categorical.
  • Similarity is not based on geometric distance.

For example:

TEXT
Good use case:
Customer age + spending amount
Poor use case:
Customer name + product category

Categorical variables need appropriate encoding or different clustering approaches.

Real-world example

A retail company wants to group customers based on:

  • Annual spending.
  • Number of purchases.

Each customer is represented as a point:

TEXT
Customer A: (5000, 20)
Customer B: (7000, 25)
Customer C: (1000, 5)

K-Means uses Euclidean distance to find customers that are close together.

Python
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
X = [
[5000, 20],
[7000, 25],
[1000, 5],
[1200, 6]
]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
model = KMeans(
n_clusters=2,
random_state=42
)
model.fit(X_scaled)
print(model.labels_)

The algorithm may identify:

TEXT
Cluster 0:
High spending customers
Cluster 1:
Low spending customers

This works because spending and purchase behavior are numeric features where Euclidean distance represents similarity reasonably well.

Common mistakes

  • * Assuming K-Means can use any distance metric without changing the algorithm.
  • * Forgetting to scale features before using Euclidean distance.
  • * Using Euclidean distance on categorical data without proper encoding.
  • * Ignoring outliers that can significantly affect centroid locations.
  • * Believing that Euclidean distance always represents real-world similarity.
  • * Confusing Euclidean distance with cosine similarity, which measures direction rather than physical distance.

Follow-up questions

  • Why does K-Means minimize squared Euclidean distance instead of regular Euclidean distance?
  • Why is feature scaling necessary before applying K-Means?
  • What happens if you use Manhattan distance instead of Euclidean distance in K-Means?
  • Why does K-Means perform poorly with non-spherical clusters?
  • How does the choice of distance metric affect clustering results?
  • When would cosine distance be preferred over Euclidean distance?

More K-Means Clustering interview questions

View all →