The Seedling Sorter

Decision Trees & Ensemble LearningHard+30 XP+30

The Seedling Sorter

A greenhouse sorts young tomato plants into three bins: "sell", "grow" (keep growing) and "compost". Each plant is measured as [height_cm, leaf_count]. Instead of writing the sorting rules by hand, the team wants a decision tree grown from past plants, with stopping rules so it can't keep splitting until it has memorised every plant.

Growing the tree. All training rows start at the root, which is at depth 0. At every node:

  1. Stop and make a leaf if any of these holds:
    • every row in the node has the same label (the node is pure),
    • the node's depth equals max_depth,
    • the node has fewer than min_samples_split rows,
    • no split is possible, because each feature has only one value across the node's rows.
  2. Otherwise, split on the best question. For each feature, sort the distinct values that feature takes in this node, and try a threshold halfway between each neighbouring pair. Rows with value <= threshold go left, the rest go right. Keep the split with the lowest score (defined below).
  3. Grow both children the same way, one level deeper.

A split's score is the weighted Gini impurity of its two groups:

score=nLn GL+nRn GR,G=1−∑labelsp2\text{score} = \frac{n_L}{n}\,G_L + \frac{n_R}{n}\,G_R, \qquad G = 1 - \sum_{\text{labels}} p^2score=nnL​​GL​+nnR​​GR​,G=1−labels∑​p2

Here nLn_LnL​ and nRn_RnR​ count the rows sent each way, nnn counts the node's rows, and ppp is a label's share of the rows in that group. Treat two scores within 10−910^{-9}10−9 of each other as a tie, and break ties by taking feature 0 before feature 1, then the smaller threshold.

A leaf predicts the label held by most of its rows. If two labels tie for most, it predicts the one that comes first alphabetically.

Task: write grow_and_predict(X_train, y_train, X_test, max_depth, min_samples_split). Grow the tree, then return a tuple (predictions, leaves):

  • predictions: the predicted label for each row of X_test, in order. A row goes left at a node when its value is <= that node's threshold.
  • leaves: the number of leaves in the finished tree.

There, a height split at 3 and a leaf-count split at 1.5 both score 0, so the tie rule picks height. The new plant has height 3≤33 \le 33≤3, so it goes left.

Build Your Own Tree

Pick a feature for the root question and watch the games split into groups.

Choose a root question above to split the games.

Choosing the first question is the most important decision in building a tree.

Examples

X_train = [[3, 2], [5, 4], [6, 3], [8, 5], [9, 7], [10, 4], [11, 6], [13, 8], [14, 5], [15, 9], [16, 7], [18, 10]]
y_train = ['compost', 'compost', 'grow', 'compost', 'grow', 'grow', 'sell', 'sell', 'grow', 'sell', 'sell', 'sell']
X_test = [[7, 4], [12, 5.2], [17, 8]]
max_depth = 2
min_samples_split = 2
returns (['grow', 'grow', 'sell'], 4)
X_train = [[3, 2], [5, 4], [6, 3], [8, 5], [9, 7], [10, 4], [11, 6], [13, 8], [14, 5], [15, 9], [16, 7], [18, 10]]
y_train = ['compost', 'compost', 'grow', 'compost', 'grow', 'grow', 'sell', 'sell', 'grow', 'sell', 'sell', 'sell']
X_test = [[7, 4], [10.2, 6], [16, 3]]
max_depth = 1
min_samples_split = 2
returns (['compost', 'compost', 'sell'], 2)

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