Replace NaNs With the Mean

easynumpy_manipulation

Implement replace_nans(matrix).

Return a copy of matrix with every NaN replaced by the mean of all non-NaN values. If every value is NaN (or the matrix is empty), return zeros of the same shape. Leave the input unchanged. No for/while loops.

Constraints
  • no for/while loops

Your solution

Edit replace_nans and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

Build a boolean mask with np.isnan; if it's all True, fill zeros, else assign matrix[~mask].mean() at the masked positions.