Scale Each Column of a NumPy Matrix by a Vector Using Broadcasting

Quick Overview

Multiply each column of a 2-D NumPy array by its own factor from a 1-D vector, without loops and without modifying the input. It tests the broadcasting rules, the dtype of the result, and how to reshape the vector to scale rows instead.

Scale Each Column of a NumPy Matrix by a Vector Using Broadcasting

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `scale_columns(X, s)`. Given `X` of shape `(n, m)` and `s` of shape `(m,)`, multiply column `j` of `X` by `s[j]` and return the `(n, m)` result. Use broadcasting (`X * s`) with no explicit loops, and return a new array without mutating `X`. For example: ```python X = np.array([[1, 2, 3], [4, 5, 6]]) s = np.array([10, 100, 1000]) scale_columns(X, s) # array([[ 10, 200, 3000], # [ 40, 500, 6000]]) ``` ```hint Line up the shapes Write the two shapes one above the other, aligned on the right, and compare them axis by axis starting from the last one. ``` ### Constraints & Assumptions - `X` has shape `(n, m)` and `s` has shape `(m,)`. - The shapes are compatible, so input validation is out of scope. ### What a Strong Answer Covers - The broadcasting rule that lines a `(m,)` vector up with the columns of an `(n, m)` matrix. - The result checked against the example, including its dtype. - Why the result is a new array and `X` is left unchanged. - How the shapes would have to change to scale rows instead. ### Follow-up Questions - How would you scale each row by a vector of shape `(n,)` instead, and what goes wrong when `n == m` if you forget to reshape? - How would you do the scaling in place, and what can fail when `X` holds integers and `s` holds floats? - How would you use the same idea to standardize every column to mean 0 and standard deviation 1?

Overview: Multiply each column of a 2-D NumPy array by its own factor from a 1-D vector, without loops and without modifying the input. It tests the broadcasting rules, the dtype of the result, and how to reshape the vector to scale rows instead.

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

Implement scale_columns(X, s). Given X of shape (n, m) and s of shape (m,), multiply column j of X by s[j] and return the (n, m) result. Use broadcasting (X * s) with no explicit loops, and return a new array without mutating X.

For example:

X = np.array([[1, 2, 3],
              [4, 5, 6]])
s = np.array([10, 100, 1000])
scale_columns(X, s)
# array([[  10,  200, 3000],
#        [  40,  500, 6000]])

Constraints & Assumptions

  • X has shape (n, m) and s has shape (m,) .
  • The shapes are compatible, so input validation is out of scope.

What a Strong Answer Covers Guidance

  • The broadcasting rule that lines a (m,) vector up with the columns of an (n, m) matrix.
  • The result checked against the example, including its dtype.
  • Why the result is a new array and X is left unchanged.
  • How the shapes would have to change to scale rows instead.

Follow-up Questions Guidance

  • How would you scale each row by a vector of shape (n,) instead, and what goes wrong when n == m if you forget to reshape?
  • How would you do the scaling in place, and what can fail when X holds integers and s holds floats?
  • How would you use the same idea to standardize every column to mean 0 and standard deviation 1?