Fit multiple OLS with an intercept and return the slope array and the intercept as a float

Quick Overview

Write a Python function that fits a multiple linear regression with an intercept and returns the slope coefficients as an array and the intercept as a float. It tests fitting with NumPy or scikit-learn, splitting the parameter vector correctly, and choosing a numerically stable least-squares solver.

Fit multiple OLS with an intercept and return the slope array and the intercept as a float

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `fit_coef_and_intercept(X, y)` that fits an ordinary least-squares regression of `y` on all columns of `X` with an intercept and returns `(coef, intercept)`. Any OLS technique is accepted (normal equations, `np.linalg.lstsq`, or `sklearn.linear_model.LinearRegression`). Requirements: - Fit with an intercept (do not force the line through the origin). - `coef`: 1-D `np.ndarray` of shape `(p,)`, in the same column order as `X`. - `intercept`: Python `float` (not a 0-d or length-1 array). With `LinearRegression`, use `float(model.intercept_)`. ```hint Where the intercept lives If you add a column to `X` yourself, its position decides which entry of the solution vector is the intercept and which entries are slopes. Count the entries you return in `coef`. ``` ### Constraints & Assumptions - `X` is a full-rank float ndarray of shape `(n, p)` with n=40 and p=3. - `y` has shape `(n,)`. ### What a Strong Answer Covers - Fitting with an intercept so the fit is not forced through the origin. - Splitting the fitted parameters into a slope array of shape `(p,)` in column order and a scalar intercept. - Returning the intercept as a Python `float`, not a NumPy scalar or array. - Which solver is numerically sound, and what the full-rank assumption guarantees. ### Follow-up Questions - How would you compute `coef` and `intercept` from the normal equations directly? - Why is `np.linalg.lstsq` usually preferred over solving the normal equations? - How do the returned values change if you standardize every column of `X` before fitting?

Overview: Write a Python function that fits a multiple linear regression with an intercept and returns the slope coefficients as an array and the intercept as a float. It tests fitting with NumPy or scikit-learn, splitting the parameter vector correctly, and choosing a numerically stable least-squares solver.

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

Implement fit_coef_and_intercept(X, y) that fits an ordinary least-squares regression of y on all columns of X with an intercept and returns (coef, intercept). Any OLS technique is accepted (normal equations, np.linalg.lstsq, or sklearn.linear_model.LinearRegression).

Requirements:

  • Fit with an intercept (do not force the line through the origin).
  • coef : 1-D np.ndarray of shape (p,) , in the same column order as X .
  • intercept : Python float (not a 0-d or length-1 array). With LinearRegression , use float(model.intercept_) .

Constraints & Assumptions

  • X is a full-rank float ndarray of shape (n, p) with n=40 and p=3.
  • y has shape (n,) .

What a Strong Answer Covers Guidance

  • Fitting with an intercept so the fit is not forced through the origin.
  • Splitting the fitted parameters into a slope array of shape (p,) in column order and a scalar intercept.
  • Returning the intercept as a Python float , not a NumPy scalar or array.
  • Which solver is numerically sound, and what the full-rank assumption guarantees.

Follow-up Questions Guidance

  • How would you compute coef and intercept from the normal equations directly?
  • Why is np.linalg.lstsq usually preferred over solving the normal equations?
  • How do the returned values change if you standardize every column of X before fitting?