Options

How to Get Rules from a Decision Tree?

MartinLiebigMartinLiebig Administrator, Moderator, Employee, RapidMiner Certified Analyst, RapidMiner Certified Expert, University Professor Posts: 3,507 RM Data Scientist
edited November 2018 in Knowledge Base

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

 

 

 

- Sr. Director Data Solutions, Altair RapidMiner -
Dortmund, Germany
Tagged:

Comments

Sign In or Register to comment.