Professional Documents
Culture Documents
AiML 4 4 SG
AiML 4 4 SG
2
Objectives
• This lesson covers the following objectives:
−Understand non-binary tree structure
−Create a non-binary tree structure
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 3
3
Trees Revisited
• To write an algorithm for ID3, the decision tree will be
stored in a tree structure
• It is not a binary tree, since a node may have 2 or more
child nodes
• The first step is to create a structure capable of
representing a tree
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 4
4
Example Tree
2 3 4
5 6 7
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 5
5
Task – non-binary Tree
• Create a class called Node that can represent a multi-
child tree
• Data will be stored as a string
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 6
6
Possible Solution
public class Node {
private List<Node> children = new ArrayList<Node>();
private Node parent;
private String data;
public Node() {
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 7
7
Methods
• Create the following methods for interacting with the
Node class
Method Signature Description
void setData(String data) Set the data value of the current node
String getData() Get the data value of the current node
List<Node> getChildren() Return an ArrayList of nodes containing all of
the children under a node
void addChild(Node node) Add a child node to the current node
void removeChildren() Remove all children from a node
public Node getParent() Returns the value of the parent node
void print(String prefix, Print all the nodes from the current node
boolean isTail)
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 8
8
Possible Solution
public void setData(String data) {
this.data = data;
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 9
9
Possible Solution
public void addChild(Node node) {
node.parent = this;
this.children.add(node);
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 10
10
Possible Solution
public void print(String prefix, boolean isTail) {
System.out.println(prefix + (isTail ? “\\-- " : “|-- ") + data);
for (int i = 0; i < children.size() - 1; i++) {
children.get(i).print(prefix + (isTail ? " " : “| "), false);
}
if (children.size() > 0) {
children.get(children.size() - 1)
.print(prefix + (isTail ?" " : “| "), true);
}
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 11
11
Tree Class
• Like the binary tree, create a tree class to hold the root
and provide other helper methods
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 12
12
Tree Class
public class Tree {
private Node root;
public Node getRoot() {
return this.root;
}
public void setRoot(Node root) {
this.root = root;
}
public boolean isEmpty() {
return (root == null);
}
public void print() {
root.print("",true);
}}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 13
13
Tree Structure
• The classes are created
• The root of the decision tree (as created in our worked
example) looked like this:
Outlook
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 14
14
Tree Structure
• There are a variety of ways to represent the decision
tree
• Create a solution that stores the leaves and the
branches in nodes
• Differentiate between the nodes by having a Node type
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 15
15
Tree Structure
• It is possible to use other approaches
• For example, rather than having an ArrayList to store
children, use a map collection to store both the name
of the link and the resulting attribute
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 16
16
Tree Structure
• It is possible to create any structure that best fits your
solution
• The solution below uses a Node for a leaf and a branch
• An enum can classify the different node types
public enum NodeType {
ROOTNODE,
LEAFNODE,
BRANCH
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 17
17
Tree Structure
• Modify the node class to store this type
public class Node {
private List<Node> children = new ArrayList<Node>();
private Node parent;
private String data;
private NodeType type;
public Node() {
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 18
18
Tree Structure
• This will allows the tree to be populated with a
solution as it is built
• As built, the branches and leaves will be displayed
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 19
19
Tree Structure
└── Outlook
├── Rain
│ └── Wind
│ ├── Weak
│ │ └── Yes
│ └── Strong
│ └── No
├── Overcast
│ └── Yes
└── Sunny
└── Humidity
├── High
│ └── No
└── Normal
└── Yes
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 20
20
Final Task
• The final task is to implement a solution to the ID3
algorithm
• There are many ways to do this!
• Assume that all files are in CSV format and the last
column is always the outcome
• All data will be classified and there will be no missing
values
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 21
21
Final Task
• A number of CSV files have been supplied to test your
algorithm
• Store the data in a table-like format of an ArrayList or
ArrayList of Strings
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 22
22
Tree Structure
• Example:
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 23
23
Load CSV
• A simple routine to load the file:
public List<ArrayList<String>> loadCSV(String filepath) throws
FileNotFoundException {
List<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
Scanner scan = new Scanner(new File(filepath));
while (scan.hasNextLine()) {
String line = scan.nextLine();
ArrayList<String> lineArrayList = new
ArrayList<String>(Arrays.asList(line.split(",")));
data.add(lineArrayList);
}
return data;
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 24
24
Display Table
• A routine to display a table of the data:
static public void printArrayList(List<ArrayList<String>> data) {
//record that largest string length in each column for formatting
int colnum = 0;
List<Integer> maxcolwidths = new ArrayList<Integer>();
for(ArrayList<String> row : data) {
for (String item : row) {
……<Continued next>
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 25
25
Display Table
• Use this to display the data when loaded, and when
the table is down during recursive calls (will monitor
progress)
colnum = 0;
for(ArrayList<String> row : data) {
for (String item : row) {
String format = "| %-" + maxcolwidths.get(colnum) + "s";
System.out.printf(format,item);
colnum++;
}
colnum = 0;
System.out.println();
}
}
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 26
26
Task
• Create a solution for ID3
• There are a number of CSV files, including the outdoor
sport that you can use to test your algorithm
• You do not have to use any of the methods shown
above or even the Node class, but it would be useful as
a starting point
• You can choose any way you wish to generate a
solution
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 27
27
Summary
• In this lesson, you should have learned how to:
−Understand non-binary tree structure
−Create a non-binary tree structure
AiML 4-4
Create an ID3 Tree Copyright © 2020, Oracle and/or its affiliates. All rights reserved. 28
28
29