Compute vector L1 and L2 norms and a matrix Frobenius norm with np.linalg.norm

Quick Overview

Write a NumPy function that returns the Euclidean and L1 norms of a vector and the Frobenius norm of a matrix using np.linalg.norm. Tests how the ord argument changes meaning between vectors and matrices, and common numerical pitfalls when computing norms.

Compute vector L1 and L2 norms and a matrix Frobenius norm with np.linalg.norm

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `norms(v, M)` that returns the tuple `(l2, l1, fro)` in that exact order, using `np.linalg.norm` with the appropriate `ord`. Requirements: - `l2` is the Euclidean norm of `v` (default `ord`). - `l1` is the L1 norm of `v` (`ord=1`). - `fro` is the Frobenius norm of `M` (`ord='fro'`). Example: ```python v = np.array([3, 4]) M = np.array([[1, 2], [3, 4]]) norms(v, M) # (5.0, 7.0, 5.477225575051661) ``` ```hint Check the dimensions `np.linalg.norm` reads `ord` differently for a 1-D array and a 2-D array. Make sure each call gets the input whose norm you want. ``` ### What a Strong Answer Covers - Returns the three values in the order `(l2, l1, fro)`, as plain floats that match the example. - Chooses the right `ord` for each norm and explains how `ord` changes meaning between 1-D and 2-D inputs. - Gives the definition of each norm clearly enough to check the example by hand. - Mentions input pitfalls: integer arrays, a matrix that is not 2-D, and overflow for very large entries. ### Follow-up Questions - How do the matrix `ord` options differ from the Frobenius norm? - How would you compute per-row norms of `M` in one call? - How would you scale every row of `M` to unit length, and what happens to a row of zeros?

Overview: Write a NumPy function that returns the Euclidean and L1 norms of a vector and the Frobenius norm of a matrix using np.linalg.norm. Tests how the ord argument changes meaning between vectors and matrices, and common numerical pitfalls when computing norms.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement norms(v, M) that returns the tuple (l2, l1, fro) in that exact order, using np.linalg.norm with the appropriate ord.

Requirements:

  • l2 is the Euclidean norm of v (default ord ).
  • l1 is the L1 norm of v ( ord=1 ).
  • fro is the Frobenius norm of M ( ord='fro' ).

Example:

v = np.array([3, 4])
M = np.array([[1, 2], [3, 4]])
norms(v, M)
# (5.0, 7.0, 5.477225575051661)

What a Strong Answer Covers Guidance

  • Returns the three values in the order (l2, l1, fro) , as plain floats that match the example.
  • Chooses the right ord for each norm and explains how ord changes meaning between 1-D and 2-D inputs.
  • Gives the definition of each norm clearly enough to check the example by hand.
  • Mentions input pitfalls: integer arrays, a matrix that is not 2-D, and overflow for very large entries.

Follow-up Questions Guidance

  • How do the matrix ord options differ from the Frobenius norm?
  • How would you compute per-row norms of M in one call?
  • How would you scale every row of M to unit length, and what happens to a row of zeros?