Column-Wise Weighted Average with NumPy

Quick Overview

Compute the weighted average of every column of a NumPy matrix, with one weight per row, using np.average. It tests choosing the right axis, how NumPy normalizes by the weight total, and how to handle weights that sum to zero or do not match the row count.

Column-Wise Weighted Average with NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a 2-D array `matrix` of shape `(m, n)` and a 1-D array `weights` of length `m`, return the weighted average of each column (each row weighted by its entry in `weights`) as an array of shape `(n,)`. For example: ```text matrix = [[1, 2], [3, 4]] weights = [1, 3] Output: [2.5, 3.5] ``` The first column is `(1*1 + 3*3) / 4 = 2.5` and the second is `(1*2 + 3*4) / 4 = 3.5`. ```hint Line up the weights Work out which dimension of `matrix` the weights belong to, and check whether the function divides by the weight total for you or you have to do it. ``` ### Constraints & Assumptions - `matrix` has shape `(m, n)` and `weights` has length `m`, one weight per row. - Use `np.average` with `axis=0` and the `weights` parameter. Do not write the reduction by hand. ### What a Strong Answer Covers - Why `axis=0` lines the length-`m` weights up with the rows and leaves one value per column. - What `np.average` computes for each column and why the weights do not have to sum to 1. - A check of the result against the example, including the output shape and dtype. - Bad inputs: a weights array whose length does not match the row count, and weights that sum to zero. - Time and memory cost. ### Follow-up Questions - What happens if the weights sum to zero? - How do you also recover the sum of the weights? - Some entries of `matrix` are NaN. How would you skip them so that each column averages only its valid entries, with the skipped rows' weights left out of that column's total?

Overview: Compute the weighted average of every column of a NumPy matrix, with one weight per row, using np.average. It tests choosing the right axis, how NumPy normalizes by the weight total, and how to handle weights that sum to zero or do not match the row count.

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

Given a 2-D array matrix of shape (m, n) and a 1-D array weights of length m, return the weighted average of each column (each row weighted by its entry in weights) as an array of shape (n,).

For example:

matrix  = [[1, 2],
           [3, 4]]
weights = [1, 3]
Output: [2.5, 3.5]

The first column is (1*1 + 3*3) / 4 = 2.5 and the second is (1*2 + 3*4) / 4 = 3.5.

Constraints & Assumptions

  • matrix has shape (m, n) and weights has length m , one weight per row.
  • Use np.average with axis=0 and the weights parameter. Do not write the reduction by hand.

What a Strong Answer Covers Guidance

  • Why axis=0 lines the length- m weights up with the rows and leaves one value per column.
  • What np.average computes for each column and why the weights do not have to sum to 1.
  • A check of the result against the example, including the output shape and dtype.
  • Bad inputs: a weights array whose length does not match the row count, and weights that sum to zero.
  • Time and memory cost.

Follow-up Questions Guidance

  • What happens if the weights sum to zero?
  • How do you also recover the sum of the weights?
  • Some entries of matrix are NaN. How would you skip them so that each column averages only its valid entries, with the skipped rows' weights left out of that column's total?