Decision Tree

AI 101

Trees and Keys

We return to the question of film with:

  1. More understanding of Colab, and…
  2. More understanding of classification.

What is a Decision tree?

  • A decision tree is a flowchart-like structure in which each internal step represents a feature, and each end point node represents a label.
  • The paths from root to leaf represent classification rules.
  • A dichotomous key is a decision tree that can be used for classification.
    • In our case with oaks, a “tree tree” - or an “oak decision tree”.

The Key

  • Recall, a key is a branching series of yes/no questions.
    • The dichotomous key gets its name from having two (“di”) possibilities for any case
    • “yes” and “no” or “true” and “false” or etc. etc.
  • To classify an oak, there are a series of questions that a non-expert can answer by inspecting the tree, such as being looking at the whole tree or looking at its leaves.
  • After answer a question, there is either a reference to a new question, usually by number, or a note that you have determined what the type of tree is.
Features

We now recognize these “yes”/“no” questions as features.

We recall:

In machine learning and pattern recognition, a feature is an individual measurable property or characteristic of a data set.

  • You may have to zoom in…
    • If you turn it upside down, it somewhat looks like a tree (hence the name).

OakKey Start Leaves are smooth with no teeth or lobes? Evergreen Laves evergreen? Start->Evergreen True Bristles Lobes/teeth bristle-tipped? Start->Bristles False GrowthHabit Large Tree (not shrub)? Evergreen->GrowthHabit True LeafShape Leaves more than 3x long as wide Evergreen->LeafShape False LiveOak Southern live oak GrowthHabit->LiveOak True DwarfOak Dwarf live oak GrowthHabit->DwarfOak False WillowOak Willow oak LeafShape->WillowOak True ShingleOak Shingle oak LeafShape->ShingleOak False LobeCount6 3 or fewer lobes? Bristles->LobeCount6 True LobeCount7 9 or fewer lobes? Bristles->LobeCount7 False BlackjackOak Blackjack oak LobeCount6->BlackjackOak True RedOak Northern red oak LobeCount6->RedOak False WhiteOak White oak LobeCount7->WhiteOak True SwampOak Swamp chestnut oak LobeCount7->SwampOak False

Our Films

  • We return to the films from the classification lecture.
  • We will not interact with Gemini today…
    • Rather, create a “program” of sorts that will ask us questions and determine a film.
  • Create a script which will ask you a series of yes/no questions about a film and then eventually tell you which film you are thinking off.
  • While you may use multiple cells, your ultimate result should be a single code cell (like below).

Simple Example

  • Try this out.
instructions = "Please reply exactly yes or no, three or two letters, all lower case, and nothing else. "

if "yes" == input(instructions + "Does the film feature clones?"):
  if "yes" == input(instructions + "Does the film feature Darth Vader?"):
    print("III")
  else:
    print("II")
else:
  print("I")

Easy Mode

  • Create a simple program to differentiate the three most recent Alien films.
    • Prometheus dir. Ridley Scott
    • Alien: Covenant dir. Ridley Scott
    • Alien: Romulus dir. Fede Álvarez

Medium Mode

  • Create a simple program to differentiate the original Alien vs. Predator films.
    • The first four Alien films, Alien, Aliens, Alien 3, and Alien: Resurrection
    • The first two Predator films, Predator, and Predator 2
    • The two crossover films, Alien vs. Predator and *AVPR: Aliens vs Predator - Requiem”

Hard Mode

  • Create a simple program to differentiate all Alien and Predator films covered in lecture.

Choose your own adventure

  • You may, of course, consider other conceptual frameworks:
    • Science fiction settings such as le Guin’s Earthsea, Asimov’s Foundation and Robot crossovers, Herbert’s Dune
    • Cinematic universes such as Marvel
  • Alien vs. Predator is an unusually high profile crossover, but I’m sure there are others of which I’m not aware.

Requirements

You should use the following:

Plus/Equals

  • I found it very helpful in this lab to add many helpful tidbits together.
    • “If you reply using capital letters, my program will break!”
  • I used = to give short nicknames to longer messages.
  • I used + to combine them - usually before asking a question from the key.
  • Here is an example:
instructions = "Please reply exactly yes or no, three or two letters, all lower case, and nothing else. "

print(instructions + "Does the film feature clones?")

If/else Syntax

  • Like while which we learned in the “Colab” lab, we can use if and else to ask questions depending on some condition.
    • For example, we can ask if leaves are “smooth” or “lobed”.
    • if leaves “smooth”, we can then ask if the leaves are evergreen.
    • else we can ask if the lobes (or teeth) are bristle-tipped.
    • Far more easily, we ask yes or no questions.
  • Here is an example:
if "yes" == input("Does the film feature Anakin Skywalker?"):
    print("Prequel Trilogy")
else:
    print("It's complicated")
Double equals? What’s that!

In Colab, both = and == mean something.

  1. I call = “single equals assignment”. It assigns a nickname to something longer or more complicated. It gets its own line.
  2. I call == “double equals equality”. It checks to see if two things are equal, usually with an if.
if 2 + 2 == 4:
    print("Hi!")

Nesting

  • It is possible to place one if statement inside another with indentation.
    • Term this “nesting”.
if "yes" == input("Does the film feature clones?"):
  if "yes" == input("Does the film feature Darth Vader?"):
    print("III")
  else:
    print("II")
else:
  print("I")

Your Task

  • No write-up today, just try to get this working.