Normalize Matrix Rows to Unit L2 Length in NumPy

Quick Overview

Scale every row of a 2-D NumPy array to unit L2 length without Python loops. It tests per-axis norms with np.linalg.norm, how broadcasting lines up a column of norms against a matrix, and what to do when a row has zero length.

Normalize Matrix Rows to Unit L2 Length in NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `normalize_rows(M)` that takes a 2D array and returns a new array of the same shape where each row is scaled to unit L2 norm. Requirements: - Compute norms with `np.linalg.norm(M, axis=1)` and divide via broadcasting (for example, `keepdims=True` for shape `(n, 1)`). - No Python loops. Example: ```python M = np.array([[3.0, 4.0], [0.0, 2.0]]) normalize_rows(M) # array([[0.6, 0.8], # [0. , 1. ]]) ``` ```hint Compare the shapes Write the shape of the norms array next to the shape of `M` and work out which axis broadcasting will line them up on. ``` ### Constraints & Assumptions - `M` is a 2-D array of shape `(n, m)`. - No row is all zeros. ### What a Strong Answer Covers - Computing all row norms in one vectorized call along the right axis. - Why the norms need shape `(n, 1)`, and what goes wrong with shape `(n,)`, including the square-matrix case. - Returning a new float array and leaving `M` unchanged, even for integer input. - What happens if a row has zero norm. ### Follow-up Questions - How would you handle all-zero rows safely? - How would you normalize columns instead of rows? - How would you use the normalized rows to compute the cosine similarity between every pair of rows?

Overview: Scale every row of a 2-D NumPy array to unit L2 length without Python loops. It tests per-axis norms with np.linalg.norm, how broadcasting lines up a column of norms against a matrix, and what to do when a row has zero length.

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

Implement normalize_rows(M) that takes a 2D array and returns a new array of the same shape where each row is scaled to unit L2 norm.

Requirements:

  • Compute norms with np.linalg.norm(M, axis=1) and divide via broadcasting (for example, keepdims=True for shape (n, 1) ).
  • No Python loops.

Example:

M = np.array([[3.0, 4.0],
              [0.0, 2.0]])
normalize_rows(M)
# array([[0.6, 0.8],
#        [0. , 1. ]])

Constraints & Assumptions

  • M is a 2-D array of shape (n, m) .
  • No row is all zeros.

What a Strong Answer Covers Guidance

  • Computing all row norms in one vectorized call along the right axis.
  • Why the norms need shape (n, 1) , and what goes wrong with shape (n,) , including the square-matrix case.
  • Returning a new float array and leaving M unchanged, even for integer input.
  • What happens if a row has zero norm.

Follow-up Questions Guidance

  • How would you handle all-zero rows safely?
  • How would you normalize columns instead of rows?
  • How would you use the normalized rows to compute the cosine similarity between every pair of rows?