RuleTreeClassifier#
- class RuleTreeClassifier(max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=True, base_stumps: ClassifierMixin | list = None, stump_selection: str = 'random', random_state=None)[source]#
Bases:
ClassifierMixin,RuleTreeRuleTree for classification.
The tree grows breadth-first; a node is not split if it is pure. Each node predicts the majority class of its instances and stores the class probabilities and log-odds.
- Parameters:
max_leaf_nodes (int, default=inf) – Maximum number of leaves.
min_samples_split (int, default=2) – Minimum number of instances to split a node.
max_depth (int, default=inf) – Maximum depth of the tree (the root has depth 0).
prune_useless_leaves (bool, default=True) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list, default=None) – Stumps used to split the nodes (see
RuleTree.tree.RuleTree.RuleTree); they must be classification stumps. If None, DecisionTreeStumpClassifier().stump_selection ({'random', 'best'}, default='random') – How the stump of each node is chosen among base_stumps.
random_state (int, RandomState instance or None, default=None) – Seed of the random choice of the stumps, also given to the stumps without a random_state.
- Variables:
classes (numpy.ndarray) – Classes of the target.
n_classes (int) – Number of classes.
root (RuleTreeNode) – Root of the fitted tree.
candidate_splits (list of tuple) –
(probability, stump, data_type): the stumps usable on the training data, with the data type they are used on.cache (dict) – Values cached by the stumps during fit (see
get_cache()).
See also
RuleTreeRegressorthe same tree for regression.
RuleTree.stumps.classificationthe stumps for classification.
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) >>> model.predict(X[:3]) array([0, 0, 0])
- check_additional_halting_condition(y, curr_idx: ndarray)[source]#
Stop splitting a node whose instances all have the same class.
- Parameters:
y (numpy.ndarray) – Target of the whole data set.
curr_idx (numpy.ndarray) – Indices of the instances in the node.
- Returns:
halt – True if the node is pure.
- Return type:
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) with the majority class, the class probabilities, the log-odds and the number of the instancesidx.- Parameters:
y (numpy.ndarray) – Target of the whole data set.
idx (numpy.ndarray) – Indices of the instances reaching the node.
node_id (str) – Identifier of the node.
node (RuleTreeNode, default=None) – Existing node to update instead of creating a new one.
- Returns:
node
- Return type:
- predict_proba(X: ndarray = None, X_ts=None, X_img=None, X_txt=None)[source]#
Class probabilities of each instance: the ones of the leaf it reaches.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features), default=None) – Tabular data.
X_ts (numpy.ndarray of shape (n_samples, n_signals, length), default=None) – Time series.
X_img (numpy.ndarray of shape (n_samples, ...), default=None) – Images.
X_txt (array-like of shape (n_samples,), default=None) – Texts.
- Returns:
proba – Probabilities of the classes, in the order of
classes_.- Return type:
numpy.ndarray of shape (n_samples, n_classes)
- get_balanced_stumps(X=None, p=0.2, X_ts=None, X_img=None, X_txt=None)[source]#
Internal nodes whose split is balanced on the given data (e.g. the training set): the smaller side has a fraction of all the instances (the balance score) above
p. The visit stops at the first unbalanced node of each path.- Parameters:
X (numpy.ndarray, default=None) – Data, as given to fit.
X_ts (numpy.ndarray, default=None) – Data, as given to fit.
X_img (numpy.ndarray, default=None) – Data, as given to fit.
X_txt (numpy.ndarray, default=None) – Data, as given to fit.
p (float, default=0.2) – Minimum balance score.
- Returns:
stumps – Node id -> (node, balance score), in depth-first order.
- Return type:
- stumps_to_trees(balanced_nodes)[source]#
A tree of depth 1 for each of the given nodes: its split and two leaves.
- Parameters:
balanced_nodes (dict) – Node id -> (node, balance score), as returned by
get_balanced_stumps().- Returns:
trees – (feature, threshold) of the split -> RuleTreeClassifier with that split as root.
- Return type: