Pruner Base Class

Module containing Pruner class.

class pruneabletree.pruner.Pruner(tree)[source]

Base class for decision tree pruners.

Warning: This class should not be used directly. Use derived classes instead.

Methods

is_leaf(node_id) Returns True if the given node is a leaf.
leaf_prediction(node_id) Returns the class index of that the node with the given ID would predict.
num_actual_nodes(tree) Returns the actual number of nodes in the given tree after pruning.
num_instances(node_id[, y_idx]) Returns the number of instances in a given node.
num_leaves(tree) Returns the number of leaves.
to_leaf(node_id, depths) Convert the node with the given ID to a leaf, pruning away its children.
is_leaf(node_id)[source]

Returns True if the given node is a leaf.

Parameters:
node_id : int

The ID of the node in the tree

leaf_prediction(node_id)[source]

Returns the class index of that the node with the given ID would predict.

Parameters:
node_id : int

The ID of the node in the tree

static num_actual_nodes(tree)[source]

Returns the actual number of nodes in the given tree after pruning.

This differs from the tree.node_count property, which contains the number of nodes before pruning. Updating this value would break other features since the underlying data structures still have their original sizes.

Parameters:
tree : Tree object

The underlying tree object of a DecisionTreeClassifier (e.g. clf.tree_).

num_instances(node_id, y_idx=None)[source]

Returns the number of instances in a given node.

If a class index is given, only returns the number of instances belonging to that class.

Parameters:
node_id : int

The ID of the node in the tree

y_idx : int, optional

The index of the class the instances should belong to. If provided, other classes are ignored.

static num_leaves(tree)[source]

Returns the number of leaves.

Parameters:
tree : Tree object

The underlying tree object of a DecisionTreeClassifier (e.g. clf.tree_).

to_leaf(node_id, depths)[source]

Convert the node with the given ID to a leaf, pruning away its children.

Parameters:
node_id : int

The ID of the node in the tree

depths : array of shape = [tree.node_count]

Data structure that keeps track of the depth of each node.