# Trying to understand the result provided by np.linalg.norm function in numpy (normalisation)

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

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:

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:

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

• That was as simple as that. Thanks you for the clarification :D Commented May 2, 2020 at 9:00