Replace missing values in each feature with that feature's observed mean or median. For a two-dimensional input, compute the statistic independently for every column. Treat a one-dimensional input as one feature. Fill a feature containing only missing values with 0.0. Preserve every observed value and return a floating-point NumPy array without modifying the input.
Input: X = [[1, nan], [3, 5]], strategy = "mean"
Output: [[1.0, 5.0], [3.0, 5.0]]
Explanation: The second column has one observed value, so its missing entry is filled with 5.
Input: X = [[nan, 2], [nan, 4]], strategy = "median"
Output: [[0.0, 2.0], [0.0, 4.0]]
Input: X = [1, nan, 3, nan, 5], strategy = "mean"
Output: [1.0, 3.0, 3.0, 3.0, 5.0]
Use np.isnan to separate missing and observed entries.
Work column by column for a two-dimensional input and assign into a copied float array.
Sign in to take notes on this problem
Accepts: array
Accepts: string
Replace missing values in each feature with that feature's observed mean or median. For a two-dimensional input, compute the statistic independently for every column. Treat a one-dimensional input as one feature. Fill a feature containing only missing values with 0.0. Preserve every observed value and return a floating-point NumPy array without modifying the input.
Input: X = [[1, nan], [3, 5]], strategy = "mean"
Output: [[1.0, 5.0], [3.0, 5.0]]
Explanation: The second column has one observed value, so its missing entry is filled with 5.
Input: X = [[nan, 2], [nan, 4]], strategy = "median"
Output: [[0.0, 2.0], [0.0, 4.0]]
Input: X = [1, nan, 3, nan, 5], strategy = "mean"
Output: [1.0, 3.0, 3.0, 3.0, 5.0]
Use np.isnan to separate missing and observed entries.
Work column by column for a two-dimensional input and assign into a copied float array.
Sign in to take notes on this problem
Accepts: array
Accepts: string