Handle Missing Data (Mean Imputation) - Data Preprocessing
Implement Handle Missing Data (Mean Imputation) in Python with NumPy. medium exercise with examples, hints, and instant feedback. No setup or signup required.
Handle Missing Data (Mean Imputation)
What you'll build
A function that replaces every missing value (NaN) in a 1-D array with the mean of the values that are present.
Background
Most models can't consume NaN. The simplest fix is imputation: fill each gap with a statistic of the observed values. Mean imputation keeps the column mean unchanged; it is SimpleImputer(strategy='mean') in scikit-learn. (In a real pipeline the mean must come from the training split only: see Leakage-Safe Imputation and Scaling.)
Formula
observed = { x[i] : x[i] is not NaN }
fill = sum(observed) / |observed|
out[i] = fill if x[i] is NaN else x[i]
Inputs
arr: 1-D float array of shape(n,)with at least one non-NaN value; missing entries arenp.nan(tests may pass a Python list)
Expected return format
A new float array of shape (n,) with no NaNs. Values that were present are unchanged.
Edge cases and conventions
- An array with no NaNs is returned unchanged (as a copy).
- Don't modify the input:
arr[mask] = von the caller's array would change their data. Work on a copy. np.meanof an array containing NaN is NaN, so you must exclude the missing values when computing the fill.
Constraints
- Use NumPy; vectorise (no Python loop over elements).
Follow-up
- When is the median a better fill than the mean?
- Why can adding a binary "was missing" feature help a model?
Examples
Input: fill_missing_with_mean(np.array([1.0, np.nan, 3.0, np.nan, 5.0])) Output: [1.0, 3.0, 3.0, 3.0, 5.0]
The observed values 1, 3, 5 have mean 3, which fills both gaps.
Input: fill_missing_with_mean(np.array([np.nan, 10.0, 20.0])) Output: [15.0, 10.0, 20.0]
A NaN at the start is filled the same way: mean(10, 20) = 15.
Practice in Python
import numpy as np
def fill_missing_with_mean(arr: np.ndarray) -> np.ndarray:
"""
Replace NaN values with the mean of the non-NaN values.
Args:
arr: 1-D float array of shape (n,), at least one value not NaN
Returns:
New float array of shape (n,) with every NaN replaced by the mean
(the input array is left unchanged)
"""
# Your code here
pass
The interactive editor loads with Python and NumPy. Run your implementation against 6 tests, or use the progressive hints.