Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement decision tree with c# (visual studio 2008) - Help

I have a decision tree that i need to turn to a code in C#

The simple way of doing it is using if-else statements but in this solution i will need to create 4-5 nested conditions.

I am looking for a better way to do it and so far i read a little bit about rule engines.

Do you have something else to suggest for an efficient way to develop decision tree with 4-5 nested conditions?

like image 667
Chen Avatar asked Oct 08 '10 09:10

Chen


People also ask

What are the 3 types of nodes used in the decision trees?

There are three different types of nodes: chance nodes, decision nodes, and end nodes. A chance node, represented by a circle, shows the probabilities of certain results. A decision node, represented by a square, shows a decision to be made, and an end node shows the final outcome of a decision path.

How do we train decision trees?

Decision Tree models are created using 2 steps: Induction and Pruning. Induction is where we actually build the tree i.e set all of the hierarchical decision boundaries based on our data. Because of the nature of training decision trees they can be prone to major overfitting.

What software is used for decision trees?

To see more examples or use software to build your own decision tree, check out some of these resources: IBM SPSS Decision Trees. LucidChart Decision Tree Software. Zingtree Interactive Decision Tree Template.


1 Answers

I implemented a simple decision tree as a sample in my book. The code is available online here, so perhaps you could use it as an inspiration. A decision is essentially represented as a class that has references to true branch and false branch and contains a function that does the test:

class DecisionQuery : Decision {
  public Decision Positive { get; set; }
  public Decision Negative { get; set; }
  // Primitive operation to be provided by the user
  public Func<Client, bool> Test { get; set; }

  public override bool Evaluate(Client client) {
    // Test a client using the primitive operation
    bool res = Test(client);
    // Select a branch to follow
    return res ? Positive.Evaluate(client) : Negative.Evaluate(client);
  }
}

Here, Decision is a base class that contains Evaluate method and the source contains one additional derived type that contains a final decision of the tree (yes/no). The type Client is a sample input data that you're analysing using the tree.

To create a decision tree, you can write something like:

var tree = new DecisionQuery {
    Test = (client) => client.Income > 40000,
    Positive = otherTree,
    Negative = someOtherTree
  };

If you just want to write five nested static if clauses then maybe just writing if is fine. The benefit of using a type like this one is that you can easily compose trees - e.g. reuse a part of a tree or modularize the construction.

like image 142
Tomas Petricek Avatar answered Sep 25 '22 20:09

Tomas Petricek