RapidMiner

How to Get Rules from a Decision Tree?

by Moderator on ‎10-21-2016 09:53 AM - edited on ‎11-03-2016 07:35 AM by Community Manager

Question

Decision Trees are well known for their understandability. How can I get the rules a Decision Tree provides in a more handy format?

Answer

To convert Decision Trees into Rules you can use the Tree to Rule operator. The Tree to Rule operator is a nested operator, means you can put the Decision Tree inside.

The Decision Tree is parsed into a rule format which is easier to understand

 

RuleModel.png

 

 

This Rule Model can be parsed into a example set with the following Groovy script. The Groovy script needs to be used in an Execute Script operator. An example is attached as a process.

 

import com.rapidminer.operator.learner.rules.RuleModel;
import com.rapidminer.tools.Ontology;
import com.rapidminer.tools.LogService;
import com.rapidminer.operator.learner.rules.Rule;

import java.util.logging.Level
RuleModel ruleModel = input[0]
numberOfAttributes = 4;

Attribute[] attributes= new Attribute[numberOfAttributes];
attributes[0] = AttributeFactory.createAttribute("Full Rule", Ontology.STRING);
attributes[1] = AttributeFactory.createAttribute("Label", Ontology.STRING);
attributes[2] = AttributeFactory.createAttribute("Correct Examples covered by this rule", Ontology.STRING);
attributes[3] = AttributeFactory.createAttribute("Wrong Examples covered by this rule", Ontology.STRING);
MemoryExampleTable table = new MemoryExampleTable(attributes);
DataRowFactory ROW_FACTORY = new DataRowFactory(0);

String[] myvalues = new String[numberOfAttributes]

for(Rule currentRule : ruleModel.getRules()){
    int correct = 0;
    int wrong = 0;
    int label = ruleModel.getLabel().getMapping().getIndex(currentRule.getLabel());
    LogService.root.log(Level.INFO, currentRule.toString())
    int[] frequencies = currentRule.getFrequencies();
    if (frequencies != null) {
        for (int i = 0; i < frequencies.length; i++) {
            if (i == label) {
                correct += frequencies[i];
            } else {
                wrong += frequencies[i];
          }
      }
      myvalues[0] = currentRule.toString()
      myvalues[1] = currentRule.getLabel()
      myvalues[2] = String.valueOf(correct);
      myvalues[3] = String.valueOf(wrong);

      DataRow row = ROW_FACTORY.create(myvalues, attributes)
      table.addDataRow(row);
    }
}

return table.createExampleSet();

The result looks like the screen shot below:

 

Example Set from Tree.png

 

 

 

Comments
fatinharun94
Contributor

 How to predict using Support Vector Machine? I am new here. 

RMStaff
RMStaff
fatinharun94
Contributor

Thank you for the link. I just got the solution.

 This is what i understand from the link. Is this correct?This is what i understand from the link. Is this correct?