Return only the fitted intercept of a multiple OLS regression as a Python float

Quick Overview

Write a Python function that fits a multiple linear regression with an intercept and returns only the fitted intercept as a plain float. It tests how an intercept enters the design matrix, where to read it from the fit, and how NumPy scalar types differ from Python floats.

Return only the fitted intercept of a multiple OLS regression as a Python float

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `def fitted_intercept(X: np.ndarray, y: np.ndarray) -> float` that fits a linear regression of `y` on all columns of `X` with an intercept, and returns only the fitted intercept. Requirements: - Fit with an intercept on all `p` columns of `X`. Any OLS technique is accepted. - Return a Python `float` (`type(result) is float`): not an array, tuple, or coefficient vector. ```hint Check the return type Most numerical libraries hand back NumPy scalars or arrays. Look at what `type()` reports for the value you plan to return, since the check compares the type exactly. ``` ### Constraints & Assumptions - `X` is a float ndarray of shape `(40, 3)`. - `y` is a float ndarray of shape `(40,)`. ### What a Strong Answer Covers - Fitting with an intercept on all three columns, either by adding a ones column or by using a library that fits one. - Where the intercept sits in the fitted result. - Returning a plain Python `float`, and why a NumPy scalar fails `type(result) is float`. - When the intercept is not identified by the data. ### Follow-up Questions - How does the fitted intercept relate to the means of `X` and `y`? - What happens to the intercept if you center every column of `X` before fitting? - How would you get a confidence interval for the intercept?

Overview: Write a Python function that fits a multiple linear regression with an intercept and returns only the fitted intercept as a plain float. It tests how an intercept enters the design matrix, where to read it from the fit, and how NumPy scalar types differ from Python floats.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement def fitted_intercept(X: np.ndarray, y: np.ndarray) -> float that fits a linear regression of y on all columns of X with an intercept, and returns only the fitted intercept.

Requirements:

  • Fit with an intercept on all p columns of X . Any OLS technique is accepted.
  • Return a Python float ( type(result) is float ): not an array, tuple, or coefficient vector.

Constraints & Assumptions

  • X is a float ndarray of shape (40, 3) .
  • y is a float ndarray of shape (40,) .

What a Strong Answer Covers Guidance

  • Fitting with an intercept on all three columns, either by adding a ones column or by using a library that fits one.
  • Where the intercept sits in the fitted result.
  • Returning a plain Python float , and why a NumPy scalar fails type(result) is float .
  • When the intercept is not identified by the data.

Follow-up Questions Guidance

  • How does the fitted intercept relate to the means of X and y ?
  • What happens to the intercept if you center every column of X before fitting?
  • How would you get a confidence interval for the intercept?