Mockingjay.Tree (Mockingjay v0.1.0)

A struct containing a convenient in-memory representation of a decision tree.

Each "node" in the tree is a Tree struct. A "decision" or "inner" node is any node whose :left and :right values are a valid Tree. A "leaf" or "outer" node is any node whose :left and :right values are nil. :left and :right must either both be nil or both be a Tree.

  • :id - The id of the node. This is a unique reference. This is generated automatically when using the from_map function.
  • :left - The left child of the node. This is nil for leaf nodes. This is a Tree for decision nodes.
  • :right - The right child of the node. This is nil for leaf nodes. This is a Tree for decision nodes.
  • :value - The value of the node:
    • For leaf nodes, this is the value as a number.
    • For non-leaf nodes, this is a map containing the following keys:
      • :feature - The feature used to split the data (if it is not a leaf).
      • :threshold - The threshold used to split the data (if it is not a leaf).

Link to this section Summary

Types

t()

A simple binary tree implementation.

Functions

Returns a list of nodes in BFS order. For the uses in Mockingjay, BFS is tree-level order from right to left on each level. The nodes include their children nodes.

Checks is the given child_id exists in the tree.

Returns the depth of the tree. The root node is at depth 0.

Returns the depth of the tree starting from the given level.

Returns tree nodes as a list in DFS order.

Returns a Tree struct from a map. The map must have the appropriate required keys for the Tree struct. Any extra keys are ignored.

Returns a list of the decision nodes in the tree in BFS order.

Returns a list of the values of the decision nodes in the tree in BFS order.

Returns a list of the leaf nodes in the tree in BFS order.

Returns a list of the values of the leaf nodes in the tree in BFS order.

Traverse the tree in BFS order, applying a reducer function to each node.

Link to this section Types

@type t() :: %Mockingjay.Tree{
  id: reference(),
  left: t() | nil,
  right: t() | nil,
  value: number() | %{feature: pos_integer(), threshold: number()}
}

A simple binary tree implementation.

Link to this section Functions

Returns a list of nodes in BFS order. For the uses in Mockingjay, BFS is tree-level order from right to left on each level. The nodes include their children nodes.

Link to this function

child?(tree, child_id)

Checks is the given child_id exists in the tree.

Returns the depth of the tree. The root node is at depth 0.

Link to this function

depth_from_level(tree, current_depth)

Returns the depth of the tree starting from the given level.

Returns tree nodes as a list in DFS order.

Returns a Tree struct from a map. The map must have the appropriate required keys for the Tree struct. Any extra keys are ignored.

Link to this function

get_decision_nodes(tree)

Returns a list of the decision nodes in the tree in BFS order.

Link to this function

get_decision_values(tree)

Returns a list of the values of the decision nodes in the tree in BFS order.

Link to this function

get_leaf_nodes(tree)

Returns a list of the leaf nodes in the tree in BFS order.

Link to this function

get_leaf_values(tree)

Returns a list of the values of the leaf nodes in the tree in BFS order.

Link to this function

reduce_tree(root, acc, reducer)

Traverse the tree in BFS order, applying a reducer function to each node.