Loading...
Traverse a decision tree to make predictions.
A decision tree is represented as nested dictionaries. Each internal node has:
Each leaf node has:
Given a tree and a list of data points (each a list of feature values), return the predicted class for each point.
tree = {"feature": 0, "threshold": 5, "left": {"class": 0}, "right": {"class": 1}}
X = [[3], [7], [5]][0, 1, 0]
X. For the first point [3], we check the feature value at index 0 (3) against the threshold (5).0. The same process applies to the third point [5], as 5≤5 also leads to the left subtree.[7], the feature value (7) is greater than the threshold (5), so we move to the right subtree and predict class 1.[0, 1, 0].