Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.linalg.norm ord=2 not giving Euclidean norm

Tags:

python

numpy

I'm attempting to compute the Euclidean distance between two matricies which I would expect to be given by the square root of the element-wise sum of squared differences.

This seems to me to be exactly the calculation computed by numpy's linalg.norm function, however it doesn't appear to match my expected result.

For example this code returns different values (5.385 vs 5.339)

import numpy as np

a = np.arange(6).reshape(2, 3)
b = np.array([[1,2,3], [-1,1,4]])

print(np.sqrt(np.sum(np.square(a-b))))
print(np.linalg.norm(a-b, 2))

Have I misinterpreted the linalg.norm function? Why are the two above calculation methodologies not returning the same value?

like image 767
handlist Avatar asked Mar 25 '21 19:03

handlist


1 Answers

From what I can see in the DocString of np.linalg.norm, it looks like for arrays with dim>2, it takes the largest singular value for ord=2, meaning np.linalg.norm(a, ord=2) is the same as np.linalg.svd(a)[1].max(). So in your case that's gonna be:

print(np.sqrt(np.sum(np.square(a-b))))
print(np.linalg.norm(a-b, 2))
print(np.linalg.svd(a-b)[1].max())

And this will return 5.385, 5.339, 5.339.

The mathematical formulation is given on Wikipedia, where the distinction is made between 2-norm (which is ord=2) and Frobenius norm (ord=None).

like image 56
sams-studio Avatar answered Sep 20 '22 01:09

sams-studio