Fit a simple OLS line in NumPy and predict new points

Quick Overview

Implement a NumPy function that fits a simple ordinary least squares line on one feature and returns predictions for new inputs, including points outside the training range. It tests the closed-form slope and intercept, vectorized prediction, output shape and handling of degenerate input.

Fit a simple OLS line in NumPy and predict new points

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_and_predict(x, y, x_new)` that fits a simple ordinary least squares (OLS) line `intercept + slope * x` on the training arrays `x` and `y`, then returns `intercept + slope * x_new`. Requirements: - Predict every value in `x_new`, including points outside the training range. - Return a 1-D float `np.ndarray` of shape `(m,)` with `m = len(x_new)`. Do not return the coefficients or a scalar. - Any of `np.polyfit`, the normal equations, or `np.linalg.lstsq` is fine. The result is checked with `numpy.testing.assert_allclose`. Example: ```python x = np.array([0., 1., 2., 3.]) y = np.array([1., 3., 5., 7.]) # y = 1 + 2x x_new = np.array([-1., 4.]) fit_and_predict(x, y, x_new) # array([-1., 9.]) ``` ```hint Fit once, predict in one expression Compute the two coefficients from the training data a single time, then apply them to the whole `x_new` array with one NumPy expression. Check the shape and dtype of what you return. ``` ### Constraints & Assumptions - `x` and `y` are 1-D numeric arrays of the same length `n`. - `x_new` is a 1-D numeric array of length `m`; its values may lie outside the range of `x`. - The output must match a reference fit to floating-point tolerance (`assert_allclose`). ### What a Strong Answer Covers - How the slope and intercept are computed (closed form or a NumPy solver) and why they minimize the squared error. - Returning one float prediction per element of `x_new`, with no special handling for extrapolated points. - Converting inputs to 1-D float arrays so lists and integer arrays work. - What happens when every `x` value is the same and the slope is undefined. - The time and space cost of the fit and the prediction. ### Follow-up Questions - How would you extend this to several features, and why is `np.linalg.lstsq` safer than inverting $X^\top X$ directly? - How should the function behave when all `x` values are equal? - How would you express uncertainty for a prediction far outside the training range? - How would you fit the line on a stream of points too large to hold in memory?

Overview: Implement a NumPy function that fits a simple ordinary least squares line on one feature and returns predictions for new inputs, including points outside the training range. It tests the closed-form slope and intercept, vectorized prediction, output shape and handling of degenerate input.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherMachine Learning
0
0

Implement fit_and_predict(x, y, x_new) that fits a simple ordinary least squares (OLS) line intercept + slope * x on the training arrays x and y, then returns intercept + slope * x_new.

Requirements:

  • Predict every value in x_new , including points outside the training range.
  • Return a 1-D float np.ndarray of shape (m,) with m = len(x_new) . Do not return the coefficients or a scalar.
  • Any of np.polyfit , the normal equations, or np.linalg.lstsq is fine. The result is checked with numpy.testing.assert_allclose .

Example:

x = np.array([0., 1., 2., 3.])
y = np.array([1., 3., 5., 7.])   # y = 1 + 2x
x_new = np.array([-1., 4.])
fit_and_predict(x, y, x_new)     # array([-1.,  9.])

Constraints & Assumptions

  • x and y are 1-D numeric arrays of the same length n .
  • x_new is a 1-D numeric array of length m ; its values may lie outside the range of x .
  • The output must match a reference fit to floating-point tolerance ( assert_allclose ).

What a Strong Answer Covers Guidance

  • How the slope and intercept are computed (closed form or a NumPy solver) and why they minimize the squared error.
  • Returning one float prediction per element of x_new , with no special handling for extrapolated points.
  • Converting inputs to 1-D float arrays so lists and integer arrays work.
  • What happens when every x value is the same and the slope is undefined.
  • The time and space cost of the fit and the prediction.

Follow-up Questions Guidance

  • How would you extend this to several features, and why is np.linalg.lstsq safer than inverting X⊤XX^\top X directly?
  • How should the function behave when all x values are equal?
  • How would you express uncertainty for a prediction far outside the training range?
  • How would you fit the line on a stream of points too large to hold in memory?