Z-Score Standardization of a 1-D NumPy Array

Quick Overview

Write a NumPy function that standardizes a 1-D array to zero mean and unit standard deviation using the population standard deviation. It tests vectorized broadcasting, the difference between ddof=0 and ddof=1, and handling of constant or missing values.

Z-Score Standardization of a 1-D NumPy Array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write `zscore(a: np.ndarray) -> np.ndarray` that standardizes a 1-D array to zero mean and unit standard deviation. Return `(a - mean) / std`. - Compute mean and standard deviation over the whole array. - Use the population standard deviation: `np.std(a, ddof=0)`. - Return a new array of the same shape and floating-point dtype. For example: ```python a = np.array([1, 2, 3, 4, 5]) zscore(a) # array([-1.41421356, -0.70710678, 0. , 0.70710678, 1.41421356]) ``` ```hint Degenerate input Consider what the formula produces when every element of `a` has the same value. ``` ### What a Strong Answer Covers - The standardization formula and why the population standard deviation (`ddof=0`) gives different numbers from the sample version (`ddof=1`). - A vectorized implementation that broadcasts the mean and standard deviation and returns a float array of the same shape. - What happens with a constant array, an empty array and NaN values. - A quick check that the result has mean close to 0 and standard deviation close to 1. ### Follow-up Questions - How would you handle a constant array where `std == 0`? - How would you extend this to standardize along one axis of a 2-D array? - In a train and test split, which data should the mean and standard deviation come from, and why? - How would you standardize an array that contains missing values stored as NaN?

Overview: Write a NumPy function that standardizes a 1-D array to zero mean and unit standard deviation using the population standard deviation. It tests vectorized broadcasting, the difference between ddof=0 and ddof=1, and handling of constant or missing values.

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

Write zscore(a: np.ndarray) -> np.ndarray that standardizes a 1-D array to zero mean and unit standard deviation. Return (a - mean) / std.

  • Compute mean and standard deviation over the whole array.
  • Use the population standard deviation: np.std(a, ddof=0) .
  • Return a new array of the same shape and floating-point dtype.

For example:

a = np.array([1, 2, 3, 4, 5])
zscore(a)
# array([-1.41421356, -0.70710678, 0. , 0.70710678, 1.41421356])

What a Strong Answer Covers Guidance

  • The standardization formula and why the population standard deviation ( ddof=0 ) gives different numbers from the sample version ( ddof=1 ).
  • A vectorized implementation that broadcasts the mean and standard deviation and returns a float array of the same shape.
  • What happens with a constant array, an empty array and NaN values.
  • A quick check that the result has mean close to 0 and standard deviation close to 1.

Follow-up Questions Guidance

  • How would you handle a constant array where std == 0 ?
  • How would you extend this to standardize along one axis of a 2-D array?
  • In a train and test split, which data should the mean and standard deviation come from, and why?
  • How would you standardize an array that contains missing values stored as NaN?