Missing Data Imputation and Regression Pipeline
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):
...
Xis ann x pmatrix given as a list of rows; each entry is a float orNone(missing).yis the length-ntarget vector.
Requirements:
- 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 with0.0. - Fit ordinary least squares WITH an intercept on the cleaned data.
- 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-
Nonecolumns, 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.