Builder Module
The builder module of PyXAI allows to build a classifier with tree components using nodes and leaves. This can be very useful to test and check different types of explanations. Definitions of classes and methods are given on this page and some examples are provided in the Decision Tree, Random Forest and Boosted Tree pages.
PyXAI data structures are used directly in the builder module. A DecisionNode object represents a node while a LeafNode is a leaf. A DecisionTree object consists of DecisionNode and LeafNode. The models are specified through the objects DecisionTree, RandomForest and BoostedTrees. A DecisionTree contains only one tree whereas a RandomForest or BoostedTree object represents a set of trees and then contains some DecisionTree objects. Each DecisionNode represents a condition “<id_feature> <operator> <threshold> ?” (such as “$x_4 \ge 0.5$ ?”) which can be created using the builder.DecisionNode class.
Methods
LeafNode.__init__: To create a leaf of a tree.DecisionNode.__init__: To create a desision node that is a condition “<id_feature> <operator> <threshold> ?” (such as “$x_4 ≥ 0.5$ ?”)DecisionTree.__init__: To create a decision tree consisting of decision nodes and leaves.RandomForest.__init__: To create a random forest consisting of decision trees.BoostedTrees.__init__: To create a boosted tree consisting of decision trees.BoostedTreesRegression.__init__: To create a boosted tree to predict regression values.
Enumerations
OperatorCondition
Represent the possible values of operators of a condition “<id_feature> <operator> <threshold> ?”
-
OperatorCondition.GE|Builder.GE|GE:
The operator ≥ “Greater than or Equal to”. -
OperatorCondition.GT|Builder.GT|GT:
The operator > “Greater Than”. -
OperatorCondition.LE|Builder.LE|LE:
The operator ≤ “Less Than or Equal”. -
OperatorCondition.LT|Builder.LT|LT:
The operator < “Less Than”. -
OperatorCondition.EQ|Builder.EQ|EQ:
The operator = “Equal”. -
OperatorCondition.NEQ|Builder.NEQ|NEQ:
The operator ≠ “Not Equal”.
Example
from pyxai import Builder, Learning, Explaining
nodeT1_1 = Builder.DecisionNode(1, left=0, right=1)
nodeT1_3 = Builder.DecisionNode(3, left=0, right=nodeT1_1)
nodeT1_2 = Builder.DecisionNode(2, left=1, right=nodeT1_3)
nodeT1_4 = Builder.DecisionNode(4, left=0, right=nodeT1_2)
tree1 = Builder.DecisionTree(4, nodeT1_4, force_features_equal_to_binaries=True, class_names=['A', 'B'])
nodeT2_4 = Builder.DecisionNode(4, left=0, right=1)
nodeT2_1 = Builder.DecisionNode(1, left=0, right=nodeT2_4)
nodeT2_2 = Builder.DecisionNode(2, left=nodeT2_1, right=1)
tree2 = Builder.DecisionTree(4, nodeT2_2, force_features_equal_to_binaries=True,
class_names=['A', 'B']) # 4 features but only 3 used
nodeT3_1_1 = Builder.DecisionNode(1, left=0, right=1)
nodeT3_1_2 = Builder.DecisionNode(1, left=0, right=1)
nodeT3_4_1 = Builder.DecisionNode(4, left=0, right=nodeT3_1_1)
nodeT3_4_2 = Builder.DecisionNode(4, left=0, right=1)
nodeT3_2_1 = Builder.DecisionNode(2, left=nodeT3_1_2, right=nodeT3_4_1)
nodeT3_2_2 = Builder.DecisionNode(2, left=0, right=nodeT3_4_2)
nodeT3_3_1 = Builder.DecisionNode(3, left=nodeT3_2_1, right=nodeT3_2_2)
tree3 = Builder.DecisionTree(4, nodeT3_3_1, force_features_equal_to_binaries=True, class_names=['A', 'B'])
forest = Builder.RandomForest([tree1, tree2, tree3], n_classes=2, class_names=['A', 'B'])