F# 函数构造二叉决策树

F# 函数构造二叉决策树,f#,F#,假设我有以下树结构: type Tree = | Branch of (string*string) * (Tree*Tree) | Leaf of float 例如,它可能看起来像这样: Branch (("X1",">4.5"), (Branch (("X2",">4.5"), (Leaf 3.4, Leaf 5.5)), Branch (("X3",">4.5"), (Leaf 6.5, Leaf 4.5)))) type DecisionTreeNode =

假设我有以下树结构:

type Tree =
  | Branch of (string*string) * (Tree*Tree)
  | Leaf of float
例如,它可能看起来像这样:

Branch (("X1",">4.5"), (Branch (("X2",">4.5"), (Leaf 3.4, Leaf 5.5)), Branch (("X3",">4.5"), (Leaf 6.5, Leaf 4.5))))
type DecisionTreeNode = 
  // Attribute name and value / child node list 
  | DecisionNode of string * (string * DecisionTreeNode) seq 
  // Decision and corresponding evidence 
  | Leaf of bool * Record seq
type Tree = 
  | Branch of string*((string*Tree)*(string*Tree))
  | Leaf of float
创建这样一个树(从数据、随机或其他方面)的函数的基本部分是什么? 我知道我的问题类似于,但我最难翻译到我的树上

编辑:我正在尝试建立一个决策树,我从如下树开始:

Branch (("X1",">4.5"), (Branch (("X2",">4.5"), (Leaf 3.4, Leaf 5.5)), Branch (("X3",">4.5"), (Leaf 6.5, Leaf 4.5))))
type DecisionTreeNode = 
  // Attribute name and value / child node list 
  | DecisionNode of string * (string * DecisionTreeNode) seq 
  // Decision and corresponding evidence 
  | Leaf of bool * Record seq
type Tree = 
  | Branch of string*((string*Tree)*(string*Tree))
  | Leaf of float
然而,我的是一个回归树,所以它应该有浮点叶,我只需要二进制分割,所以我想我可以使用tuple而不是seq作为节点。 再次看了那棵树之后,我想知道我的树是否应该是这样:

Branch (("X1",">4.5"), (Branch (("X2",">4.5"), (Leaf 3.4, Leaf 5.5)), Branch (("X3",">4.5"), (Leaf 6.5, Leaf 4.5))))
type DecisionTreeNode = 
  // Attribute name and value / child node list 
  | DecisionNode of string * (string * DecisionTreeNode) seq 
  // Decision and corresponding evidence 
  | Leaf of bool * Record seq
type Tree = 
  | Branch of string*((string*Tree)*(string*Tree))
  | Leaf of float
我没有工作过,但在阅读了你的要求之后

  • 二元分裂
  • 浮叶
  • 查看中的示例,并查看一些使用谷歌搜索的图像示例

    我将使用:

    类型树=
    |字符串的分支*(字符串*树)*(字符串*树)
    |浮叶

    并使用

    将节点与
    |分支(决策,(v1,l)、(v2,r))->//做点什么
    |叶值->//做点什么

    您可以将值与
    v1
    v2
    进行比较,然后选择适当的分支
    l
    r

    注意:我删除了
    ((字符串*树)*(字符串*树))
    周围的
    ),以便您可以使用

    分支(决策,(v1,l)、(v2,r))
    而不是
    分支(决策,((v1,l)、(v2,r)))


    还要注意的是,我还没有测试或编译这段代码,但它应该可以让您开始使用。

    我明白了我最初的目标。类似这样的函数(将“while i>0”逻辑替换为从中创建树的内容),它基于@GuyCoder的答案中给出的树结构:

    type Tree =
    | Branch of string*(string*Tree)*(string*Tree)
    | Leaf of float
    
    let rec buildTree i =
        if i<1 then Leaf 1.
        else Branch ("Branch", ("Condition1", (buildTree (i-1))), ("Condition1", (buildTree (i-1))))
    
    类型树=
    |字符串的分支*(字符串*树)*(字符串*树)
    |浮叶
    让rec构建一棵树=
    
    如果我想知道你粘在哪个部位?因此,我不是来写我所有的代码问题的。我在某种程度上澄清了这个问题。这让我走上了正确的方向,但我真正想要的是在我的答案中给出的。这是正确的处理方法吗?我希望得到公平的分数。