KatInterview

Fit a Line by Batch Gradient Descent with Standardized Input

Math & Bit ManipulationAuto-graded · 19 test cases
MediumAuto-gradedMath & Bit Manipulationgradient-descentlinear-regressionnumerical

Last updated

Implement full-batch gradient descent for a one-feature least-squares line, standardizing the input, and map the learned parameters back to the original scale.

You are given two equal-length lists of numbers: inputs xs and targets ys. Fit the line y_hat = slope * x + intercept using batch gradient descent, written by hand. Do not call any library routine that fits the line for you.

Return [slope, intercept] in the original units of xs and ys.

Procedure

Follow this procedure so that your results match the expected values.

  1. Loss. Use mean squared error, L = (1/n) * sum((pred_i - y_i)^2).
  2. Edge cases.
    • If xs is empty, return [0.0, 0.0].
    • Let mean_x be the mean of xs and sd_x the population standard deviation (divide by n). If sd_x == 0, every input is identical and the slope cannot be determined. Return [0.0, mean(ys)].
  3. Standardize the input. Compute z_i = (x_i - mean_x) / sd_x. Run gradient descent on z, not on x, so that one fixed step size is stable whatever the magnitude of the inputs.
  4. Gradient descent on the standardized data.
    • Start with w = 0.0 and b = 0.0.
    • Perform exactly 1000 iterations with learning rate 0.1.
    • In each iteration, compute error_i = w * z_i + b - y_i for every example using the current w and b. Then update both parameters at once:
      • grad_w = (2/n) * sum(error_i * z_i)
      • grad_b = (2/n) * sum(error_i)
      • w -= 0.1 * grad_w
      • b -= 0.1 * grad_b
  5. Convert back to the original scale.
    • slope = w / sd_x
    • intercept = b - slope * mean_x
  6. Return [slope, intercept].

With these settings the procedure converges to the ordinary least-squares line, so the result should agree with the closed-form fit up to floating-point error.

Constraints

  • 0 <= len(xs) == len(ys) <= 1000
  • Values in xs and ys are finite real numbers of moderate magnitude (about |v| <= 1000).
  • Return a list of two floats: [slope, intercept].
  • Use only the Python standard library.

Examples

Example 1

Input: fit_line_gd([1, 2, 3, 4], [3, 5, 7, 9])

Output: [1.9999999999999991, 1]

Example 2

Input: fit_line_gd([0, 1, 2, 3, 4], [5, 4, 3, 2, 1])

Output: [-0.9999999999999999, 4.999999999999999]

Example 3

Input: fit_line_gd([1, 2, 3, 4, 5], [2, 3, 5, 4, 6])

Output: [0.8999999999999997, 1.2999999999999998]

Hints

Hint 1

Differentiate the mean squared error with respect to the slope and the intercept. Each gradient is a sum over all examples of the prediction error, multiplied by the input for the slope and by 1 for the intercept.

Hint 2

A fixed learning rate is only safe when the input has a sensible scale. Standardize the input (subtract the mean, divide by the standard deviation) before descending, so the loss surface is well conditioned.

Hint 3

To undo the standardization, substitute z = (x - mean_x) / sd_x into w*z + b. The slope becomes w / sd_x and the intercept becomes b - slope * mean_x. Do not forget the empty-input and constant-input cases.