← Problems

7. Euclidean Distance

EasyVectorsLinear AlgebraDistance Metrics

Given two vectors a\mathbf{a} and b\mathbf{b} in Rn\mathbb{R}^n, compute the Euclidean distance between them.

d(a,b)=i=1n(aibi)2=ab2d(\mathbf{a}, \mathbf{b}) = \sqrt{\sum_{i=1}^{n} (a_i - b_i)^2} = \|\mathbf{a} - \mathbf{b}\|_2

This is the L2L_2 norm of the difference vector and is the most common distance metric in ML — used in kk-NN, kk-means, and similarity search.

Example

a = [0, 0], b = [3, 4] Output: 5.0 # classic 3-4-5 right triangle

Constraints

  • Both inputs will always have the same length.
  • Return a Python float.
Python 3
⌘ + Enter
Run your code to see results
Ctrl + Enter