首先,为了充分披露,我想指出这与机器学习课程中的作业有关。这个问题不是家庭作业问题,而是我需要解决的问题,以便完成创建 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)))
learnTree 是 BinaryTree 类型,我定义如下:
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"
我应该使用模式匹配吗?有什么建议/想法/帮助吗?非常感谢!