简体   繁体   English

实现决策树

[英]Implementing decision tree

I'm learning how to implement simple decision tree in C#.我正在学习如何在 C# 中实现简单的决策树。 Can somebody explain me, how it looks like in pseudocode or have some easy tutorial for implementing in c#?有人可以解释一下,它在伪代码中的样子,或者有一些在 c# 中实现的简单教程吗?

I have this dataset:我有这个数据集:

在此处输入图片说明

(from here: http://storm.cis.fordham.edu/~gweiss/data-mining/weka-data/weather.nominal.arff ) (来自: http : //storm.cis.fordham.edu/~gweiss/data-mining/weka-data/weather.nominal.arff

and I've done graphical decision tree我已经完成了图形决策树在此处输入图片说明

(Sorry for my english) (对不起我的英语不好)


My idea is only this:我的想法只有这样:

if outlook = "overcast" then no 
if outlook = "sunny" and humidity = "normal" then yes
if outlook = "sunny" and humidity = "high" then no
if outlook = "rain" and wind = "true" then no
if outlook = "rain" and wind = "fasle" then yes

I really don't know, how to continue我真的不知道,如何继续

To answer the question in part, apparently the concept of a decision tree is described here .为了部分回答这个问题,显然这里描述了决策树的概念。 To implement a decision tree for the type above, you could declare a class matching the type from the table in your question.要为上述类型实现决策树,您可以声明一个与问题中表中的类型匹配的类。 Based on that type, you need to create a tree data structure in which the number of children is not limited.基于该类型,您需要创建一个树数据结构,其中子节点的数量不受限制。 While the actual data is contained only in the leaves, it would be best to have each member of the basic type defined as nullable.虽然实际数据仅包含在叶子中,但最好将基本类型的每个成员定义为可为空。 That way, in each node you could set only the members which are set to a specific value of its children.这样,在每个节点中,您只能设置被设置为其子节点的特定值的成员。 In addition, then number of nodes with values no and yes should be represented.此外,还应表示值为noyes的节点数。

If you are building an decision tree based on ID3 algorithm, you can reference this pseudo code.如果是基于ID3算法构建决策树,可以参考这个伪代码。

ID3 (Examples, Target_Attribute, Attributes)
Create a root node for the tree
If all examples are positive, Return the single-node tree Root, with label = +.
If all examples are negative, Return the single-node tree Root, with label = -.
If number of predicting attributes is empty, then Return the single node tree Root,
with label = most common value of the target attribute in the examples.
Otherwise Begin
    A ← The Attribute that best classifies examples.
    Decision Tree attribute for Root = A.
    For each possible value, vi, of A,
        Add a new tree branch below Root, corresponding to the test A = vi.
        Let Examples(vi) be the subset of examples that have the value vi for A
        If Examples(vi) is empty
            Then below this new branch add a leaf node with label = most common target value in the examples
        Else below this new branch add the subtree ID3 (Examples(vi), Target_Attribute, Attributes – {A})
End
Return Root

If you like to learn more about ID3 algorithm, please go to the link ID3 algorithm如果您想了解有关 ID3 算法的更多信息,请转到链接ID3 算法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM