Problems
Loading...
1 / 1

Impute Missing Values (mean/median)

Data Processing
Hard

Fill NaN values in each feature column using either column mean or column median.

Missing value imputation is a fundamental data preprocessing step that replaces NaN (Not a Number) values with meaningful estimates. Your function should compute the mean or median for each column using only the observed (non-NaN) values, then fill all NaN positions in that column with the computed statistic.

Function Arguments

  • X: array-like, shape (N, D) - Data with possible np.nan
  • strategy: 'mean' or 'median' - Imputation method
Loading visualization...

Examples

Input: X=[[1,nan],[3,5]], strategy='mean'

Output: [[1,5],[3,5]]

Input: X=[[nan,2],[nan,4]], strategy='median'

Output: [[0,2],[0,4]] (all-NaN col → 0)

Input: X=[1,nan,3,nan,5], strategy='mean'

Output: [1,3,3,3,5] (1D case)

Hint 1

Use np.isnan() to find NaN positions, then np.logical_not() for valid values.

Hint 2

For 2D arrays, iterate over columns. Use np.mean() for observed values.

Hint 3

Handle all-NaN columns by checking np.any() before computing statistics.

Requirements

  • Return NumPy array (N, D), no NaNs if imputable
  • Compute statistic per column on observed values only
  • Leave fully-NaN columns as-is or fill with 0 (fill with 0)
  • Do not change non-NaN values
  • Return a copy, don't modify input
  • Handle integer inputs by upcasting to float

Constraints

  • Handle integer inputs by upcasting to float
  • NumPy only; time limit: 300ms
Try Similar Problems