Unstandardize Regularized Linear Coefficients

Quick Overview

Write a function that maps lasso or ridge coefficients fit on standardized features back to the original feature scale, returning slopes and an intercept that give the same predictions. It tests the algebra of standardization, handling of constant columns, and keeping sparse coefficients exactly zero.

Unstandardize Regularized Linear Coefficients

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `unstandardize_coefficients(coef_std, intercept_std, X)`. A linear model (lasso or ridge) was fit on `Z = (X - mean) / std` with per-column population std (`ddof=0`). Return `(coef, intercept)` on the original scale so that `X @ coef + intercept == Z @ coef_std + intercept_std`. For example: ```python X = [[1.0, 0.0], [3.0, 4.0]] coef_std = [2.0, 4.0] intercept_std = 5.0 unstandardize_coefficients(coef_std, intercept_std, X) # (array([2., 2.]), -3.0) ``` ```hint Expand one term Write out a single term `coef_std_j * (x_j - mean_j) / std_j` and separate the piece that multiplies `x_j` from the piece that is a constant. ``` ### Constraints & Assumptions - `X` is 2-D with one column per coefficient; otherwise raise `ValueError`. - The per-column mean and population std (`ddof=0`) are computed from the `X` passed in, the same way the model was fit. - A constant column (std 0) was all-zero in `Z`: its original-scale coefficient is `0`. - Coefficients that are exactly `0` must stay exactly `0`. - Return `coef` as a 1-D float array and `intercept` as a Python `float`. ### What a Strong Answer Covers - The algebra that turns each standardized term into a slope on the raw feature plus a constant absorbed into the intercept - Using the same per-column mean and `ddof=0` standard deviation that the fit used - Handling constant columns without dividing by zero, and keeping the exact zeros of a lasso fit - Input validation and the requested return types - Time and space cost of the computation ### Follow-up Questions - The pipeline scaled a sparse matrix without centering it (`with_mean=False`). How does the mapping change? - How would you extend the function to a multi-output model where `coef_std` has shape `(k, p)`? - A column that should be constant comes out with a standard deviation around `1e-17`. What goes wrong, and how would you guard against it? - Why does refitting lasso on the unscaled `X` with the same penalty usually give a different model than these unstandardized coefficients?

Overview: Write a function that maps lasso or ridge coefficients fit on standardized features back to the original feature scale, returning slopes and an intercept that give the same predictions. It tests the algebra of standardization, handling of constant columns, and keeping sparse coefficients exactly zero.

PracHub logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement unstandardize_coefficients(coef_std, intercept_std, X). A linear model (lasso or ridge) was fit on Z = (X - mean) / std with per-column population std (ddof=0). Return (coef, intercept) on the original scale so that X @ coef + intercept == Z @ coef_std + intercept_std.

For example:

X = [[1.0, 0.0], [3.0, 4.0]]
coef_std = [2.0, 4.0]
intercept_std = 5.0

unstandardize_coefficients(coef_std, intercept_std, X)
# (array([2., 2.]), -3.0)

Constraints & Assumptions

  • X is 2-D with one column per coefficient; otherwise raise ValueError .
  • The per-column mean and population std ( ddof=0 ) are computed from the X passed in, the same way the model was fit.
  • A constant column (std 0) was all-zero in Z : its original-scale coefficient is 0 .
  • Coefficients that are exactly 0 must stay exactly 0 .
  • Return coef as a 1-D float array and intercept as a Python float .

What a Strong Answer Covers Guidance

  • The algebra that turns each standardized term into a slope on the raw feature plus a constant absorbed into the intercept
  • Using the same per-column mean and ddof=0 standard deviation that the fit used
  • Handling constant columns without dividing by zero, and keeping the exact zeros of a lasso fit
  • Input validation and the requested return types
  • Time and space cost of the computation

Follow-up Questions Guidance

  • The pipeline scaled a sparse matrix without centering it ( with_mean=False ). How does the mapping change?
  • How would you extend the function to a multi-output model where coef_std has shape (k, p) ?
  • A column that should be constant comes out with a standard deviation around 1e-17 . What goes wrong, and how would you guard against it?
  • Why does refitting lasso on the unscaled X with the same penalty usually give a different model than these unstandardized coefficients?