Save and Load DecisionTree to XML

This example shows how to save decision trees, represented as hierarchical structures, to XML format. It allows you to store decision trees in a structured format, capturing the conditions and outcomes at each node of the tree. Similarly, the example supports loading decision trees from XML files, enabling the reuse and sharing of decision tree models across different systems or applications.

Decision Tree is a hierarchical structure that represents a series of conditions and outcomes. It is used to evaluate input data by following the branches of the tree, checking conditions at each node, and ultimately reaching a final outcome based on the satisfied conditions.

 

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.decisiontree;

import java.io.FileInputStream;
import java.io.FileWriter;

import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTree;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTreeNode;

public class SaveAndLoadDecisionTreeToXML {

    public static void main(String[] args) throws Throwable {

        DecisionTree tree = new DecisionTree()

            .addField("ageThreshold", "40")
            .addField("overAgeThreshold", "Age >= ageThreshold")

            .setRootNode(new DecisionTreeNode()

                .addNode(new DecisionTreeNode("overAgeThreshold == true")
                    .addNode(new DecisionTreeNode("houseOwned == true").addOutcome("Eligible", "true"))

                    .addNode(new DecisionTreeNode().setCondition("houseOwned == false")
                        .addNode(new DecisionTreeNode("Income >= 2000").addOutcome("Eligible", "true"))
                        .addNode(new DecisionTreeNode("Income < 2000").addOutcome("Eligible", "false"))))

                .addNode(new DecisionTreeNode().setCondition("overAgeThreshold == false")
                    .addNode(new DecisionTreeNode("Income >= 3000").addOutcome("Eligible", "true"))
                    .addNode(new DecisionTreeNode("Income < 3000").addOutcome("Eligible", "false"))));

        // Save this DecisionTree to XML
        tree.toXml(new FileWriter("example/data/output/decision-tree.xml"), true);

        // Load DecisionTree from XML.
        DecisionTree decisionTree = new DecisionTree()
            .fromXml(new FileInputStream("example/data/output/decision-tree.xml"));

        System.out.println("DecisionTree loaded from XML:-\n" + decisionTree);
    }
}

 

Code Walkthrough

  1. DecisionTree instance is created to enable a hierarchical structure. Two variables (ageThreshold, overAgeThreshold) are also assigned to the tree. 
  2. A root node and several child nodes having conditions on properties and outcomes are added to the decision tree. 
  3. The tree is saved/exported to a new output file decision-tree.xml via toXml method of DecisionTree and FileWriter class.
  4. The decision tree is then loaded from the file created in the previous step with the FileInputStream object.
  5. Data is printed in the console with System.out.println() method.

 

Console Output

DecisionTree loaded from XML:-
{
  "fields" : [ {
    "expressionSource" : "40",
    "includeInOutcome" : false,
    "variable" : "ageThreshold"
  }, {
    "expressionSource" : "Age >= ageThreshold",
    "includeInOutcome" : false,
    "variable" : "overAgeThreshold"
  } ],
  "rootExpressionContext" : {
    "parent" : { }
  },
  "rootNode" : {
    "nodes" : [ {
      "conditionAsSource" : "overAgeThreshold == true",
      "nodes" : [ {
        "conditionAsSource" : "houseOwned == true",
        "outcomes" : [ {
          "expressionSource" : "true",
          "variable" : "Eligible"
        } ]
      }, {
        "conditionAsSource" : "houseOwned == false",
        "nodes" : [ {
          "conditionAsSource" : "Income >= 2000",
          "outcomes" : [ {
            "expressionSource" : "true",
            "variable" : "Eligible"
          } ]
        }, {
          "conditionAsSource" : "Income < 2000",
          "outcomes" : [ {
            "expressionSource" : "false",
            "variable" : "Eligible"
          } ]
        } ]
      } ]
    }, {
      "conditionAsSource" : "overAgeThreshold == false",
      "nodes" : [ {
        "conditionAsSource" : "Income >= 3000",
        "outcomes" : [ {
          "expressionSource" : "true",
          "variable" : "Eligible"
        } ]
      }, {
        "conditionAsSource" : "Income < 3000",
        "outcomes" : [ {
          "expressionSource" : "false",
          "variable" : "Eligible"
        } ]
      } ]
    } ]
  },
  "rownum" : 0
}

 

Mobile Analytics