Docsity

Programming Assignment 4: Decision Trees in Lisp or Prolog - Prof. Mary Califf, Assignments of Information Technology

Toggle sidebar
bg1
Programming Assignment 4:
Decision Trees
Due date: Friday, April 16, 2004 – beginning of class
Description:
For this assignment, you will implement a simple version of the ID3 decision tree
algorithm. This is the basic algorithm discussed in your book, using information gain as
the splitting criteria. You may write this program in either Lisp or Prolog: your choice.
Your code will need to do several things:
1) Produce a decision tree from a list of examples
2) Given a decision tree and an instance (an unclassified example), classify the
instance
3) Print out a decision tree
Format:
To handle a set of examples, you’ll need two pieces of information: an idea of what the
examples look like and the examples themselves. To provide the first piece of
information, you'll use two lists: a list of the attributes and a list of the domains of each
attribute. The examples will be a list of examples. Each example will be a two-item list:
the first item will be either + or – and the second a list of the values for each attribute. In
Lisp, you’ll use variables to hold this information. In Prolog, you can use a predicate to
define the attribute and domain lists (similar to the way action, initial, and goal were used
in Program 3).
Lisp:
In your program file, declare your domain and attribute variables:
(defvar *attributes*) ; A list defining the features of the instances
(defvar *domains*) ; A list defining the domain of each feature
To use the program you’ll do something like the following:
(setf *attributes* '(outlook temperature humidity windy))
(setf *domains* '((sunny overcast rain)
(cool mild hot)
(high normal)
(true false)))
(setf weather-examples '((- (sunny hot high false))(- (sunny hot high true))
(+ (overcast hot high false))(+ (rain mild high false))
(+ (rain cool normal false)) (- (rain cool normal true))
(+ (overcast cool normal true))(- (sunny mild high false))
(+ (sunny cool normal false))(+ (rain mild normal false))
(+ (sunny mild normal true))(+ (overcast mild high true))
(+ (overcast hot normal false))(- (rain mild high true))))
pf3

Partial preview of the text

Download Programming Assignment 4: Decision Trees in Lisp or Prolog - Prof. Mary Califf and more Assignments Information Technology in PDF only on Docsity!

Programming Assignment 4:

Decision Trees

Due date : Friday, April 16, 2004 – beginning of class Description : For this assignment, you will implement a simple version of the ID3 decision tree algorithm. This is the basic algorithm discussed in your book, using information gain as the splitting criteria. You may write this program in either Lisp or Prolog: your choice. Your code will need to do several things:

  1. Produce a decision tree from a list of examples
  2. Given a decision tree and an instance (an unclassified example), classify the instance
  3. Print out a decision tree Format: To handle a set of examples, you’ll need two pieces of information: an idea of what the examples look like and the examples themselves. To provide the first piece of information, you'll use two lists: a list of the attributes and a list of the domains of each attribute. The examples will be a list of examples. Each example will be a two-item list: the first item will be either + or – and the second a list of the values for each attribute. In Lisp, you’ll use variables to hold this information. In Prolog, you can use a predicate to define the attribute and domain lists (similar to the way action, initial, and goal were used in Program 3). Lisp: In your program file, declare your domain and attribute variables: (defvar attributes) ; A list defining the features of the instances (defvar domains) ; A list defining the domain of each feature To use the program you’ll do something like the following: (setf attributes '(outlook temperature humidity windy)) (setf domains '((sunny overcast rain) (cool mild hot) (high normal) (true false))) (setf weather-examples '((- (sunny hot high false))(- (sunny hot high true)) (+ (overcast hot high false))(+ (rain mild high false)) (+ (rain cool normal false)) (- (rain cool normal true)) (+ (overcast cool normal true))(- (sunny mild high false)) (+ (sunny cool normal false))(+ (rain mild normal false)) (+ (sunny mild normal true))(+ (overcast mild high true)) (+ (overcast hot normal false))(- (rain mild high true))))

Prolog: To use the program you’ll do something like the following: attributes([outlook, temperature, humidity, windy]). domains([[sunny, overcast, rain], [cool, mild, hot], [high, normal], [true, false]]). A list of examples would look like: [['-', [sunny, hot, high, false]], ['-', [sunny, hot, high, true]], ['+', [overcast, hot, high, false]], [ '+', [rain, mild, high, false]], ['+', [rain, cool, normal, false]], ['-', [rain, cool, normal, true]], ['+', [overcast, cool, normal, true]], [ '-', [sunny, mild, high, false]], ['+', [sunny, cool, normal, false]], ['+', [rain, mild, normal, false]], ['+', [sunny, mild, normal, true]], ['+', [overcast, mild, high, true]], ['+', [overcast, hot, normal, false]] ['-', [rain, mild, high, true]]] What the tree should look like printed out: If you really want to, you can print the tree in a standard tree format, but a simpler suggestion would be something like the following: OUTLOOK = SUNNY HUMIDITY = NORMAL => YES = HIGH => NO = OVERCAST => YES = RAIN WIND = STRONG => NO = WEAK => YES Notes: The basic function or predicate to construct a decision tree is recursive. The base case will produce a leaf node. You should write a predicate to compute the entropy of a set of examples given the number of positive examples and number of negative examples. Both sicstus and clisp provide a logarithm. In both languages, you probably want to use format to print. Both languages provide ways to get the length of a list and to get a particular member of a list. In sicstus, that will require the lists library (which was used in program 3). You might want to start by thinking about what a tree is going to look like. In lisp, you would probably use a list-based representation. In prolog you could use lists or structures and lists.