Median Imputer With Missing Indicators
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `MedianMissingIndicatorImputer`, a scikit-learn transformer over pandas DataFrames. `fit` learns per-column medians for numeric (non-bool) columns. `transform` fills nulls with those medians and appends a `<col>_missing` indicator for every numeric column that had nulls during fit.
The transformer must meet these requirements:
- Store `medians_` (a float64 Series indexed by numeric column) and `indicator_columns_` (a list in fit column order). An all-null numeric column gets median `0.0`.
- The output has the fit columns in fit order, then the indicators. Numeric columns become float64, indicators are int64 (0/1), and non-numeric columns pass through unchanged. The input index is preserved.
- `transform` reorders the columns to fit order and drops extra columns. A fit column absent at transform time raises `ValueError`. Non-DataFrame input raises `TypeError`.
- `get_feature_names_out()` returns the output column names as an object array.
- Inputs are never mutated. The transformer must work with `clone` and `fit_transform`.
For example:
```python
train = pd.DataFrame({"a": [1.0, np.nan, 3.0, 10.0],
"b": [2.0, 4.0, 6.0, 8.0],
"c": ["x", "y", None, "z"]})
imp = MedianMissingIndicatorImputer().fit(train)
# medians_ -> a: 3.0, b: 5.0 ; indicator_columns_ -> ["a"]
imp.transform(pd.DataFrame({"a": [np.nan], "b": [np.nan], "c": ["q"]}))
# a b c a_missing
# 0 3.0 5.0 q 1
```
Column `b` is imputed at transform time but gets no indicator, because it had no nulls during fit.
```hint Freeze the schema in fit
Decide in `fit` which columns are numeric and which get an indicator, and store that. `transform` should only read this state, whatever nulls the new data contains.
```
```hint Bool columns
`is_numeric_dtype` returns True for bool columns, so they need to be excluded explicitly.
```
### Constraints & Assumptions
- `X` is a pandas DataFrame with unique column names. Columns can have any dtype, including nullable ones such as `Int64` and `boolean`.
- `fit` accepts a `y` argument and ignores it, so the transformer fits into a `Pipeline`.
- No constructor parameters are required.
### Clarifying Questions to Ask
- If a column was numeric at fit time but arrives as strings at transform time, should `transform` raise or try to coerce it?
- What should happen if an input already has a column named like an indicator, such as `a_missing`?
- Should calling `transform` before `fit` raise scikit-learn's `NotFittedError`?
### What a Strong Answer Covers
- Numeric columns detected by dtype with bool excluded, and every piece of state (column order, numeric set, medians, indicator set) fixed in `fit`.
- A median that skips nulls, works on the float64 cast of the column and falls back to `0.0` for an all-null column.
- The output contract: fit order, extra columns dropped, int64 indicators last and computed from the incoming nulls, non-numeric columns unchanged, input index kept, names matching `get_feature_names_out()`.
- The `TypeError` and `ValueError` cases, and a freshly built output frame so the caller's data is never mutated.
- Compatibility with `clone`, `fit_transform` and `Pipeline`.
### Follow-up Questions
- Why not add an indicator for a column that only has nulls at transform time?
- When is the median a poor fill value, and what would you use instead?
- How much do the missing indicators help a linear model compared with a gradient-boosted tree that handles NaN natively?
Overview: Build a scikit-learn transformer for pandas DataFrames that fills numeric nulls with training medians and appends missing-value indicator columns. It tests fit-time schema decisions, dtype handling for bool and nullable columns, column ordering, and writing transformers that work with clone and pipelines.