By Jamal Hussein · Last updated:
Distance Between Vectors Calculator d = √Σ(ai − bi)²
Enter two vectors with the same number of components, separated by commas or spaces. You get the Euclidean (L2) distance plus the Manhattan, Chebyshev, Minkowski, squared Euclidean and cosine distances, a plot for 2D inputs, and the working.
The distance between two vectors is a single non-negative number that says how far apart they are; the Euclidean distance is the square root of the sum of the squared differences between matching components. This calculator takes two vectors of equal dimension and returns the Euclidean (L2), Manhattan (L1), Chebyshev (L∞), Minkowski, squared Euclidean and cosine distances.
What is the distance between two vectors?
Treat each vector as a point in n-dimensional space. A distance function assigns a number to a pair of points: zero when they coincide, larger as they move apart. The familiar straight-line version is the Euclidean distance, which is the length of the difference vector \(a – b\).
It is not the only sensible definition. Add the absolute differences and you get the Manhattan distance; keep only the largest one and you get the Chebyshev distance; ignore length altogether and compare directions and you get the cosine distance. Each answers a slightly different question, which is why the same pair of vectors can be “close” under one measure and “far” under another.
How do you calculate the Euclidean distance between two vectors?
Subtract the vectors component by component, square each difference, add the squares and take the square root:
Here \(a\) and \(b\) are the two vectors, \(n\) is the number of components in each, and \(a_i\), \(b_i\) are their \(i\)-th components. Drop the square root and you have the squared Euclidean distance.
Euclidean, Manhattan and Chebyshev are all members of one family, the Minkowski distance of order \(p\):
Setting \(p = 1\) gives Manhattan, \(p = 2\) gives Euclidean, and letting \(p\) grow without limit gives Chebyshev. The order must be at least 1 for the result to obey the triangle inequality. Cosine distance sits outside this family because it is built from the angle between the vectors, not from their difference:
where \(a \cdot b\) is the dot product of the two vectors and \(\lVert a \rVert\), \(\lVert b \rVert\) are their lengths. It runs from 0 (same direction) through 1 (perpendicular) to 2 (opposite), and it is undefined if either vector is all zeros.
Worked example: the distance between (1, 2) and (4, 6)
This is the default input above. The component differences are \(1 – 4 = -3\) and \(2 – 6 = -4\).
The squared Euclidean distance is the 25 under the root. The Manhattan distance is \(3 + 4 = 7\), the Chebyshev distance is \(\max(3, 4) = 4\), and the Minkowski distance of order 3 is \((27 + 64)^{1/3} = 91^{1/3} = 4.4979\).
For the cosine distance, \(a \cdot b = 1\times4 + 2\times6 = 16\), \(\lVert a \rVert = \sqrt{5}\) and \(\lVert b \rVert = \sqrt{52}\), so the similarity is \(16/\sqrt{260} = 0.9923\) and the distance is \(1 – 0.9923 = 0.0077\). The two points are 5 units apart, yet the vectors point in almost the same direction, about 7.1 degrees apart.
Which distance metric should you use?
The table lists every result for the worked example with the situation each one suits.
| Distance | How it combines the differences 3 and 4 | Value | The right choice when |
|---|---|---|---|
| Manhattan (L1) | 3 + 4 | 7 | Data is sparse or high-dimensional, or one large outlying feature should not dominate. |
| Euclidean (L2) | √(3² + 4²) | 5 | Features are dense, on comparable scales, and straight-line geometry is meaningful. |
| Minkowski, p = 3 | (3³ + 4³)1/3 | 4.4979 | You want to tune, as a hyperparameter, how strongly the largest difference counts. |
| Chebyshev (L∞) | max(3, 4) | 4 | Only the worst single coordinate matters, as in tolerance checks or king moves on a grid. |
| Squared Euclidean | 3² + 4² | 25 | You only need to rank neighbours or minimise a sum; it skips the root and keeps the order. |
| Cosine distance | 1 − 16 / √260 | 0.0077 | Direction carries the meaning and magnitude should not, as with text embeddings. |
For any pair of vectors the Minkowski values shrink as the order rises, so Chebyshev ≤ Euclidean ≤ Manhattan always holds: here 4 ≤ 5 ≤ 7. The plot shows why for two dimensions. The orange path has to travel along the axes, while the indigo segment cuts the corner.
Where are vector distances used in machine learning?
- k-nearest neighbours. The prediction for a point comes from the training points with the smallest distance to it, Euclidean by default in most libraries. Features must be scaled first. Take three people described by (age, income): (25, 40000), (55, 42000) and (26, 45000). Unscaled, the first is 2000.2 from the second and 5000.0 from the third, so a 30-year age gap counts for nothing next to a small difference in income.
- k-means clustering. Each point joins the centroid with the smallest squared Euclidean distance, and the centroid is the mean because the mean is the point that minimises that sum of squares. Swapping in another distance changes the algorithm, which is why L1 clustering uses medians instead. The same scaling rule applies.
- Sparse and high-dimensional data. Squaring lets a few large differences swamp the rest, and in many dimensions Euclidean distances between random points bunch together so that the nearest and farthest neighbours look alike. L1 keeps more contrast in that setting, a result reported by Aggarwal, Hinneburg and Keim in 2001.
- Text embeddings and retrieval. A long document and a short one on the same topic produce vectors of different length pointing the same way. Cosine distance ignores the length and scores them as near neighbours, which is why vector search is usually framed in terms of cosine similarity.
The two views meet when vectors are normalised. For unit-length vectors, \(\lVert a – b \rVert^2 = \lVert a \rVert^2 + \lVert b \rVert^2 – 2\,a \cdot b = 2 – 2\cos\theta\), so the squared Euclidean distance is exactly 2 times the cosine distance. Try the “Unit vectors at 90°” preset: (1, 0) and (0, 1) give a squared Euclidean distance of 2 and a cosine distance of 1. Ranking normalised embeddings by Euclidean distance or by cosine distance therefore returns the same order.
Common mistakes
- Skipping feature scaling. Any Minkowski distance is dominated by the feature with the largest numeric range. Standardise or min-max scale before kNN, k-means or any distance-based model.
- Mixing up cosine similarity and cosine distance. Similarity of 1 means identical direction; distance of 0 means the same thing. Sorting by the wrong one reverses your ranking.
- Comparing squared and unsquared values. A threshold of 5 on the Euclidean distance is a threshold of 25 on the squared version. Check which one a library returns.
- Treating cosine distance as a true metric. It can break the triangle inequality, so index structures that rely on that rule need normalised vectors and Euclidean distance instead.
- Misaligned components. Both vectors must list the same features in the same order. A mismatch in length is caught as an error; a mismatch in order silently gives a wrong number.
Distance between two vectors in Python with NumPy and SciPy
This reproduces every value in the worked example:
import numpy as np
from scipy.spatial import distance
a = np.array([1.0, 2.0])
b = np.array([4.0, 6.0])
print(np.linalg.norm(a - b)) # 5.0 Euclidean (L2)
print(distance.euclidean(a, b)) # 5.0
print(distance.cityblock(a, b)) # 7.0 Manhattan (L1)
print(distance.chebyshev(a, b)) # 4.0 L-infinity
print(distance.sqeuclidean(a, b)) # 25.0
print(round(distance.minkowski(a, b, p=3), 6)) # 4.497941
print(round(distance.cosine(a, b), 6)) # 0.007722
np.linalg.norm also takes an ord argument: ord=1 returns the Manhattan distance and ord=np.inf the Chebyshev distance of the difference vector. See the numpy.linalg.norm documentation and the list of metrics in the scipy.spatial.distance documentation.
Frequently asked questions
Is Euclidean distance the same as the L2 norm?
Euclidean distance is the L2 norm of the difference between the two vectors. The L2 norm measures the length of one vector, and the distance between a and b is that length applied to a minus b.
Can the distance between two vectors be negative?
No. Euclidean, Manhattan, Chebyshev and Minkowski distances are zero only when the two vectors are identical and positive otherwise. Cosine distance ranges from 0 to 2 and is 0 for any two vectors that point in the same direction, even if their lengths differ.
Do the two vectors need the same dimension?
Yes. Every formula pairs the i-th component of one vector with the i-th component of the other, so both vectors must have the same number of components. The calculator reports an error if the counts differ.
What is the difference between cosine distance and Euclidean distance?
Euclidean distance measures how far apart the two points are, so it changes when either vector is scaled. Cosine distance measures only the angle between them and ignores length. For vectors normalised to unit length the two agree on ranking, because the squared Euclidean distance equals 2 times the cosine distance.
Related calculators
Distance is one of several ways to compare vectors. The linear algebra calculators hub lists the rest. Two that pair naturally with this page: