Training accuracy
Test accuracy
Leaves
Depth
    • Training points
    • Test points

    Split by

    More information about decision trees

    Decision tree builder

    A decision tree classifies by asking one simple question after another: is x smaller than 0.4? Is y larger than −0.1? Each answer sends a point down one branch, until a leaf decides its class. Pick a data set or click in your own points, set the maximum depth, and watch the tree cut the plane into boxes, choosing at every step the cut that separates the classes best. Then compare training with test points and see where a deeper tree stops learning and starts memorising. Pick a data set, or choose Your own points and click in the plane to add points of the chosen class (right-click removes one). The tree splits the points with straight cuts, always choosing the cut that makes the two groups purest, until it reaches the maximum depth. The coloured boxes show what each leaf votes for. Click a box or a node to see its points and its rule. Filled dots train the tree, rings test it: the chart shows how deeper trees fit the training points ever better while the test points stop improving. Press Play to grow the tree one level at a time.

    How it works

    1. Choose a data set, such as three clusters, a diagonal or XOR, or choose Your own points and click in the plane to add them.
    2. Set the maximum depth, the smallest leaf and whether to split by Gini impurity or information gain; press Play to grow the tree level by level.
    3. Click a box or a node to see its points and its rule, and read in the chart where the test accuracy stops improving.

    How a tree chooses its questions

    At every node the tree tries every possible cut: between each two neighbouring values of x, and of y. For each cut it measures how mixed the two groups would be, with the Gini impurity (the chance that two points drawn at random from a group belong to different classes) or with the entropy. It keeps the cut that lowers the impurity most, and then does the same again in each half. Because every cut is parallel to an axis, the tree divides the plane into boxes, and a sloping boundary becomes a staircase.

    Greedy, and prone to overfit

    The tree picks the best cut for the moment, without looking ahead. On XOR, where the four corners alternate, no single first cut helps much, so the tree can waste its first levels on small gains at the edges and need several levels more than the two well-chosen cuts a person would make. Left to grow, a tree carves out a box for every stray point: it gets every training point right and starts to fail on new ones. Limiting the depth, requiring a minimum number of points per leaf, or averaging many trees in a random forest keeps that in check.

    Frequently asked questions

    What is the difference between Gini impurity and information gain?

    Both measure how mixed a group of points is. Gini impurity is the chance that two points picked at random belong to different classes; entropy, used for information gain, is the number of bits needed on average to say which class a point belongs to. They usually choose the same or nearly the same cuts. Gini is a little faster to compute, which is why CART and many libraries use it by default.

    Why does the training accuracy keep rising but the test accuracy does not?

    A deeper tree can always fit the training points better, down to a box for every single point, including the noisy ones. Those last boxes describe accidents of this particular sample, not the pattern behind it, so they do not help, or even hurt, on new points. The depth where the test accuracy is highest is the best compromise.

    Why are the boundaries made of straight lines?

    Each node asks about one input at a time, such as x < 0.42, which is a straight line parallel to an axis. A diagonal or curved boundary can only be approximated by many small steps. Oblique trees, which test combinations like x + y < 0.3, and neural networks can draw such boundaries directly.

    What is a random forest?

    Many trees, each grown on a random sample of the training data and allowed to choose from a random subset of the inputs at each node, which then vote. The trees make different mistakes, so their vote is much more reliable than any single tree. Random forests and gradient-boosted trees are among the most successful methods for tabular data.

    How do you prevent overfitting in a decision tree?

    Stop the tree before it cuts boxes around single noisy points: lower the Maximum depth or raise Points per leaf, at least. Libraries also prune a full tree back afterwards or average many trees in a random forest. The chart shows the depth at which the test accuracy is highest.

    Decision Tree Builder: how do I record a video?

    Press the red Record button in the playback bar: it films what the stage shows. Press it again to stop, and the video is saved to your device. Recording is free; you only need to sign in. Without a subscription the video carries a small PictorX logo in a corner. The button beside Record opens the video options: the shape (as shown, square, story 9:16 or wide 16:9), the length (until you stop, or 5, 10, 15 or 30 seconds for social media) and the file: MP4, WebM or an animated GIF. You can add one of three music tracks: calm, bright or beat.

    Ready to try it?

    Open PictorX in your browser — no install, no credit card required to start.

    Go to Editor