TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Impute Missing Values (mean/median)

Data Processing
Hard

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.

Loading visualization...

Examples

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]

Hint 1

Use np.isnan to separate missing and observed entries.

Hint 2

Work column by column for a two-dimensional input and assign into a copied float array.

Requirements

  • Compute each fill value from observed entries only
  • Fill an entirely missing feature with 0.0
  • Preserve observed entries and input shape
  • Return a new NumPy array

Constraints

  • X is a nonempty one-dimensional or two-dimensional numeric list that may contain NaN
  • strategy is mean or median
  • Use NumPy only
Try Similar Problems
Mean Rating ImputationStratified SplitKfold SplitZscore StandardizationMinmax Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: string

You must run your code first.
PrevNext

Impute Missing Values (mean/median)

Data Processing
Hard

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.

Loading visualization...

Examples

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]

Hint 1

Use np.isnan to separate missing and observed entries.

Hint 2

Work column by column for a two-dimensional input and assign into a copied float array.

Requirements

  • Compute each fill value from observed entries only
  • Fill an entirely missing feature with 0.0
  • Preserve observed entries and input shape
  • Return a new NumPy array

Constraints

  • X is a nonempty one-dimensional or two-dimensional numeric list that may contain NaN
  • strategy is mean or median
  • Use NumPy only
Try Similar Problems
Mean Rating ImputationStratified SplitKfold SplitZscore StandardizationMinmax Normalization

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

Accepts: string

You must run your code first.