Neighbours or a Tree?

Machine Learning in CodeHard+10 XP+10

Neighbours or a Tree?

You have two models and one dataset. Which one should you trust? Give both the same fair test.

Task: write compare_models(name, seed). name is "iris" or "wine".

  • Load the dataset as X and y, and split with train_test_split(X, y, test_size=0.25, random_state=seed, stratify=y).
  • Train a KNeighborsClassifier(n_neighbors=5) and a DecisionTreeClassifier(random_state=0) on the same training rows.
  • Score both on the same test rows. Round each score to 3 decimal places.
  • Return a dict: {"knn": knn score, "tree": tree score, "winner": ...}.
  • "winner" is "knn" or "tree" for whichever rounded score is higher, or "tie" if the rounded scores are equal.

Examples

name = "wine"
seed = 3
returns {'knn': 0.667, 'tree': 0.933, 'winner': 'tree'}
name = "iris"
seed = 3
returns {'knn': 0.947, 'tree': 0.921, 'winner': 'knn'}

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