首先,为了提供完整的信息,我想指出这与机器学习课程中的作业有关。这个问题不是作业问题,而是我需要弄清楚的,以便完成创建ID3决策树算法的更大问题。需要帮助创建一个给定真值的二叉树
我需要生成类似于树给定的真值表
let learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))
learnedTree是我定义如下类型二叉树的时候以下几点:
type BinaryTree =
| Leaf of int
| Node of int * string * BinaryTree * BinaryTree
ID3算法考虑到各种方程来确定在哪里拆分树,我已经搞清楚了,我只是无法从我的真值表中创建学习树。例如,如果我有以下表
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
我决定拆就属性A1我将结束与以下:
(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
然后我会分裂左侧和分裂右侧,并继续递归模式,直到叶节点是纯粹的,我最终得到一棵类似于以下的基于分裂的树。
let learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))
这里就是我这样的“黑客”一起迄今为止,但我想我可能是遥远:
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"
我应该使用模式,而不是匹配?任何提示/想法/帮助?谢谢一堆!