Tree

class gatree.tree.node.Node(att_index=None, att_value=None)

Bases: object

Node class for decision trees. Each node represents a decision in the tree. The node can be a mid-tree node or a leaf node. Mid-tree nodes have an attribute index and an attribute value, while leaf nodes have a class index. The node can have a parent, left child, and right child. The node also has a fitness value, true class values, and predicted class values for evaluation.

Parameters:
  • att_index (int, optional) – Attribute index or -1 if this is a leaf.

  • att_value (any, optional) – Attribute value or class index if this is a leaf.

parent

Parent node.

Type:

Node

left

Left child node.

Type:

Node

right

Right child node.

Type:

Node

att_index

Attribute index or -1 if this is a leaf.

Type:

int, optional

att_value

Attribute value or class index if this is a leaf.

Type:

any, optional

fitness

Placeholder for fitness value.

Type:

None

y_true

List of true class values for evaluation.

Type:

list

y_pred

List of predicted class values for evaluation.

Type:

list

clear_evaluation()

Clears the evaluation of this node and all its children. The true class values and predicted class values are reset to empty lists.

static copy(node, parent=None)

Returns a deep copy of the given node. The parent node can be specified for the copied node.

Parameters:
  • node (Node) – Node to be copied.

  • parent (Node, optional) – Parent node of the copied node.

Returns:

Deep copy of the given node.

Return type:

Node

depth()

Returns the depth of this node.

Returns:

Depth of this node.

Return type:

int

get_children()

Returns the children of this node.

Returns:

Tuple containing the left and right children nodes.

Return type:

tuple

get_leaves()

Returns the leaves of this node.

Returns:

List of leaves of this node.

Return type:

list

get_root()

Returns the root node of the tree.

Returns:

Root node of the tree.

Return type:

Node

is_evaluated()

Returns true if this node and all its children are evaluated. A node is evaluated if it has true class values and predicted class values.

Returns:

True if this node and all its children are evaluated.

Return type:

bool

make_node(depth=0, max_depth=None, random=None, att_indexes=None, att_values=None, class_count=None)

Randomly generates the node and its children. The depth of the tree and the maximum depth of the tree can be specified. The random number generator, attribute indexes, attribute values, and number of classes must be provided.

Parameters:
  • depth (int, optional) – Current depth of the tree.

  • max_depth (int, optional) – Maximum depth of the tree.

  • random (Random, optional) – Random number generator.

  • att_indexes (numpy.ndarray, optional) – Attribute indexes.

  • att_values (dict, optional) – Attribute values.

  • class_count (int, optional) – Number of classes.

Returns:

Randomly generated node with children.

Return type:

Node

max_depth()

Returns the maximum depth of the tree. The depth of the tree is the number of edges on the longest path from the root to a leaf.

Returns:

Maximum depth of the tree.

Return type:

int

static max_depth_helper(n)

Helper function for max_depth.

Parameters:

n (Node) – Node to be used as the root.

Returns:

Maximum depth of the tree.

Return type:

int

predict_one(X, y=None, train=False)

Predicts the class of the given instance. If the actual class is provided, the true class value and predicted class value are stored for evaluation.

Parameters:
  • X (list) – Instance to be predicted.

  • y (int) – Actual class of the given instance.

  • train (bool) – If it’s used for training or only predicting

Returns:

Predicted class.

Return type:

int

set_left(n)

Sets the left child of this node to the given node. Also sets the parent of the given node to this node.

Parameters:

n (Node) – Node to be set as the left child.

set_right(n)

Sets the right child of this node to the given node. Also sets the parent of the given node to this node.

Parameters:

n (Node) – Node to be set as the right child.

size()

Size is the number of all nodes (mid-tree nodes + leaves) in the trees.

Returns:

Number of all nodes in the trees.

Return type:

int

static size_helper(n)

Helper function for size.

Parameters:

n (Node) – Node to be used as the root.

Returns:

Number of all nodes in the trees.

Return type:

int