Tree

class bartpy.tree.Tree(nodes: typing.List[bartpy.node.TreeNode])[source]

An encapsulation of the structure of a single decision tree Contains no logic, but keeps track of 4 different kinds of nodes within the tree:

  • leaf nodes
  • decision nodes
  • splittable leaf nodes
  • prunable decision nodes
Parameters:nodes (List[Node]) – All nodes contained in the tree, i.e. decision and leaf nodes
add_node(node: bartpy.node.TreeNode) → None[source]

Add a node to the tree Note that this is non-recursive, only adds the node and not any children

decision_nodes

List of decision nodes in the tree. Decision nodes are internal split nodes, i.e. not leaf nodes

leaf_nodes

List of all of the leaf nodes in the tree

nodes

List of all nodes contained in the tree

out_of_sample_predict(X) → numpy.ndarray[source]

Prediction for a covariate matrix not used for training

Note that this is quite slow

Parameters:X (pd.DataFrame) – Covariates to predict for
Returns:
Return type:np.ndarray
predict() → numpy.ndarray[source]

Generate a set of predictions with the same dimensionality as the target array Note that the prediction is from one tree, so represents only (1 / number_of_trees) of the target

prunable_decision_nodes

List of decision nodes in the tree that are suitable for pruning In particular, decision nodes that have two leaf node children

remove_node(node: bartpy.node.TreeNode) → None[source]

Remove a single node from the tree Note that this is non-recursive, only drops the node and not any children

splittable_leaf_nodes

List of all leaf nodes in the tree which can be split in a non-degenerate way i.e. not all rows of the covariate matrix are duplicates

update_y(y: numpy.ndarray) → None[source]

Update the cached value of the target array in all nodes Used to pass in the residuals from the sum of all of the other trees

bartpy.tree.mutate(tree: bartpy.tree.Tree, mutation: bartpy.mutation.TreeMutation) → None[source]

Apply a change to the structure of the tree Modifies not only the tree, but also the links between the TreeNodes

Parameters:
  • tree (Tree) – The tree to mutate
  • mutation (TreeMutation) – The mutation to apply to the tree
class bartpy.mutation.TreeMutation(kind: str, existing_node: bartpy.node.TreeNode, updated_node: bartpy.node.TreeNode)[source]

An encapsulation of a change to be made to the tree. Constructed of three components

  • the node to be changed
  • what it should be changed to
  • a string name of the kind of change (normally grow or prune)