Add Calculated Fields to a Decision Tree

This example extends the functionality of decision trees by providing the capability to add calculated fields. It allows you to define custom calculations or transformations based on existing fields within the decision tree. By incorporating calculated fields, you can enhance the decision-making capabilities of the tree by introducing additional features or derived values.

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.

In e-commerce, the example 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 AddCalculatedFieldsToADecisionTree {

    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()

            .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"))));

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

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

 

Code Walkthrough

  1. DefaultExpressionContext is defined as the input of the decision tree where we set properties such as AgehouseOwned and Income.
  2. Then, a DecisionTree is initialized with two calculated fields: ageThreshold and overAgeThreshold.
  3. A root node and a variety of child nodes having conditions on properties AgehouseOwned and Income are added.
  4. In order to attach an outcome with a node, addOutcome() method is invoked.
  5. The input is then evaluated and stored in a DecisionTreeResult instance.
  6. 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