The Fruit Stall

Machine Learning in CodeMedium+10 XP+10

The Fruit Stall

A fruit stall keeps a small table of labelled fruit. The owner wants a model, an honest score for it, and answers for today's new fruit.

Task: write stall_model(table, new_rows, seed).

  • table is a dict mapping column names to lists. Its "fruit" column is the label. Every other column is a feature.
  • new_rows is a dict with the same feature columns in the same order, and no "fruit" column.
  • Split the features and labels with train_test_split(X, y, test_size=0.25, random_state=seed, stratify=y).
  • Train a KNeighborsClassifier(n_neighbors=3) on the training rows.
  • Return [test score rounded to 2 decimal places, list of predictions for new_rows].

Examples

table = {"weight": [150, 170, 130, 110, 160, 120, 165, 115], "sweet": [7, 8, 3, 2, 6, 3, 7, 2], "fruit": ["apple", "apple", "lemon", "lemon", "apple", "lemon", "apple", "lemon"]}
new_rows = {"weight": [158, 118], "sweet": [6, 3]}
seed = 0
returns [1.0, ['apple', 'lemon']]
table = {"fruit": ["plum", "plum", "melon", "melon", "plum", "melon", "plum", "melon", "plum", "melon", "plum", "melon"], "weight": [60, 70, 900, 1100, 65, 950, 75, 1000, 58, 1050, 68, 980], "seeds": [1, 1, 300, 420, 1, 380, 1, 400, 1, 350, 1, 390]}
new_rows = {"weight": [64, 1020, 300], "seeds": [1, 400, 1]}
seed = 1
returns [1.0, ['plum', 'melon', 'plum']]

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