Explore Library
Code QuizIntermediate

Limiting Tree Depth

Spot why this depth-limited tree builder never actually stops on depth.

Codepython
def build_tree(data, depth, max_depth):
    if depth > max_depth or is_pure(data):
        return make_leaf(data)
    feature, threshold = best_split(data)
    left, right = split(data, feature, threshold)
    return {
        'feature': feature,
        'threshold': threshold,
        'left': build_tree(left, depth, max_depth),
        'right': build_tree(right, depth, max_depth),
    }

max_depth is supposed to cap tree growth to reduce overfitting. What is the bug?