Yesterday's Average

Machine Learning in CodeMedium+10 XP+10

Yesterday's Average

A fitted imputer fills every later gap with the number it learned from the training rows, never with an average of the new rows.

Task: write fill_later(train_values, later_values, strategy).

  • Both lists hold numbers with some None gaps. train_values has at least one real number.
  • Fit a SimpleImputer(strategy=strategy) on train_values only. strategy is "mean" or "median".
  • Use it to fill the gaps in later_values.
  • Return [learned value, filled later values]. The learned value is the imputer's statistics_ entry. Round it and every filled value to 2 decimal places.

Examples

train_values = [150.0, None, 170.0, 130.0, None, 160.0]
later_values = [145.0, None, 165.0]
strategy = "mean"
returns [152.5, [145.0, 152.5, 165.0]]
train_values = [10, 12, 11, 90, None]
later_values = [None, None, 50]
strategy = "median"
returns [11.5, [11.5, 11.5, 50.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run