Menu
Coddy logo textTech

Decision Tree

Lesson 17 of 19 in Coddy's Introduction to Machine Learning course.

A Decision Tree is a non-linear model used for both classification and regression tasks. It mirrors human decision-making more closely than other algorithms, making it both intuitive and powerful for handling complex datasets. At its core, a decision tree splits the data into subsets using a series of simple rules, which is akin to asking a series of yes/no questions about the features of the data points.
 

Source: wikipedia

Visualize a decision tree as a tree-like graph, where:

  • Each internal node represents a "test" on an attribute (e.g., whether a coin flip comes up heads or tails), 
  • Each branch represents the outcome of the test, and
  • Each leaf node represents a class label (decision taken after computing all attributes).

Types of Decision Trees:

  • Classification Trees: Used when the predicted outcome is the class to which the data belongs.
  • Regression Trees: Used when the predicted outcome can be considered a real number (e.g., the price of a house).


 

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Medium

Create a function named decide_outcome that receives a list of rules in the following format:

rules = {
	rule1: {"type": "bigger", "value": 5},
	rule2: {"type": "smaller", "value": 7},

}

And it receives to which rule to proceed if the outcome is met or not:

outcome = {
	"rule1": {True: "rule2", False: "rule3"},
	"rule2": {True: "rule3", False: "rule4"},
	"rule3": {True: None, False: None}
	"rule4": {True: None, False: None}

}
data_point = {
	"rule1": 8,
	"rule2": 10,
}

The output should be rule4

  • 8 is bigger than 5 (True) → go to rule2 from this "rule1": {True: "rule2", False: "rule3"},
  • 10 is smaller than 7 (False) → go to rule4 from this "rule2": {True: "rule3", False: "rule4"},
  • There are no other ways to continue so the output is rule4

Always start from <strong>rule1</strong>

Try it yourself

def decide_outcome(rules, outcome, point):
    current_rule = "rule1"
    # Write your code here

All lessons in Introduction to Machine Learning