Fit a Quadratic as a Linear Model

Quick Overview

Fit a quadratic curve to one-dimensional data with ordinary least squares by building a design matrix of an intercept, x and x squared, then return the three coefficients in a fixed order. Tests why polynomial regression is still a linear model and how to solve it in a numerically sound way.

Fit a Quadratic as a Linear Model

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fit_quadratic(x: np.ndarray, y: np.ndarray) -> np.ndarray` for 1-D float arrays of equal length `n`. Fit `y = b0 + b1*x + b2*x**2` as a linear model via OLS and return its parameters. Requirements: - Build a design matrix with columns `[x, x**2]` and fit OLS with an intercept. - Return a 1-D float ndarray of shape `(3,)` ordered `[b0_intercept, b1_linear, b2_quadratic]`. Example: ```python x = np.array([0., 1., 2., 3.]) y = 1 + 2*x + 3*x**2 # exact quadratic fit_quadratic(x, y) # array([1., 2., 3.]) # [b0, b1, b2] ``` ```hint Treat the square as a feature Once `x**2` is computed, it is just another input column. Decide how the intercept enters the design matrix and which solver you trust with it. ``` ### Constraints & Assumptions - `x` and `y` are 1-D float arrays of the same length `n`. - The output order is fixed: intercept, linear coefficient, quadratic coefficient. ### What a Strong Answer Covers - Builds a design matrix with an intercept, `x` and `x**2`, or an equivalent setup that fits the intercept separately. - Solves the least-squares problem with a numerically sound method. - Returns a shape-(3,) float array in the order `[b0, b1, b2]`. - Explains why a quadratic in `x` is still a linear regression. - Notes when the coefficients are not uniquely determined. ### Follow-up Questions - Why does this stay a linear regression problem? - What breaks numerically for high-degree fits, and how do you help? - On real, noisy data, how would you choose the polynomial degree?

Overview: Fit a quadratic curve to one-dimensional data with ordinary least squares by building a design matrix of an intercept, x and x squared, then return the three coefficients in a fixed order. Tests why polynomial regression is still a linear model and how to solve it in a numerically sound way.

Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fit_quadratic(x: np.ndarray, y: np.ndarray) -> np.ndarray for 1-D float arrays of equal length n. Fit y = b0 + b1*x + b2*x**2 as a linear model via OLS and return its parameters.

Requirements:

  • Build a design matrix with columns [x, x**2] and fit OLS with an intercept.
  • Return a 1-D float ndarray of shape (3,) ordered [b0_intercept, b1_linear, b2_quadratic] .

Example:

x = np.array([0., 1., 2., 3.])
y = 1 + 2*x + 3*x**2          # exact quadratic
fit_quadratic(x, y)
# array([1., 2., 3.])   # [b0, b1, b2]

Constraints & Assumptions

  • x and y are 1-D float arrays of the same length n .
  • The output order is fixed: intercept, linear coefficient, quadratic coefficient.

What a Strong Answer Covers Guidance

  • Builds a design matrix with an intercept, x and x**2 , or an equivalent setup that fits the intercept separately.
  • Solves the least-squares problem with a numerically sound method.
  • Returns a shape-(3,) float array in the order [b0, b1, b2] .
  • Explains why a quadratic in x is still a linear regression.
  • Notes when the coefficients are not uniquely determined.

Follow-up Questions Guidance

  • Why does this stay a linear regression problem?
  • What breaks numerically for high-degree fits, and how do you help?
  • On real, noisy data, how would you choose the polynomial degree?