Decision trees

Published

2023-08-01

Decision trees

We may represent a decision-making process diagrammatically with a decision tree. Decision trees are commonly used for species identification.1

Here’s an example:

Decision tree for fruit
Figure 1: Decision tree for fruit

Here, we start on the left and move toward the right, making decisions along the way. Notice that at each branching point we have two branches.

So, for example, to reach “watermelon, cantaloupe”, we make the decisions: soft inside, small seeds, thick skin, and unsegmented. To reach “peach”, we’d have to have made the decisions: soft inside, pit or stone, and fuzzy.

How do we encode these decision points? One way is to treat them as yes or no questions. So if we ask “Is the fruit soft inside?” then we have a yes or no answer. If the answer is “no”, then we know the fruit is not soft inside and thus must be hard inside (like a walnut or almond).

Here’s a snippet of Python code, demonstrating a single question:

response = input('Is the fruit soft inside? y/n  ')
if response == 'y':
    # we know the fruit is soft inside
    # ...
else:
    # we know the fruit is hard inside
    # ...

We can write a program that implements this decision tree by using multiple, nested if statements.

"""
CS 1210
Decision tree for fruit identification
"""

response = input('Is the fruit soft inside? y/n  ')
if response.lower() == 'y':
    # soft inside
    response = input('Does it have small seeds? y/n  ')
    if response.lower() == 'y':
        # small seeds
        response = input('Does it have a thin skin? y/n  ')
        if response.lower() == 'y':
            # thin skin
            print("Tomato")
        else:
            # thick skin
            response = input('Is it segmented? y/n  ')
            if response.lower() == 'y':
                # segmented
                print("Orange or lemon")
            else:
                # unsegmented
                print("Watermelon or cantaloupe")
    else:
        # pit or stone
        response = input('Is it fuzzy? y/n  ')
        if response.lower() == 'y':
            # segmented
            print("Peach")
        else:
            # unsegmented
            print("Plum")
else:
    # hard inside
    print("Walnut or almond")

Comprehension check

  1. In the decision tree above, Figure 1, which decisions lead to plum? (There are three.)

  2. Revisit ?@sec-if_elif_else and draw decision trees for the code examples shown.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. If you’ve had a course in biology, you may have heard of a cladogram for representing taxonomic relationships of organisms. A cladogram is a kind of decision tree. If you’re curious, see: https://en.wikipedia.org/wiki/Cladogram and similar applications.↩︎