Evaluate a Decision Tree

This example evaluates decision trees by checking input data against predefined conditions and producing outcomes based on that evaluation. It provides a powerful tool for making automated decisions based on complex logic and conditions.

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.

Real-life use cases for this library can be found in various domains where decision-making based on specific criteria is required. For instance, in e-commerce, the library can be used to evaluate customer behavior and preferences to determine personalized product recommendations. The decision tree can consider factors such as past purchase history, browsing patterns, and demographic information to generate tailored recommendations for each customer.

 

Java Code Listing

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

import com.northconcepts.datapipeline.core.Record;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTree;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTreeNode;
import com.northconcepts.datapipeline.foundations.decisiontree.DecisionTreeResult;
import com.northconcepts.datapipeline.internal.expression.DefaultExpressionContext;

public class EvaluateADecisionTree {

    public static void main(String[] args) {
        DefaultExpressionContext input = new DefaultExpressionContext();
        input.setValue("Age", 49);
        input.setValue("houseOwned", true);
        input.setValue("Income", 1000.0);

        DecisionTree tree = new DecisionTree().setRootNode(new DecisionTreeNode()

            .addNode(new DecisionTreeNode("Age >= 40")
                .addNode(new DecisionTreeNode("houseOwned == true").addOutcome("Eligible", "true"))

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

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

        DecisionTreeResult result = tree.evaluate(input);
        Record outcome = result.getOutcome();

        System.out.println("outcome = " + outcome);
    }
}

 

Code Walkthrough

  1. A DefaultExpressionContext is defined as the input of the decision tree where we set properties such as Age, houseOwned and Income.
  2. Then, a DecisionTree is initialized with a root node and a variety of child nodes having conditions on properties Age, houseOwned and Income.
  3. In order to attach an outcome with a node, addOutcome() method is invoked.
  4. The input is then evaluated and stored in a DecisionTreeResult instance.
  5. Finally, getOutcome() is invoked to display the results of the evaluation on the console.

 

Console Output

outcome = Record (MODIFIED) {
  0:[Eligible]:BOOLEAN=[true]:Boolean
}
Mobile Analytics