Week 2 - Exercise 6 - what is 2-norm? - Python Basics With Numpy Programming Assignment

In Python Basics With Numpy Programming Assignment

it says:
" numpy.linalg.norm has another parameter ord where we specify the type of normalization to be done (in the exercise below you’ll do 2-norm). To get familiar with the types of normalization you can visit numpy.linalg.norm

Exercise 6 - normalize_rows

Implement normalizeRows() to normalize the rows of a matrix. After applying this function to an input matrix x, each row of x should be a vector of unit length (meaning length 1)."

The numpy doc link defines it as simply “2-norm (largest sing. value)”

I am not sure what “largest sing. value” means. My assumption would be that for example [[0., 3., 4.], [1., 6., 4.]], the result would be that each value in a row would be divided by the largest value in the row. In this case, [[0/4, 3/4, 4/4], [1/6, 6/6, 4/6]]. However, this is not the correct answer.

Question 1) Does it matter to understand the specifics to how 2-norm is calculated in order to continue?

Question 2) If it does, what does “largest sing. value” mean and how does one calculate normalization with it?

The 2-norm is simply the normal Euclidean length of a vector (each row of the matrix in this case). That means the square root of the sum of the squares of the individual values.

You don’t need to worry about the “singular” value term that they mention in the documentation. The Euclidean 2-norm is only different than the Frobenius norm when you are dealing with complex numbers, which we never do in these courses.

Just invoke np.linalg.norm with the appropriate arguments and be happy. :nerd_face:

2 Likes

The largest singular value corresponds to the largest value you get when performing singular value decomposition of a matrix.
Let’s use the matrix mentioned in the markdown to perform SVD.

>>> import numpy as np
>>> A = np.asarray([[0, 3, 4], [2,6,4]])
>>> S = np.linalg.svd(A, compute_uv=False)
>>> S
array([8.82419565, 1.7701896 ])

Performing L2-norm of the entire matrix does the same:

>>> np.linalg.norm(A, ord=2)
8.82419564510368

Based on the above example, you see the max(S) the same as the L2-norm of the matrix.

When you see the text as below in this numpy table, it means that use the 1st formula you find as you traverse rows below. In this case, see the formula in other case and substitute ord as 2.

Please spend some time to read rest of the markdown to understand the importance of the axis parameter.

1 Like

Understood. Thank you :smiley:

Thank you!
I thought “as below” meant it would be defined in the examples that followed further down on the page. And I couldn’t find a clear explanation there either. That makes a lot more sense.

Ok, understood. I am lacking in linear algebra knowledge to fully understand the implications, but your explanation helps me understand how the calculation is done and where it is coming from. I appreciate your time explaining that. Thank you again! :smiley: