Euclidean Distance
Cosine Similarity
Normalization
Data Scaling
Machine Learning

How to convert Euclidean distance to range 0 and 1 like Cosine Similarity?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In machine learning and data analysis, similarity measures play a crucial role. Among these, Cosine Similarity is a popular choice for tasks like document comparison and clustering. It yields values between 0 and 1, where 1 indicates complete similarity and 0 no similarity, offering an intuitive understanding of how similar entities are. Conversely, Euclidean Distance, which measures the "straight-line" distance between two points in multi-dimensional space, yields a non-normalized distance value. This article explains how to scale Euclidean Distance into a range of [0, 1], allowing for a more direct comparison to similarity measures.

Understanding Euclidean Distance

Euclidean Distance, defined as:

d(x,y)=_i=1n(x_iy_i)2d(x, y) = \sqrt{\sum\_{i=1}^{n} (x\_i - y\_i)^2}

calculates the geometric distance between two n-dimensional points xx and yy. While it's effective in capturing dissimilarities, its primary downside is its unbounded nature, potentially complicating interpretations across different datasets or feature scales.

Normalizing Euclidean Distance

To convert Euclidean Distance to a range [0, 1], akin to Cosine Similarity, normalization techniques are needed. Two primary methods for achieving this are Min-Max Normalization and the Exponential Transformation.

Min-Max Normalization

Min-Max Normalization rescales values linearly. For Euclidean Distance, the technique normalizes the distance using the formula:

d_normalized(x,y)=1d(x,y)d_maxd\_{normalized}(x, y) = 1 - \frac{d(x, y)}{d\_{max}}

Where: • d(x,y)d(x, y) is the raw Euclidean Distance. • dmaxd_{max} is the maximum observed distance in the dataset or a theoretical maximum based on the context.

This approach yields a "distance similarity" value where 1 indicates identical points and 0 completely divergent points.

Example:

Assume three data points in 2D: • A=(1,2)A = (1, 2)B=(3,4)B = (3, 4)C=(5,6)C = (5, 6)

Calculate all pairwise Euclidean Distances: • d(A,B)=82.83d(A, B) = \sqrt{8} \approx 2.83d(A,C)=325.66d(A, C) = \sqrt{32} \approx 5.66d(B,C)=82.83d(B, C) = \sqrt{8} \approx 2.83

Let the maximum observed distance dmax=5.66d_{max} = 5.66.

Applying Min-Max Normalization: • dnormalized(A,B)=12.835.66=0.5d_{normalized}(A, B) = 1 - \frac{2.83}{5.66} = 0.5dnormalized(A,C)=15.665.66=0d_{normalized}(A, C) = 1 - \frac{5.66}{5.66} = 0dnormalized(B,C)=12.835.66=0.5d_{normalized}(B, C) = 1 - \frac{2.83}{5.66} = 0.5

Exponential Transformation

Another effective transformation uses an exponential function, emphasizing smaller distances and compressing larger distances:

d_normalized(x,y)=ed(x,y)d\_{normalized}(x, y) = e^{-d(x, y)}

The exponential function inherently constrains the distance to [0, 1].

Example:

For the same points as above: • d(x,y)=82.83d(x, y) = \sqrt{8} \approx 2.83d(x,y)=325.66d(x, y) = \sqrt{32} \approx 5.66

Exponential normalization: • dnormalized(A,B)=e2.830.059d_{normalized}(A, B) = e^{-2.83} \approx 0.059dnormalized(A,C)=e5.660.003d_{normalized}(A, C) = e^{-5.66} \approx 0.003dnormalized(B,C)=e2.830.059d_{normalized}(B, C) = e^{-2.83} \approx 0.059

Comparison with Cosine Similarity

Here's a summary of converting Euclidean distances to a similarity-like measure alongside Cosine Similarity:

MeasureRaw Value (Range)Normalized (Range 0 to 1)
Euclidean Distanced(x,y)d(x, y)$1 - \frac\{d(x, y)\}\{d_\{max\}\}$ or $e^\{-d(x, y)\}$
Cosine Similaritycos(θ)cos(\theta)[-1, 1][0,1][\text{-1, 1}] \rightarrow [0, 1] mapping negative to zero (non-negative datasets)

Cosine Similarity is direction-based, highlighting orientation over magnitude, and typically handles sparse data well. In contrast, normalized Euclidean Distance allows the consideration of absolute magnitudes.

Conclusion

Converting Euclidean Distance into a normalized, bounded similarity measure provides a clearer context for understanding data similarities. By applying methods such as Min-Max Normalization and Exponential Transformation, one can effectively translate raw Euclidean metrics into a range suitable for comparison with directional metrics like Cosine Similarity. This harmonizes inter-measure comparability and enhances interpretability in multi-modal data analyses.


Course illustration
Course illustration

All Rights Reserved.