Skip to content

decision tree

learning

Level 2 of 3

Also called a classification tree.

how it works

The idea in short steps and three pictures, then the tree grown on the same 8 points, 2 questions deep, step by step.

Given points whose shapes are known, a decision tree learns questions that name the shape of a new point. Each question is a cut, halfway between two neighbouring places the points stand at: across below 3.5 asks whether a point stands left of 3.5. To weigh a question, the tree reads every point of the group once to see which side it falls on; each read is one look. For a group it weighs every question, across and up, and asks the one that gains most. Below, 3 triangles low on the left and 5 squares, entropy 0.954. Their places across, 1, 2, 3, 4, 7 and 8, give 5 cuts, and up gives 5 more: 10 questions. That is fewer than 2(n - 1) = 14, the most 8 points can have, as some points share a place. Across below 3.5 gains 0.954 − (4/8 × 0.811 + 4/8 × 0), about 0.549, the most: the group's entropy less each side's entropy times its share, 4 of 8. Each side then becomes a group, split the same way. A group of one shape is a leaf, and so is one at the greatest depth, or one no question helps. A leaf takes the shape most of its points have.

02468100246810A3+5B3+1C0+4
trianglessquares3 + 5all 80.9543 + 1yes0.8110 + 4no0
questiongain
across below 3.5?0.549
across below 5.5?0.348
up below 1.5?0.199

The question gains 0.549.

With scripts on, a picker shows each question's two sides. The drawing and this line change with it.

The 8 points, 3 triangles and 5 squares, and three of the root's ten questions. Across below 3.5 and up below 3.5 tie for the most, 0.549; a tie goes to across.

Training points

02468100246810

Test points

02468100246810

The tree, each group's triangles+squares

A4+6B4+2C0+4D3+0E1+2
01234123greatest depthpoints wrongtraining points wrongtest points wrong

At most 2 deep, the tree has 3 leaves. It gets 1 of its 10 training points wrong and 1 of the 10 test points.

With scripts on, a slider sets the greatest depth from 1 to 3. Both planes, the tree, the chart and this line change with it.

A tree grown on 10 training points and checked on 10 test points it never saw. At depth 1 it gets 2 of its own points wrong and 4 test points. At 2, 1 in each set. At 3 none of its own, but 2 test points, as it carves out one odd triangle.
Triangles in a corner: 8 moments of its 9 steps, the steps between them left for the watch page
  1. step 1 of 9

    02468100246810A3+5

    8 points of two shapes, and a tree that asks at most 2 questions on the way to any leaf. Every point starts in one group, A.

    what it remembers to know where it is
    groups made
    1
    leaves found
    0

    watch from here

  2. step 2 of 9

    02468100246810A3+5

    Group A: weigh 10 questions, each a cut across or up halfway between two of its points. Another gains as much, so the tie goes to across.

    what it remembers to know where it is
    groups made
    1
    leaves found
    0

    watch from here

  3. step 3 of 9

    02468100246810A3+5B3+1C0+4

    Split group A by asking whether a point is across below 3.5. It gains 0.549. 4 points say yes and 4 say no, and each side is a new group below it.

    what it remembers to know where it is
    groups made
    3
    leaves found
    0

    watch from here

  4. step 5 of 9

    02468100246810A3+5B3+1C0+4D3+0E0+1

    Split group B by asking whether a point is up below 5.5. It gains 0.811. 3 points say yes and 1 says no, and each side is a new group below it.

    what it remembers to know where it is
    groups made
    5
    leaves found
    0

    watch from here

  5. step 6 of 9

    02468100246810A3+5B3+1C0+4D3+0E0+1

    Group C holds 0 triangles and 4 squares. Every point in it is one shape, so it is a leaf: a square.

    what it remembers to know where it is
    groups made
    5
    leaves found
    1

    watch from here

  6. step 7 of 9

    02468100246810A3+5B3+1C0+4D3+0E0+1

    Group D holds 3 triangles and 0 squares. Every point in it is one shape, so it is a leaf: a triangle.

    what it remembers to know where it is
    groups made
    5
    leaves found
    2

    watch from here

  7. step 8 of 9

    02468100246810A3+5B3+1C0+4D3+0E0+1

    Group E holds 0 triangles and 1 square. Every point in it is one shape, so it is a leaf: a square.

    what it remembers to know where it is
    groups made
    5
    leaves found
    3

    watch from here

  8. step 9 of 9

    02468100246810A3+5B3+1C0+4D3+0E0+1

    The tree has 3 leaves. Each leaf's part of the plane is shaded in its shape, the shape a new point there would get.

    what it remembers to know where it is
    groups made
    5
    leaves found
    3

    watch from here

the steps, in plain words
  1. start with every point in one groupPython line 18
  2. take the next group, top to bottomPython lines 19 and 20
  3. weigh every question across and up, and keep the one that gains mostPython lines 13, 14, 15, 16, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34 and 38
  4. split the group by it into two groups belowPython lines 39 and 40
  5. a group of one shape, at the greatest depth, or that no question helps, is a leafPython lines 21, 24, 35, 36 and 37
  6. shade each leaf's part of the plane in the shape most of its points havePython line 22
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

from math import log2


def decision_tree(points, depth):
    """The tree grown on points, breadth first, top to bottom.

    Each point is a tuple (across, up, shape), the shape "triangle" or
    "square"; depth is the most questions asked in a row. A question is
    ("across", cut) or ("up", cut): a point below the cut goes to the
    first group, the rest to the second. A leaf is (shape,). A tie goes to
    across, then the smaller cut; a leaf's tie to the triangles.
    """
    def entropy(group):
        n = len(group)
        tri = sum(1 for p in group if p[2] == "triangle")
        return -sum(c / n * log2(c / n) for c in (tri, n - tri) if c)

    tree, queue = [], [(points, 0)]  # every point in one group
    while queue:
        group, level = queue.pop(0)  # the next group, top to bottom
        tri = sum(1 for p in group if p[2] == "triangle")
        shape = "triangle" if 2 * tri >= len(group) else "square"
        scored = []
        if 0 < tri < len(group) and level < depth:
            for axis, name in ((0, "across"), (1, "up")):  # every question
                values = sorted({p[axis] for p in group})
                for a, b in zip(values, values[1:]):
                    cut = (a + b) / 2  # halfway between neighbours
                    below = [p for p in group if p[axis] < cut]
                    above = [p for p in group if p[axis] >= cut]
                    low, high = len(below) / len(group), len(above) / len(group)
                    gain = entropy(group) - low * entropy(below) - high * entropy(above)
                    scored.append((gain, name, cut, below, above))
        top = max((s[0] for s in scored), default=0)
        if top <= 1e-12:
            tree.append((shape,))  # one shape, the greatest depth, or no gain
            continue
        gain, name, cut, below, above = next(s for s in scored if abs(s[0] - top) < 1e-12)
        tree.append((name, cut))  # the question that gains most
        queue += [(below, level + 1), (above, level + 1)]
    return tree

how it scales

How the points looked at grow, from ten points to two thousand, on two sets of points.

Each step across or up is ten times the one before. From 10 to 100 points, the row's looks go from 90 to 9,900, about a hundred times: ten times across, a hundred times up. So both lines climb two steps up for each step across. A slope of 2 means the count grows as Θ(n2)\Theta(n^2), as section 4 works out.

points looked at, ten times more at each mark up

a seconda minutean houra dayslope 2102104106108101010121014101102103104105106107a seconda minutean houra dayslope 2101102103104105106107108109101010111012101310141015101102103104105106107a seconda minutean houra dayslope 2101102103104105106107108109101010111012101310141015101102103104105106107

nn, how many points have a shape, ten times more at each mark to the right

  • the diagonal, a triangle then a square by turns, grown 3 deep, where each question peels off one end pointsolid where measured by running it; dashed where its formula carries it on to ten millionslope 2: ten times n, 100 times the work
  • one row of points, triangles on the left half and squares on the right, which one question splitssolid where measured by running it; dashed where its formula carries it on to ten million
what the diagonal by turns costs, at a billion points looked at a second
nnpoints looked attime
1,0005,982,016about 6 milliseconds
100,00059,998,200,016about 60 seconds
10,000,000599,999,820,000,016about 6.9 days

The points looked at are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count a decision tree's looks yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each n it grows the tree on two sets
of n points, 3 questions deep at most, and prints how many points the
questions look at: one row in two halves, triangles then squares, and the
diagonal with a triangle then a square by turns. The counts are the same on
any computer; only the time differs.
"""

from math import log2


def decision_tree(points, depth):
    """The tree grown on points, breadth first, top to bottom.

    Each point is a tuple (across, up, shape), the shape "triangle" or
    "square"; depth is the most questions asked in a row. A question is
    ("across", cut) or ("up", cut): a point below the cut goes to the
    first group, the rest to the second. A leaf is (shape,). A tie goes to
    across, then the smaller cut; a leaf's tie to the triangles.
    """
    def entropy(group):
        n = len(group)
        tri = sum(1 for p in group if p[2] == "triangle")
        return -sum(c / n * log2(c / n) for c in (tri, n - tri) if c)

    tree, queue = [], [(points, 0)]  # every point in one group
    while queue:
        group, level = queue.pop(0)  # the next group, top to bottom
        tri = sum(1 for p in group if p[2] == "triangle")
        shape = "triangle" if 2 * tri >= len(group) else "square"
        scored = []
        if 0 < tri < len(group) and level < depth:
            for axis, name in ((0, "across"), (1, "up")):  # every question
                values = sorted({p[axis] for p in group})
                for a, b in zip(values, values[1:]):
                    cut = (a + b) / 2  # halfway between neighbours
                    below = [p for p in group if p[axis] < cut]
                    above = [p for p in group if p[axis] >= cut]
                    low, high = len(below) / len(group), len(above) / len(group)
                    gain = entropy(group) - low * entropy(below) - high * entropy(above)
                    scored.append((gain, name, cut, below, above))
        top = max((s[0] for s in scored), default=0)
        if top <= 1e-12:
            tree.append((shape,))  # one shape, the greatest depth, or no gain
            continue
        gain, name, cut, below, above = next(s for s in scored if abs(s[0] - top) < 1e-12)
        tree.append((name, cut))  # the question that gains most
        queue += [(below, level + 1), (above, level + 1)]
    return tree


def count(points, depth):
    """The same method on points, counting each point each question looks at."""
    looked, queue = 0, [(points, 0)]
    while queue:
        group, level = queue.pop(0)
        tri = sum(1 for p in group if p[2] == "triangle")
        if tri in (0, len(group)) or level == depth:
            continue  # a leaf weighs no question
        first = decision_tree(group, 1)  # the question this group asks
        questions = sum(len({p[axis] for p in group}) - 1 for axis in (0, 1))
        looked += questions * len(group)  # each question looks at every point
        if len(first) == 1:
            continue  # no question gains: a leaf
        name, cut = first[0]
        axis = 0 if name == "across" else 1
        queue += [([p for p in group if p[axis] < cut], level + 1), ([p for p in group if p[axis] >= cut], level + 1)]
    return looked


def cheapest(n):
    """One row of n points, 1 apart, the left half triangles: one question splits them."""
    return [(i, 5, "triangle" if i < n / 2 else "square") for i in range(n)], 3


def dearest(n):
    """The diagonal, a triangle then a square by turns: each question peels off one end point."""
    return [(i, i, "triangle" if i % 2 == 0 else "square") for i in range(n)], 3


if __name__ == "__main__":
    print(f"{'n':>5} {'two halves':>11} {'by turns':>10}")
    for n in (10, 20, 50, 100, 200, 500, 1000, 2000):
        print(f"{n:>5} {count(*cheapest(n)):>11} {count(*dearest(n)):>10}")

best, worst and average

Here best and worst only name two sets of points, both measured; no average is worked out, as there is no one way to pick points. The best column is the one row's cost, not the cheapest set: points packed in a square block need fewer looks, as the table shows. The diagonal by turns is the dearest set grown 3 deep. Why: each way, a group has at most one gap fewer than its points, so its questions are at most twice that. Setting one point apart leaves the biggest group, all but one point, to weigh next. One big group looks more than two smaller ones. At 8 points, groups of 7 and 1 look 2 × 7 × 6 = 84 times below the root. Groups of 4 and 4 look only 24 + 24 = 48.

A case is one kind of input, the thing the method is given, such as a set of points. A bound says how fast that case's cost grows as more points come in.

The count is in the grid. Every question looks at every point of its group. So the row looks about n times n times and the diagonal about 6 times that, and the three bounds all grow as n squared.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
bestone row of points, triangles on the left half and squares on the right, which one question splitsworstthe diagonal, a triangle then a square by turns, grown 3 deep, where each question peels off one end point
points looked atn(n−1)n(n - 1)6n2−18n+166n^2 - 18n + 16, from 2 points on; 1 point is a leaf, 0 looks
how the points looked at grow in each case
tight, Θ\ThetaΘ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)
ceiling, OOO(n2)O(n^2)O(n2)O(n^2)
floor, Ω\OmegaΩ(n2)\Omega(n^2)Ω(n2)\Omega(n^2)

extra memory: the tree, at most 1 + 2 + 4 + 8 = 15 groups at depth 3, and the groups' points, at most n at each depth

Extra memory is the room it needs on top of the input itself, while it runs.

02468100246810A4+4B4+0C0+4
02468100246810A4+4B1+0C3+4D0+1E3+3FG
16 pointsplaces acrossplaces upquestionslooked
one row in halves16115240
a 4 by 4 block in halves44696
The same 16 points two ways, each split by one question. The block stands at only 4 places each way, so it has fewer questions to weigh: 6 × 16 = 96 looks.

where the formula comes from

The two counts above, and the gain of a perfect question, each worked out in small steps naming the line it comes from.

Why does the diagonal by turns, grown 3 deep, look 6n2−18n+166n^2 - 18n + 16 times?
  1. the move: see the questions

    the line:
    m points, all apart:2(m−1) questions,each looks at m\begin{gathered} m \text{ points, all apart:} \\ 2(m - 1) \text{ questions,} \\ \text{each looks at } m \end{gathered}
    02468100246810A2+2B1+0C1+2D0+1E1+1FG

    why it is allowed: On the diagonal no two points share a place across or up. A group of mm points has m−1m - 1 gaps each way, so 2(m−1)2(m - 1) questions, and each looks at all mm points.

  2. the move: an end point peels off

    the line:
    m points→1 leaf+(m−1) points\begin{gathered} m \text{ points} \to \\ 1 \text{ leaf} + (m - 1) \text{ points} \end{gathered}
    pointsgain of each cut across, left to right
    30.252, 0.252
    40.311, 0, 0.311
    50.171, 0.020, 0.020, 0.171
    60.191, 0, 0.082, 0, 0.191
    Every cut's gain on the diagonal by turns. The two end cuts gain most, and up has the same cuts, so the first cut across is asked. The same holds at every size measured, 3 to 1,000.

    why it is allowed: On the diagonal by turns, the end cuts leave one point alone, of one shape, and gain most, as the table shows. A tie goes to the smaller cut. That point is a leaf and weighs nothing. What is left is again a diagonal by turns.

  3. the move: three groups weighed

    the line:
    C=2n(n−1)+2(n−1)(n−2)+2(n−2)(n−3)\begin{gathered} C = 2n(n - 1) \\ + 2(n - 1)(n - 2) \\ + 2(n - 2)(n - 3) \end{gathered}
    grouppointsquestionslooked
    the root4624
    after one end point leaves3412
    after another224
    The 4 points of section 5, grown 3 deep. Each question that gains most peels off one end point, a leaf of one shape. 24 + 12 + 4 = 40.

    why it is allowed: CC is the points looked at. By lines 1 and 2, the groups weighed hold nn, then n−1n - 1, then n−2n - 2 points. After 3 questions in a row the groups are leaves. At 3 points the third group holds 1 point and weighs nothing: 2 × 1 × 0 = 0. At 2 points the second group holds 1 point, 2 × 1 × 0 = 0, and the third holds none, 2 × 0 × (−1) = 0.

  4. the move: multiply out

    the line:
    C=(2n2−2n)+(2n2−6n+4)+(2n2−10n+12)\begin{gathered} C = (2n^2 - 2n) \\ + (2n^2 - 6n + 4) \\ + (2n^2 - 10n + 12) \end{gathered}

    why it is allowed: By line 3, each pair of brackets multiplied out.

  5. the move: add like terms

    the line:
    C=6n2−18n+16C = 6n^2 - 18n + 16

    why it is allowed: By line 4, the squares add to 6n26n^2, the nn terms to −18n-18n, and the numbers to 16. For 4 points, 40.

  6. the move: a ceiling

    the line:
    C≤6n2C \le 6n^2

    why it is allowed: By line 5, CC is 6n26n^2 with 18 times nn taken away and 16 put back. From 1 point on, more is taken away than put back.

  7. the move: read it as a bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 6, CC is at most 6 times n2n^2.

  8. the move: drop the later groups

    the line:
    C≥2n(n−1)C \ge 2n(n - 1)

    why it is allowed: By line 3, the other two terms are never below 0 from 2 points on, so CC is at least its first term.

  9. the move: a floor

    the line:
    C≥n2C \ge n^2
    points2n(n−1)2n(n - 1)n2n^2
    244
    3129
    42416
    54025
    The first term against n2n^2. They meet at 2 points, and the first term pulls ahead from there.

    why it is allowed: By line 8. From 2 points on, n−1n - 1 is at least half of nn; times 2n2n, line 8's term is at least n2n^2, as the table shows.

  10. the move: read it as a bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)

    why it is allowed: By line 9, CC never drops below 1 times n2n^2.

  11. the move: ceiling and floor together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 7 and 10 bound CC by the same growth, n2n^2.

try it with a small nn: at n=4n = 4, line 3 gives C=40C = 40, which you can work out by hand

Why does one row in two halves look n(n−1)n(n - 1) times?
  1. the move: one group weighed

    the line:
    B=n(n−1)B = n(n - 1)
    questionleft sideright sidelooks
    across below 0.51 triangle1 triangle, 2 squares4
    across below 1.52 triangles2 squares4
    across below 2.52 triangles, 1 square1 square4
    One row of 4 points, 2 triangles then 2 squares. Three questions, each looking at all 4: 12 looks. The middle one leaves two groups of one shape, so the tree stops.

    why it is allowed: BB is the points looked at. All nn points stand at one height, so up has no question, and across has n−1n - 1 gaps. Each question looks at all nn points. The cut between the halves leaves both sides one shape, so it gains the group's whole entropy, the most any question can. It is asked, and the tree stops.

  2. the move: multiply out

    the line:
    B=n2−nB = n^2 - n

    why it is allowed: By line 1. For 4 points, 12.

  3. the move: a ceiling

    the line:
    B≤n2B \le n^2

    why it is allowed: By line 2, BB is n2n^2 less nn.

  4. the move: read it as a bound

    the line:
    B∈O(n2)B \in O(n^2)

    why it is allowed: By line 3, BB is at most 1 times n2n^2.

  5. the move: a floor

    the line:
    B≥12n2B \ge \frac{1}{2} n^2

    why it is allowed: By line 2, BB is n2n^2 less nn, and from 2 points on nn is at most half of n2n^2.

  6. the move: read it as a bound

    the line:
    B∈Ω(n2)B \in \Omega(n^2)

    why it is allowed: By line 5, BB never drops below a half times n2n^2.

  7. the move: ceiling and floor together

    the line:
    B∈Θ(n2)B \in \Theta(n^2)

    why it is allowed: Lines 4 and 6 bound BB by the same growth, n2n^2.

try it with a small nn: at n=4n = 4, line 1 gives B=12B = 12, which you can work out by hand

Why does a question that splits nn triangles and nn squares into two groups of one shape gain exactly 1?
  1. the move: the gain, written out

    the line:
    G=n2n log⁡2 2nn+n2n log⁡2 2nn−(12×0+12×0)\begin{gathered} G = \frac{n}{2n} \,\log_2\, \frac{2n}{n} + \frac{n}{2n} \,\log_2\, \frac{2n}{n} \\ - \left(\frac{1}{2} \times 0 + \frac{1}{2} \times 0\right) \end{gathered}

    why it is allowed: Here nn is how many of each shape, so 2n2n points, as on the concept page. GG is the gain. The group's entropy comes first. Each side holds half the points, all of one shape, so each side's entropy is 0.

  2. the move: drop the zeros

    the line:
    G=n2n log⁡2 2nn+n2n log⁡2 2nnG = \frac{n}{2n} \,\log_2\, \frac{2n}{n} + \frac{n}{2n} \,\log_2\, \frac{2n}{n}

    why it is allowed: By line 1, both sides add nothing.

  3. the move: the group is half and half

    the line:
    G=1G = 1
    where does this come from?
    question 1 of the page information and entropyWhy is a group of nn triangles and nn squares exactly 1 mixed?
    1. the move: each share times the log of one over it

      the line:
      H=n2n log⁡2 2nn+n2n log⁡2 2nnH = \frac{n}{2n} \,\log_2\, \frac{2n}{n} + \frac{n}{2n} \,\log_2\, \frac{2n}{n}

      why it is allowed: HH is the entropy of nn triangles and nn squares. Each shape's share is nn of the 2n2n points, and one over it is 2n2n over nn.

    2. the move: cancel the n

      the line:
      H=12 log⁡2 2+12 log⁡2 2H = \frac{1}{2} \,\log_2\, 2 + \frac{1}{2} \,\log_2\, 2

      why it is allowed: By line 1, nn over 2n2n is a half, and 2n2n over nn is 2.

    3. the move: two halves make one

      the line:
      H=log⁡2 2H = \log_2\, 2

      why it is allowed: By line 2, half of a number added to half of it is the number.

    4. the move: read the log

      the line:
      H=1H = 1

      why it is allowed: By line 3, 2 is 2 to the power 1, so its log to base 2 is 1. For 4 triangles and 4 squares, 1.

    the whole page: information and entropy

    why it is allowed: Line 2 is the entropy of nn triangles and nn squares, which is exactly 1; the steps that show it open just below. For the row of 4 points in the ladder above, 2 of each, the middle question gains 1.

try it with a small nn: at n=2n = 2, line 1 gives G=1G = 1, which you can work out by hand

count it yourself

Count every step on four points on the diagonal, a triangle then a square by turns, grown 3 deep. In its table, the counts are running totals so far, the questions weighed and the points they looked at. A point such as 0 0 t is a triangle at 0 across, 0 up (s is a square). After the semicolon comes the greatest depth. Then check the looks against the formula.

0 0 t, 1 1 s, 2 2 t, 3 3 s; 3, every step written down
stepwhat happensquestions weighedpoints looked at
14 points of two shapes, and a tree that asks at most 3 questions on the way to any leaf. Every point starts in one group, A.00
2Group A: weigh 6 questions, each a cut across or up halfway between two of its points. 3 others gain as much, so the tie goes to across, then to the smaller cut.624
3Split group A by asking whether a point is across below 0.5. It gains 0.311. 1 point says yes and 3 say no, and each side is a new group below it.624
4Group B holds 1 triangle and 0 squares. Every point in it is one shape, so it is a leaf: a triangle.624
5Group C: weigh 4 questions, each a cut across or up halfway between two of its points. 3 others gain as much, so the tie goes to across, then to the smaller cut.1036
6Split group C by asking whether a point is across below 1.5. It gains 0.252. 1 point says yes and 2 say no, and each side is a new group below it.1036
7Group D holds 0 triangles and 1 square. Every point in it is one shape, so it is a leaf: a square.1036
8Group E: weigh 2 questions, each a cut across or up halfway between two of its points. Another gains as much, so the tie goes to across.1240
9Split group E by asking whether a point is across below 2.5. It gains 1. 1 point says yes and 1 says no, and each side is a new group below it.1240
10Group F holds 1 triangle and 0 squares. Every point in it is one shape, so it is a leaf: a triangle.1240
11Group G holds 0 triangles and 1 square. Every point in it is one shape, so it is a leaf: a square.1240
12The tree has 4 leaves. Each leaf's part of the plane is shaded in its shape, the shape a new point there would get.1240

In all, 40 points looked at. The formula from section 4, 6n2−18n+166n^2 - 18n + 16, gives C=40C = 40 at n=4n = 4: the same.

where you meet it

Decision trees are old, quick to read, and still offered by toolkits such as scikit-learn.

  • scikit-learn, a Python library for machine learning

    Its DecisionTreeClassifier tool grows a tree of yes-or-no questions on numbers, as this page does. It can weigh a question by entropy, as here. Unless told, it uses another measure of how mixed a group is. A greatest depth, max_depth, stops it growing.

    source: scikit-learn's documentation, the user guide's section on decision trees and the DecisionTreeClassifier reference

  • ID3

    In 1986 J. Ross Quinlan set out ID3, a program he first described in 1979. It grows a tree by asking at each group the question that gains most, its gain worked from entropy. Its questions asked about named values, such as a colour, red, green or blue, and could split a group in more than two.

    source: Quinlan, Induction of Decision Trees, Machine Learning, volume 1, 1986

  • CART

    In 1984 Leo Breiman, Jerome Friedman, Richard Olshen and Charles Stone described CART, whose trees ask only yes-or-no questions, as this page's do. They grew a tree large, then cut it back a step at a time. They chose among the cut-back trees by how well each did on points held aside, like this page's test points.

    source: Breiman, Friedman, Olshen and Stone, Classification and Regression Trees, 1984

greatest depthtraining points wrongtest points wrong
12 of 104 of 10
21 of 101 of 10
30 of 102 of 10
Section 1's tuned picture as a table. Here each step deeper fits the training points better; deeper never fits them worse. The test points do best at depth 2.
02468100246810A4+4B2+0C2+4D0+3E2+1FG

what is used instead

A tree's cuts are always across or up, so a boundary that runs on a slant takes many questions. k-nearest neighbours, on this site, needs no cuts: it looks at the points nearest each new point. It measures every point for each new point, where a grown tree asks only a few questions.

how it compares

A decision tree beside k-nearest neighbours on the same points. One plane, two ways of drawing its parts.

02468100246810A3+5B3+1C0+4D3+0E0+1
02468100246810
k-nearest neighbours with k = 3, each spot shaded by the vote of its 3 nearest points. Its edges are straight pieces at any slant, halfway between pairs of points; the drawing shades it in small squares half a unit wide, so its edges look stepped.

race them on one row of your own