Fill, Scale, Vote

Machine Learning in CodeMedium+10 XP+10

Fill, Scale, Vote

A clinic's records have gaps. A three-step pipeline can fill them, scale the columns and classify, all from one fit.

Task: write predict_with_gaps(train_rows, train_labels, new_rows).

  • Rows are lists of numbers, with None where a value is missing. Turn them into arrays with np.array(rows, dtype=float), which makes each None a np.nan.
  • Build a Pipeline with three steps, in this order:
    1. ("fill", SimpleImputer(strategy="median"))
    2. ("scale", StandardScaler())
    3. ("model", KNeighborsClassifier(n_neighbors=3))
  • Fit it on the training rows, then predict new_rows. New rows can have gaps too.
  • Return the predictions as a list.

Examples

train_rows = [[120, 36.6], [None, 36.8], [130, 36.5], [180, 39.0], [175, None], [190, 39.4]]
train_labels = ["well", "well", "well", "ill", "ill", "ill"]
new_rows = [[185, 39.1], [None, 36.7]]
returns ['ill', 'well']
train_rows = [[1, 100], [2, 110], [None, 105], [9, 300], [8, None], [10, 310]]
train_labels = ["a", "a", "a", "b", "b", "b"]
new_rows = [[9, None], [2, 100], [None, 300]]
returns ['a', 'a', 'b']

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