Reputation: 89
I'm learning how to implement simple decision tree in C#. Can somebody explain me, how it looks like in pseudocode or have some easy tutorial for implementing in c#?
I have this dataset:
(from here: 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
Upvotes: 1
Views: 3679
Reputation: 62
If you are building an decision tree based on ID3 algorithm, you can reference this pseudo code.
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
Upvotes: 2
Reputation: 17595
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.
Upvotes: 2