the decision tree rule right now that I am having
{
"id": -1,
"rule": "TOTAL_REVENUE <= 300",
"left": {
"id": "0",
"rule": "TOTAL_DATA_DUR <= 39.5794",
"left": {
"id": "1",
"rule": "TOTAL_DATA_DUR <= 0.7408",
"left": null,
"right": {
"id": "3",
"rule": "TOTAL_PACKAGE_REVENUE <= 15.1350",
"left": {
"id": "4",
"rule": "TOTAL_PACKAGE_REVENUE_14DAYS <= 12.5000",
"left": null,
"right": {
"id": "6",
"value": 84.62
}
},
"right": null
}
},
"right": {
"id": "8",
"rule": "TOTAL_DATA_DUR <= 301.6211",
"left": null,
"right": {
"id": "10",
"rule": "TOTAL_DATA_DUR <= 6898.9146",
"left": {
"id": "11",
"rule": "TOTAL_PACKAGE_REVENUE <= 14.5000",
"left": null,
"right": {
"id": "13",
"rule": "TOTAL_PACKAGE_REVENUE <= 16.0000",
"left": {
"id": "14",
"value": 84.96
},
"right": {
"id": "15",
"rule": "TOTAL_PACKAGE_REVENUE <= 19.5000",
"left": null,
"right": {
"id": "17",
"value": 70.8
}
}
}
},
"right": null
}
}
}
}
the output that I am trying to get is
path1 -> TOTAL_REVENUE <= 300 and TOTAL_DATA_DUR <= 39.5794 TOTAL_DATA_DUR > 0.7408 and TOTAL_PACKAGE_REVENUE <= 15.1350 and TOTAL_PACKAGE_REVENUE_14DAYS > 12.5000
here you can see TOTAL_DATA_DUR > 0.7408 and TOTAL_PACKAGE_REVENUE_14DAYS > 12.5000 is being reversed as it is traversing through the right the rest of the condition is <= because its going through left
path2 -> TOTAL_REVENUE <= 300 and TOTAL_DATA_DUR > 39.5794 and TOTAL_DATA_DUR > 301.6211 and TOTAL_DATA_DUR <= 6898.9146 and TOTAL_PACKAGE_REVENUE > 14.5000 and TOTAL_PACKAGE_REVENUE <= 16.0000
path3 -> TOTAL_REVENUE <= 300 and TOTAL_DATA_DUR > 39.5794 and TOTAL_DATA_DUR > 301.6211 and TOTAL_DATA_DUR <= 6898.9146 and TOTAL_PACKAGE_REVENUE > 14.5000 and TOTAL_PACKAGE_REVENUE > 16.0000 and TOTAL_PACKAGE_REVENUE > 19.5000
I am fairly new to coding how will I get the required output using recursion
the code that I am working on right now
from collections import deque
import json
def isLeaf1(node):
return node.get('left') is None and node.get('right') is None
all_paths = []
def printRootToLeafPaths1(node, path, node_type=None):
# base case
if node is None:
return
# include the current node to the path
if node.get('rule') is not None:
path.append(node.get('rule'))
# if a leaf node is found, print the path
if isLeaf1(node):
all_paths.append(list(path))
# recur for the left and right subtree
printRootToLeafPaths1(node.get('left'), path, 'left')
printRootToLeafPaths1(node.get('right'), path, 'right')
# backtrack: remove the current node after the left, and right subtree are done
path.pop()
# The main function to print paths from the root node to every leaf node
def printRootToLeafPath1(root):
# list to store root-to-leaf path
path = deque()
printRootToLeafPaths1(root, path)
json_rule ='{"id":-1,"rule":"TOTAL_REVENUE <= 300","left":{"id":"0","rule":"TOTAL_DATA_DUR <= 39.5794","left":{"id":"1","rule":"TOTAL_DATA_DUR <= 0.7408","left":null,"right":{"id":"3","rule":"TOTAL_PACKAGE_REVENUE <= 15.1350","left":{"id":"4","rule":"TOTAL_PACKAGE_REVENUE_14DAYS <= 12.5000","left":null,"right":{"id":"6","value":84.62}},"right":null}},"right":{"id":"8","rule":"TOTAL_DATA_DUR <= 301.6211","left":null,"right":{"id":"10","rule":"TOTAL_DATA_DUR <= 6898.9146","left":{"id":"11","rule":"TOTAL_PACKAGE_REVENUE <= 14.5000","left":null,"right":{"id":"13","rule":"TOTAL_PACKAGE_REVENUE <= 16.0000","left":{"id":"14","value":84.96},"right":{"id":"15","rule":"TOTAL_PACKAGE_REVENUE <= 19.5000","impurity":"0.47643265235457066","samples":"304","left":null,"right":{"id":"17","value":70.8}}}},"right":null}}}}'
printRootToLeafPath1(json.loads(json_rule))
can anyone tell me what changes should I make in this code in order to obtains the output paths ?