First, to ensure full disclosure, I want to point out that this is related to homework in a machine learning class. This question is not a homework question, and instead I need to find out to complete the big problem of creating an ID3 decision tree algorithm.
I need to generate a tree like the one below when defining a truth table
let learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))
learnTree is of type BinaryTree, which I defined as follows:
type BinaryTree = | Leaf of int | Node of int * string * BinaryTree * BinaryTree
ID3 algorithms take into account various equations to determine where to divide the tree, and I did it, I just had a problem creating the learned tree from my truth table. For example, if I have the following table
A1 | A2 | A3 | Class 1 0 0 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 1 0 1 0 1 1 0
And I decided to divide by attribute A1, I would get the following:
(A1 = 1) A1 (A1 = 0) A2 | A3 | Class A2 | A3 | Class 0 0 1 1 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 1 1
Then I would split the left side and split the right side and continue the recursive pattern until the leaf nodes are clean and I end up with a tree similar to the next based on splitting.
let learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))
Here is what I have so far โcrackedโ together, but I think I could be aloof:
let rec createTree (listToSplit : list<list<float>>) index = let leftSideSplit = listToSplit |> List.choose (fun x -> if x.Item(index) = 1. then Some(x) else None) let rightSideSplit = listToSplit |> List.choose (fun x -> if x.Item(index) = 0. then Some(x) else None) if leftSideSplit.Length > 0 then let pureCheck = isListPure leftSideSplit if pureCheck = 0 then printfn "%s" "Pure left node class 0" createTree leftSideSplit (index + 1) else if pureCheck = 1 then printfn "%s" "Pure left node class 1" createTree leftSideSplit (index + 1) else printfn "%s - %A" "Recursing Left" leftSideSplit createTree leftSideSplit (index + 1) else printfn "%s" "Pure left node class 0"
Should I use pattern matching? Any tips / ideas / help? Thanks a bunch!