pythondecision-tree

Automated Decision Making Using Decision Tree Classifier


I'm trying to code a decision making process which otherwise require extensive IF ELSE scripting, and I'm wondering if it's possible to express the whole process in a form of root, decision and leave nodes in scikit learn's decision tree classifier.

Note that there is no training involved, just a direct user-definition of the nodes.


Solution

  • I get the intent, but a decision tree is a very particular algorithm with goals much different than yours. Depending on the kind of conditions and how many of them you have, you should first evaluate if a map (dictionary) is more appropiate. If you still think you need to go ahead with a tree structure, you should implement your own tree as @akshay-sehgal said.

    You can use this simple Node class that stores the condition as a lambda, in the attribute left_condition. If left_condition is None, the node is a leaf and returns value. Otherwise it's a decision node and goes either to the left or right node, as you can see in the get_value function:

    class Node:
        def __init__(left_condition = None, value = None, left = None, right= None):
        self.left_condition = left_condition
        self.value = value
        self.left = left
        self.right = right
    
    
    def get_value(tree,data):
        node = tree
        while node.left_condition is not None:
            if left_condition(data):
                node = node.left
            else:
                node = node.right
        return node.value