Missing Data Imputation and Regression Pipeline

Reported in interviews at Squarepoint Capital Two Sigma

Problem statement

You are given a feature matrix X and a target vector y. Some entries of X are missing (represented as None). Build a small modeling pipeline that cleans the data and fits a linear model.

Implement a function with the signature:

def impute_and_fit(X, y):
    ...
  • X is an n x p matrix given as a list of rows; each entry is a float or None (missing).
  • y is the length-n target vector.

Requirements:

  1. Impute each column's missing (None) entries with that column's mean over the observed (non-None) values. If an entire column is missing, fill it with 0.0.
  2. Fit ordinary least squares WITH an intercept on the cleaned data.
  3. Return the coefficient vector [intercept, b1, b2, ...] as a plain list of floats (intercept first, then one slope per feature column).

Constraints:

  • Handle edge cases: all-None columns, single-feature inputs, and columns with several missing entries.

Example

impute_and_fit(
    [[1.0, None], [None, 3.0], [3.0, 5.0], [4.0, 6.0]],
    [2.1, 4.0, 6.2, 7.9],
) -> [-2.218474576271187, 1.7166101694915263, 0.5766101694915252]

Column 1's missing value is filled with the mean of its observed entries (1+3+4)/3, column 2's with (3+5+6)/3; then OLS with an intercept is fit and the returned list is [intercept, b_x1, b_x2].

Show hint

What single statistic of each column's observed values should replace its missing entries?

Relevant roles: ML Engineer, Data Scientist, Forward Deployed Engineer and Applied Scientist.

Related free problems