Fit OLS on standardized predictors and return the slopes per one standard deviation

Quick Overview

Standardize each predictor with the population standard deviation, fit OLS with an intercept on a raw target, and return the slopes per one-SD change. It tests how rescaling predictors changes the coefficients but not the predictions, and how to handle a constant column.

Fit OLS on standardized predictors and return the slopes per one standard deviation

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `def standardized_x_coefficients(X: np.ndarray, y: np.ndarray) -> np.ndarray`. Given a 2-D float `X` of shape `(n, 2)` and a 1-D float `y` of length `n`, standardize each column of `X`, fit an intercept-bearing OLS of raw `y` on the standardized `X`, and return the two slopes (change in `y` per one-SD increase of each predictor). Requirements: - Standardize as `z = (x - mean) / std` with population std (`ddof=0`), matching `sklearn.preprocessing.StandardScaler`. - Do not standardize or center `y`. - Fit OLS with an intercept. - Return a 1-D float ndarray of shape `(2,)`: slopes in `X` column order, intercept excluded. For example: ```python X = np.array([[1.0, 10.0], [2.0, 20.0], [3.0, 40.0], [4.0, 30.0]]) y = np.array([2.0, 4.0, 8.0, 6.0]) out = standardized_x_coefficients(X, y) # shape (2,), floats ``` The expected `out` is approximately `[0.0, 2.2360680]`, which is $[0, \sqrt{5}]$ up to floating point error in the first entry. ```hint Which standard deviation Libraries disagree on the default denominator for a standard deviation. Check which one your call uses before dividing. ``` ```hint Relating the two scales Write the raw model $a + b_1 x_1 + b_2 x_2$ in terms of the z-scores and compare coefficients. That gives you a quick way to check your output. ``` ### Constraints & Assumptions - `X` is a 2-D float array of shape `(n, 2)` and `y` is a 1-D float array of length `n`. - The base task assumes each column of `X` has nonzero variance. The follow-ups ask what to do when one does not. ### What a Strong Answer Covers - Standardizing each column with its mean and population standard deviation (`ddof=0`) while leaving `y` on its raw scale. - Fitting OLS with an intercept and returning only the two slopes, in column order, as a shape `(2,)` array. - Explaining why each standardized slope equals the raw slope times that column's standard deviation, and why predictions do not change. - Naming the failure on a constant column and a way to guard against it. ### Follow-up Questions - Why does standardizing `X` but not `y` leave predictions unchanged? - When would you standardize `y` as well? - How would you avoid divide-by-zero on a constant column?

Overview: Standardize each predictor with the population standard deviation, fit OLS with an intercept on a raw target, and return the slopes per one-SD change. It tests how rescaling predictors changes the coefficients but not the predictions, and how to handle a constant column.

General logo
General
Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement def standardized_x_coefficients(X: np.ndarray, y: np.ndarray) -> np.ndarray. Given a 2-D float X of shape (n, 2) and a 1-D float y of length n, standardize each column of X, fit an intercept-bearing OLS of raw y on the standardized X, and return the two slopes (change in y per one-SD increase of each predictor).

Requirements:

  • Standardize as z = (x - mean) / std with population std ( ddof=0 ), matching sklearn.preprocessing.StandardScaler .
  • Do not standardize or center y .
  • Fit OLS with an intercept.
  • Return a 1-D float ndarray of shape (2,) : slopes in X column order, intercept excluded.

For example:

X = np.array([[1.0, 10.0],
              [2.0, 20.0],
              [3.0, 40.0],
              [4.0, 30.0]])
y = np.array([2.0, 4.0, 8.0, 6.0])
out = standardized_x_coefficients(X, y)  # shape (2,), floats

The expected out is approximately [0.0, 2.2360680], which is [0,5][0, \sqrt{5}] up to floating point error in the first entry.

Constraints & Assumptions

  • X is a 2-D float array of shape (n, 2) and y is a 1-D float array of length n .
  • The base task assumes each column of X has nonzero variance. The follow-ups ask what to do when one does not.

What a Strong Answer Covers Guidance

  • Standardizing each column with its mean and population standard deviation ( ddof=0 ) while leaving y on its raw scale.
  • Fitting OLS with an intercept and returning only the two slopes, in column order, as a shape (2,) array.
  • Explaining why each standardized slope equals the raw slope times that column's standard deviation, and why predictions do not change.
  • Naming the failure on a constant column and a way to guard against it.

Follow-up Questions Guidance

  • Why does standardizing X but not y leave predictions unchanged?
  • When would you standardize y as well?
  • How would you avoid divide-by-zero on a constant column?