最初に、完全な開示を提供するために、これは機械学習クラスの宿題に関連していることを指摘したいと思います。この質問は宿題に関する質問ではなく、代わりにID3決定木アルゴリズムを作成する大きな問題を完了するために把握する必要があるものです。助けを必要とする真理を与えられた2進木の作成表
私はlearnedTreeは次のように私が定義したどのタイプのBinaryTreeのあるlet learnedTree = Node(0,"A0", Node(2,"A2", Leaf(0), Leaf(1)), Node(1,"A1", Node(2,"A2", Leaf(0), Leaf(1)), Leaf(0)))
真理値表が与えられたときに、次のようなツリーを生成する必要が
:
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"
私が代わりにパターンマッチングを使用すべきか?任意のヒント/アイデア/助け?本当にありがとう!