I'm new to data science with a moderate math background. I'm playing around with numpy and can across the following:

enter image description here

So after reading np.linalg.norm, to my understanding it computes the 2-norm of the matrix. Wanting to see if I understood properly, I decided to compute it by hand using the 2 norm formula I found here:

enter image description here

Following computing the dot product, the characteristic equation, applying the formula for quadratic equation and taking square root of the max, I end up with a different result, namely:

enter image description here

So here is my question. What went wrong? Did I use the right formula? Also I couldn't find a conclusive way to get the 5. The doc he doc that says:

If this is set to True, the axes which are normed over are left in the result as dimensions with size one. With this option the result will broadcast correctly against the original x.

But I can't wrap my head around it. What does it represent and how you compute it?

I hope the formatting and the question are clear enough.


The formula you cited is not the formula scipy is using. According to the documentation it calculates the "Frobenius norm" which is defined to be the root of the squared sum of the elements of each row (row since you specified dim=1)

$ O_i = { \biggl(\sum_{n=1}^N \mathbf{A}_{i, n}^2\biggr) }^{1/2} $

Thus, $(3^2+4^2)^{1/2}=5$ and $(2^2+6^2+4^2)^{1/2}=56^{1/2}$

EDIT: Also, the keepdims argument just says whether to collapse the dimension that you calculated or not, a simple example is to look at the output shape of the matrix that you get with and without the argument.

a = np.array([[0, 3, 4], [2, 6, 4]])

np.linalg.norm(a, axis=1, keepdims=True).shape # output is (2, 1)
np.linalg.norm(a, axis=1, keepdims=False).shape # output is (2,) because the second dimension was collapsed
  • $\begingroup$ That was as simple as that. Thanks you for the clarification :D $\endgroup$
    – Nanoboss
    May 2 '20 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.