Feature Scaling and Gradient Descent Convergence

Quick Overview

Train a no-intercept least-squares model with batch gradient descent on raw and standardized features, then report the iterations to convergence and the condition number of the Gram matrix. Tests careful NumPy implementation and how conditioning controls the speed of gradient descent.

Feature Scaling and Gradient Descent Convergence

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement four functions that train a no-intercept least-squares model with batch gradient descent on raw and on standardized features. For each version, report the number of iterations to convergence and the condition number of the Gram matrix $G = X^\top X / n$. - `standardize(X)` subtracts the column means and divides by the population standard deviation (`ddof=0`). A column with zero standard deviation uses 1 instead, so it becomes all zeros. - `gram_condition_number(X)` returns $\lambda_{\max} / \lambda_{\min}$ of $G$ as a `float`. - `gradient_descent(X, y, lr, tol, max_iter)` starts at `w = 0` and uses the gradient $g = X^\top (Xw - y) / n$. At step `it`, if $\lVert g \rVert_2 \le$ `tol`, it returns `(w, it, True)` before updating. - If `max_iter` steps finish without that early return, `gradient_descent` returns `(w, max_iter, converged)`, where `converged` means the final gradient norm is at most `tol`. - `compare_scaling(X, y, tol, max_iter)` returns `{"raw": {...}, "standardized": {...}}`. Each entry holds `condition_number`, `learning_rate` (`1 / λmax` of that version's $G$), `n_iter`, `converged` and `w`. Example: ```python X = [[1, 0], [0, 2]] y = [1, 2] gram_condition_number(X) # 4.0 gradient_descent(X, y, 0.5, 1e-12, 2) # (array([0.4375, 1.0]), 2, False) # step 1: g=[-0.5, -2] -> w=[0.25, 1] # step 2: g=[-0.375, 0] -> w=[0.4375, 1] ``` ```hint Symmetric matrices $G$ is symmetric and positive semidefinite. Pick the NumPy eigenvalue routine built for that case and check the order it returns eigenvalues in. ``` ```hint Why scaling changes the count Write the error $w - w^*$ in the eigenbasis of $G$ and see how much each component shrinks per step when the step size is $1/\lambda_{\max}$. ``` ### Constraints & Assumptions - Use NumPy only. - No intercept term and no regularization. - `X` has shape `(n, d)` and `y` has length `n`. ### What a Strong Answer Covers - Standardization with the population standard deviation and safe handling of constant columns. - A condition number computed from the eigenvalues of the symmetric Gram matrix and returned as a Python `float`. - A descent loop that checks the tolerance before each update, reports the right iteration count, and sets `converged` correctly when `max_iter` runs out. - A `compare_scaling` that sets `lr = 1/λmax` separately for each version and fills all five fields. - An explanation of how the condition number controls the iteration count through the per-eigendirection contraction. ### Follow-up Questions - How many iterations would you expect as a function of the condition number and the tolerance? - With no intercept, what does centering the features do to the model, and how would you add an intercept back? - How would momentum or a preconditioner change the dependence on the condition number? - What goes wrong in `gram_condition_number` when a column is constant or `n` is smaller than `d`?

Overview: Train a no-intercept least-squares model with batch gradient descent on raw and standardized features, then report the iterations to convergence and the condition number of the Gram matrix. Tests careful NumPy implementation and how conditioning controls the speed of gradient descent.

General logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement four functions that train a no-intercept least-squares model with batch gradient descent on raw and on standardized features. For each version, report the number of iterations to convergence and the condition number of the Gram matrix G=X⊤X/nG = X^\top X / n.

  • standardize(X) subtracts the column means and divides by the population standard deviation ( ddof=0 ). A column with zero standard deviation uses 1 instead, so it becomes all zeros.
  • gram_condition_number(X) returns λmax⁡/λmin⁡\lambda_{\max} / \lambda_{\min} of GG as a float .
  • gradient_descent(X, y, lr, tol, max_iter) starts at w = 0 and uses the gradient g=X⊤(Xw−y)/ng = X^\top (Xw - y) / n . At step it , if ∥g∥2≤\lVert g \rVert_2 \le tol , it returns (w, it, True) before updating.
  • If max_iter steps finish without that early return, gradient_descent returns (w, max_iter, converged) , where converged means the final gradient norm is at most tol .
  • compare_scaling(X, y, tol, max_iter) returns {"raw": {...}, "standardized": {...}} . Each entry holds condition_number , learning_rate ( 1 / λmax of that version's GG ), n_iter , converged and w .

Example:

X = [[1, 0], [0, 2]]
y = [1, 2]
gram_condition_number(X)               # 4.0
gradient_descent(X, y, 0.5, 1e-12, 2)  # (array([0.4375, 1.0]), 2, False)
# step 1: g=[-0.5, -2]   -> w=[0.25, 1]
# step 2: g=[-0.375, 0]  -> w=[0.4375, 1]

Constraints & Assumptions

  • Use NumPy only.
  • No intercept term and no regularization.
  • X has shape (n, d) and y has length n .

What a Strong Answer Covers Guidance

  • Standardization with the population standard deviation and safe handling of constant columns.
  • A condition number computed from the eigenvalues of the symmetric Gram matrix and returned as a Python float .
  • A descent loop that checks the tolerance before each update, reports the right iteration count, and sets converged correctly when max_iter runs out.
  • A compare_scaling that sets lr = 1/λmax separately for each version and fills all five fields.
  • An explanation of how the condition number controls the iteration count through the per-eigendirection contraction.

Follow-up Questions Guidance

  • How many iterations would you expect as a function of the condition number and the tolerance?
  • With no intercept, what does centering the features do to the model, and how would you add an intercept back?
  • How would momentum or a preconditioner change the dependence on the condition number?
  • What goes wrong in gram_condition_number when a column is constant or n is smaller than d ?