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?
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With