RuleTreeNode#

class RuleTreeNode(node_id: str, prediction: int | str | float, prediction_probability: ndarray | float, log_odds: ndarray | float, classes: ndarray, parent: RuleTreeNode | None, stump: RuleTreeBaseStump = None, node_l: RuleTreeNode = None, node_r: RuleTreeNode = None, **kwargs)[source]#

Bases: object

A node of a RuleTree: a leaf with a prediction, or an internal node with a stump that splits its instances between its two children.

The identifier of a node is its path from the root: "R" is the root, "Rl" and "Rr" its left and right children, "Rlr" the right child of "Rl", and so on.

Parameters:
  • node_id (str) – Identifier of the node.

  • prediction (int, str or float) – Predicted class, value or cluster label.

  • prediction_probability (numpy.ndarray or float) – Class probabilities (classification) or standard deviation of the target (regression).

  • log_odds (numpy.ndarray or float) – Log-odds of the classes (classification).

  • classes (numpy.ndarray or None) – Classes of the target (classification), None otherwise.

  • parent (RuleTreeNode or None) – Parent node (None for the root).

  • stump (RuleTreeBaseStump, default=None) – Split of the node (None for a leaf).

  • node_l (RuleTreeNode, default=None) – Left and right children (None for a leaf).

  • node_r (RuleTreeNode, default=None) – Left and right children (None for a leaf).

  • **kwargs (dict) – Other attributes, e.g. samples (number of instances).

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> X, y = load_iris(return_X_y=True)
>>> model = RuleTreeClassifier(max_depth=2, random_state=0).fit(X, y)
>>> node = model.get_node_by_id("Rr")
>>> node.get_depth(), node.samples, node.is_leaf()
(1, 100, False)
>>> node.stump.get_rule()["textual_rule"], node.node_l.node_id
('X_3 <= 1.75', 'Rrl')
is_leaf()[source]#

Whether the node is a leaf (it has no children).

Returns:

is_leaf

Return type:

bool

make_leaf()[source]#

Make the node a leaf by removing its children.

Returns:

self

Return type:

RuleTreeNode

simplify() → RuleTreeNode[source]#

Merge the subtrees whose leaves all have the same prediction (see prune_useless_leaves).

Returns:

self – The root of the simplified subtree.

Return type:

RuleTreeNode

set_stump(stump: RuleTreeBaseStump)[source]#

Set the split of the node, and the identifier of the node in the stump (node_id_).

Parameters:

stump (RuleTreeBaseStump) – The fitted stump.

get_depth()[source]#

Depth of the node, from its identifier (the root has depth 0).

Returns:

depth

Return type:

int

get_rule(columns_names: list = None, scaler: TransformerMixin = None)[source]#

Rules of the subtree, as a nested dictionary (see RuleTree.get_rules).

Parameters:
  • columns_names (list of str, default=None) – Names of the tabular features.

  • scaler (TransformerMixin, default=None) – Scaler used to preprocess the data: the thresholds are shown in the original scale.

Returns:

rule – Statistics of the node (node_id, is_leaf, prediction, prediction_probability, log_odds, prediction_classes_), the rules of the children (left_node, right_node) and, for an internal node, the rule of its stump.

Return type:

dict

update_statistics(data, y, idx=None, context=None, cache=None)[source]#

Recompute the statistics of classification of the node (majority class, class probabilities, log-odds, number of instances) on the instances idx, and the statistics of its stump. A node without instances predicts nan.

Parameters:
  • data (dict) – Whole data set, as {data_type: array}.

  • y (numpy.ndarray) – Target of the whole data set.

  • idx (numpy.ndarray, default=None) – Indices of the instances reaching the node. If None, all the instances.

  • context (RuleTree, default=None) – The tree, whose classes_ are used when the node has none.

  • cache (dict, default=None) – Cache of the tree, passed to the stump.

node_to_dict()[source]#

Dictionary representation of the node (without its children, referenced by id).

Returns:

node_dict – JSON-friendly dictionary with the statistics of the node (also samples), the ids of its children and, for an internal node, the dictionary of its stump.

Return type:

dict

classmethod dict_to_node(info_dict)[source]#

Create a node from a dictionary created by node_to_dict() (children not linked).

Parameters:

info_dict (dict) – Dictionary representation of the node.

Returns:

node

Return type:

RuleTreeNode

predict(data, idx: ndarray = None, prediction_step=inf, context=None, cache=None)[source]#

Predictions, leaves and probabilities of the instances, traversing the subtree.

Parameters:
  • data (dict or numpy.ndarray) – Data set, as {data_type: array}. A single array is passed as is to the stumps.

  • idx (numpy.ndarray, default=None) – Indices of the instances to predict. If None, all the instances.

  • prediction_step (int, default=numpy.inf) – Maximum number of splits to traverse: with a finite value, the prediction of the node reached after that many splits.

  • context (RuleTree, default=None) – The tree, passed to the stumps with the identifier of their node.

  • cache (dict, default=None) – Cache of the tree, passed to the stumps.

Returns:

  • labels (numpy.ndarray of shape (n_samples,)) – Prediction of each instance.

  • leaves (numpy.ndarray of shape (n_samples,)) – Id of the node reached by each instance.

  • proba (numpy.ndarray of shape (n_samples, n_values)) – prediction_probability of the node reached by each instance.

get_predicates() → dict[source]#

Internal nodes of the subtree in depth-first order.

Returns:

predicates – Node id -> RuleTreeNode.

Return type:

dict

get_node_by_id(node_id: str) → RuleTreeNode | None[source]#

Node of the subtree with the given identifier.

Parameters:

node_id (str) – Identifier of the node.

Returns:

node – The node, or None if not in the subtree.

Return type:

RuleTreeNode or None

get_leaf_nodes() → dict[source]#

Leaves of the subtree.

Returns:

leaves – Node id -> RuleTreeNode.

Return type:

dict