One Tree or a Crowd?

Machine Learning in CodeMedium+10 XP+10

One Tree or a Crowd?

A single fully grown tree memorises. A forest of them votes, and their different mistakes cancel out. You want to see how many trees it takes.

Write tree_versus_forests(sizes) for the wine data.

  • Load the data with load_wine(return_X_y=True, as_frame=True) and split it with train_test_split(X, y, test_size=0.25, random_state=0, stratify=y).
  • Fit one DecisionTreeClassifier(random_state=0) and score it on the test rows.
  • For each value in sizes, in order, fit RandomForestClassifier(n_estimators=value, random_state=0) and score it on the test rows.

Return a dict:

  • "tree": the single tree's test score, rounded to 3 places.
  • "forests": a list of the forests' test scores, in the order of sizes, each rounded to 3 places.

Examples

sizes = [5, 25]
returns {'tree': 0.889, 'forests': [0.978, 1.0]}
sizes = [100]
returns {'tree': 0.889, 'forests': [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