Swap the Model

Machine Learning in CodeEasy+10 XP+10

Swap the Model

You want to compare two different models on the same small quiz. The point is that only the line that makes the model changes.

Task: write quiz_both_models(train_rows, train_labels, check_rows, check_labels).

  • Train a KNeighborsClassifier(n_neighbors=3) on train_rows and train_labels.
  • Train a DecisionTreeClassifier(random_state=0) on the same rows.
  • Score each model on check_rows and check_labels. The score is the fraction of check rows it gets right.
  • Return a dict {"knn": ..., "tree": ...} with each score rounded to 2 decimal places.

Examples

train_rows = [[150, 7], [170, 8], [130, 3], [110, 2], [160, 6]]
train_labels = ["apple", "apple", "lemon", "lemon", "apple"]
check_rows = [[165, 7], [125, 2], [118, 3]]
check_labels = ["apple", "lemon", "apple"]
returns {'knn': 0.67, 'tree': 0.67}
train_rows = [[1, 1], [2, 1], [1, 2], [8, 9], [9, 8], [9, 9]]
train_labels = ["low", "low", "low", "high", "high", "high"]
check_rows = [[0, 1], [9, 10], [2, 2], [8, 8]]
check_labels = ["low", "high", "low", "high"]
returns {'knn': 1.0, 'tree': 1.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