Predict with a Serialised Decision Tree

easy
Unit 5 · Clustering & Decision Trees0/3 to milestone

Trained trees are often exported as nested dicts. Each internal node is {"feature": j, "threshold": t, "left": ..., "right": ...} (go left when x[j] <= t), and each leaf is {"value": v}.

Implement:

  • predict_one(tree, x): walk from the root to a leaf (iteratively, so very deep trees don't hit the recursion limit)
  • predict(tree, X): a list of predictions
  • depth(tree): number of edges on the longest root-to-leaf path (a lone leaf has depth 0)
#decision-trees#inference2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Predict with a Serialised Decision Tree · TechGradify