RuleTree.tree package#
Submodules#
RuleTree.tree.RuleTree module#
Base class of the RuleTree models: a decision tree built node by node, where each node is split by a stump chosen among the base stumps.
- class RuleTree(max_leaf_nodes, min_samples_split, max_depth, prune_useless_leaves, base_stumps, stump_selection, random_state, exceptions='raise')[source]#
Bases:
RuleTreeBase,ABCBase class of the RuleTree models.
A RuleTree is a binary decision tree built best-first: the nodes are split one at a time, in the order given by
queue_push, until a stopping criterion is met. Each split is a stump (seeRuleTree.base.RuleTreeBaseStump.RuleTreeBaseStump) chosen amongbase_stumps, so the same tree can mix different kinds of splits (thresholds on features, distances from pivots, shapelets, …) and different data types (tabular data, time series, images).The subclasses define the task:
RuleTreeClassifier,RuleTreeRegressor,RuleTreeClusterand its variants.- Parameters:
max_leaf_nodes (int, default=None) – Maximum number of leaves. If None, unlimited.
min_samples_split (int) – Minimum number of instances to split a node.
max_depth (int) – Maximum depth of the tree (the root has depth 0).
prune_useless_leaves (bool) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list) –
Stumps used to split the nodes. One of:
a stump;
a list of stumps, chosen with equal probability;
a list of tuples
(probability, stump)or(probability, stump, data_types), where the probabilities sum to 1 anddata_types(a data type or a list of data types, seeRuleTree.utils.define.DATA_INPUT_TYPES) restricts the data the stump is used on. By default, a stump is used on every data type it supports. The probability of a stump is split equally among its data types.
stump_selection ({'random', 'best'}) – How the stump of each node is chosen: at random according to the probabilities, or the one with the highest gain among all the stumps and data types.
random_state (int, RandomState instance or None) – Seed of the random choice of the stumps, also given to the stumps whose random_state is None (so that they do not break ties at random).
exceptions ({'raise', 'ignore'}, default='raise') – What to do when a stump raises an error: raise it, or make the node a leaf and warn.
- Variables:
root (RuleTreeNode) – Root of the fitted tree.
candidate_splits (list of tuple) – Pairs (stump, data type) usable on the training data, with their probability:
(probability, stump, data_type).cache (dict) – Values cached by the stumps during fit (see
get_cache()).
See also
RuleTreeClassifier,RuleTreeRegressor,RuleTreeClusterRuleTree.base.RuleTreeBaseStump.RuleTreeBaseStumpthe interface of the stumps.
- get_cache(namespace: str) dict[source]#
Dictionary where the stumps store values shared among the splits, e.g. embeddings of the whole data set. The cache is cleared at every
fit(), kept bypartial_fit(), and can be cleared manually withclear_cache().
- clear_cache()[source]#
Remove all the values cached by the stumps (see
get_cache()).
- fit(X: ndarray | None = None, y: ndarray | None = None, X_ts: ndarray | None = None, X_img: ndarray | None = None, X_txt: ndarray | None = None, sample_weight=None)[source]#
Fit the tree on the provided data, starting from an empty tree.
At least one of X, X_ts, X_img and X_txt must be given; all the inputs refer to the same instances. Each argument is a data type (see
RuleTree.utils.define.DATA_INPUT_TYPES): the stumps are used on the data types they support.- Parameters:
X (numpy.ndarray of shape (n_samples, n_features), default=None) – Tabular data.
y (numpy.ndarray of shape (n_samples,), default=None) – Target (None for clustering).
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.
sample_weight (numpy.ndarray of shape (n_samples,), default=None) – Sample weights, passed to the stumps.
- Returns:
self – The fitted tree.
- Return type:
- Raises:
ValueError – If no input is given, the inputs have different numbers of instances, or no stump supports the provided data.
- partial_fit(X: ndarray | None = None, y: ndarray | None = None, X_ts: ndarray | None = None, X_img: ndarray | None = None, X_txt: ndarray | None = None, sample_weight=None)[source]#
Continue growing the fitted tree: the current leaves are split further using the provided data, within the limits of max_depth and max_leaf_nodes (e.g. after increasing them with set_params). The existing splits are not changed. If the tree is not fitted, this is
fit().The cache of the stumps (see
get_cache()) is kept: callclear_cache()first if the data differ from those used to fill it.- Parameters:
X (numpy.ndarray of shape (n_samples, n_features), default=None) – Tabular data.
y (numpy.ndarray of shape (n_samples,), default=None) – Target (None for clustering).
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.
sample_weight (numpy.ndarray of shape (n_samples,), default=None) – Sample weights, passed to the stumps.
- Returns:
self – The fitted tree.
- Return type:
- make_split(data: dict, y: ndarray | None, idx: ndarray, sample_weight=None) RuleTreeBaseStump[source]#
Fit the split of a node, choosing among
candidate_splits_according tostump_selection: a random pair (stump, data type), or the pair with the highest gain (gain_).- Parameters:
data (dict) – Whole data set, as
{data_type: array}.y (numpy.ndarray or None) – Target of the whole data set.
idx (numpy.ndarray) – Indices of the instances in the node.
sample_weight (numpy.ndarray, default=None) – Sample weights of the whole data set.
- Returns:
stump – The fitted stump.
- Return type:
- Raises:
NoSplitFoundWarning – If no stump can split the node.
- is_split_useless(data: dict, idx: ndarray, labels: ndarray) bool[source]#
Whether a split is useless, i.e. all the instances go to the same child.
- Parameters:
data (dict) – Whole data set, as
{data_type: array}.idx (numpy.ndarray) – Indices of the instances in the node.
labels (numpy.ndarray) – Result of the split on idx (1 left, 2 right).
- Returns:
useless
- Return type:
- predict(X: ndarray = None, X_ts=None, X_img=None, X_txt=None)[source]#
Predict the output of each instance: the prediction 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:
y_pred – Predicted classes, values or cluster labels.
- Return type:
numpy.ndarray of shape (n_samples,)
- apply(X: ndarray | None = None, X_ts=None, X_img=None, X_txt=None)[source]#
Leaf reached by each instance.
- 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:
leaves – Identifier (node_id) of the leaf reached by each instance.
- Return type:
numpy.ndarray of shape (n_samples,)
- update_statistics(X: ndarray | None = None, y: ndarray | None = None, X_ts=None, X_img=None, X_txt=None)[source]#
Recompute the statistics of the nodes (predictions, probabilities, number of instances, …) and of their stumps on new data, without changing the splits. For classification,
classes_become the classes of y (e.g. to re-label the tree with a new target). A node reached by no instance predicts nan.- Parameters:
X (numpy.ndarray of shape (n_samples, n_features), default=None) – Tabular data.
y (numpy.ndarray of shape (n_samples,), default=None) – Target (None for clustering).
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.
- get_rules(columns_names=None)[source]#
Rules of the tree, as a nested dictionary.
- Parameters:
columns_names (list of str, default=None) – Names of the tabular features, used in the rules instead of
X_<index>.- Returns:
rules – Rule of the root: the node statistics (
node_id,is_leaf,prediction, …), the rule of the split (textual_rule,not_textual_rule, …) and the rules of the children inleft_nodeandright_node(None for a leaf). Seeprint_rules().- Return type:
- make_leaf(node: RuleTreeNode) RuleTreeNode[source]#
Hook called when a node becomes a leaf. The default implementation does nothing.
- Parameters:
node (RuleTreeNode) – The new leaf.
- Returns:
node
- Return type:
- queue_pop()[source]#
Pop the next node to split from the queue.
- Returns:
idx (numpy.ndarray) – Indices of the instances reaching the node.
node (RuleTreeNode)
- check_additional_halting_condition(y, curr_idx: ndarray)[source]#
Additional condition to stop splitting a node, defined by the subclasses (e.g. a pure node for classification). The default implementation never stops.
- Parameters:
y (numpy.ndarray or None) – Target of the whole data set.
curr_idx (numpy.ndarray) – Indices of the instances in the node.
- Returns:
halt
- Return type:
- abstractmethod prepare_node(y: ndarray | None, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) with the statistics of the instancesidx: prediction, probabilities, number of instances.- Parameters:
y (numpy.ndarray or None) – Target of the whole data set.
idx (numpy.ndarray) – Indices of the instances reaching the node.
node_id (str) – Identifier of the node: its path from the root, e.g.
"Rlr".node (RuleTreeNode, default=None) – Existing node to update instead of creating a new one.
- Returns:
node
- Return type:
- abstractmethod queue_push(node: RuleTreeNode, idx: ndarray)[source]#
Push a node to split into the queue: the priority defines the order in which the tree grows.
- Parameters:
node (RuleTreeNode) – The node.
idx (numpy.ndarray) – Indices of the instances reaching the node.
- local_interpretation(X, joint_contribution=False)[source]#
Data needed to compute the contribution of each feature to the predictions (see the implementations of RuleTreeClassifier and RuleTreeRegressor, which return the contributions).
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Tabular data.
joint_contribution (bool, default=False) – Not used here.
- Returns:
leaves (numpy.ndarray) – Index of the leaf reached by each instance.
paths (list of list of int) – Path from the root to each leaf.
leaf_to_path (dict) – Leaf index -> its path.
values (numpy.ndarray) – Values of the nodes (see _tree_value).
- eval_contributions(leaves, paths, leaf_to_path, values, biases, line_shape, joint_contribution=False)[source]#
Contributions of the features to the predictions, as in treeinterpreter: the prediction of an instance is the value of the root (bias) plus the change of value along its path, attributed to the feature of each split.
- Parameters:
leaves (numpy.ndarray) – Index of the leaf reached by each instance.
paths (list of list of int) – Path from the root to each leaf.
leaf_to_path (dict) – Leaf index -> its path.
values (numpy.ndarray) – Values of the nodes.
biases (numpy.ndarray) – Value of the root, for each instance.
line_shape (int or tuple) – Shape of the contributions of one instance.
joint_contribution (bool, default=False) – If True, the contributions are given to the sets of features along the path.
- Returns:
direct_prediction (numpy.ndarray) – Value of the leaf of each instance.
biases (numpy.ndarray) – Value of the root.
contributions (numpy.ndarray or list of dict) – Contribution of each feature (or set of features, with joint_contribution) to each prediction.
- classmethod print_rules(rules: dict = None, digits=2, indent: int = 0)[source]#
Print rules as an indented list of conditions and outputs.
- Parameters:
rules (dict) – Rules returned by
get_rules().digits (int, default=2) – Number of decimals of the numerical outputs.
indent (int, default=0) – Indentation level of the root.
- to_dict(filename=None)[source]#
Dictionary representation of the fitted tree, from which
from_dict()rebuilds it.- Parameters:
filename (str or path-like, default=None) – If given, the dictionary is also saved to this file as JSON.
- Returns:
dictionary – JSON-friendly dictionary with the class of the tree, its parameters (
args, base stumps included), the classes for classification trees and the nodes. The values may be NumPy scalars: to write it withjson, usecls=RuleTree.utils.data_utils.json_numpy_encoder(as done withfilename). See Saving and loading trees.- Return type:
- Raises:
sklearn.exceptions.NotFittedError – If the tree is not fitted.
- export_graphviz(columns_names=None, scaler=None, float_precision=3, filename: str | None = None)[source]#
Graphviz representation of the tree (requires the graphviz package).
- 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.
float_precision (int, default=3) – Number of decimals of the thresholds.
filename (str, default=None) – If given, the graph is rendered to this file; otherwise it is rendered to a temporary file and opened (both require the Graphviz binaries).
- Returns:
graph – The graph, if filename is None.
- Return type:
graphviz.Digraph or None
- get_predicates()[source]#
Internal nodes (the ones with a split) in depth-first order.
- Returns:
predicates – Node id -> RuleTreeNode.
- Return type:
- get_node_by_id(node_id)[source]#
Node with the given identifier.
- Parameters:
node_id (str) – Identifier of the node: its path from the root, e.g.
"Rlr".- Returns:
node – The node, or None if not in the tree.
- Return type:
RuleTreeNode or None
- get_leaf_nodes()[source]#
Leaves of the tree.
- Returns:
leaves – Node id -> RuleTreeNode.
- Return type:
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTree#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTree#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTree#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
RuleTree.tree.RuleTreeClassifier module#
RuleTreeClassifier: the RuleTree for classification.
- class RuleTreeClassifier(max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=False, 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=False) – 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:
- queue_push(node: RuleTreeNode, idx: ndarray)[source]#
Push a node into the queue, with priority to the shallowest nodes (breadth-first growth).
- Parameters:
node (RuleTreeNode) – The node.
idx (numpy.ndarray) – Indices of the instances reaching the node.
- 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)
- local_interpretation(X, joint_contribution=False)[source]#
Contribution of each feature to the predicted class probabilities, as in treeinterpreter: the probabilities of an instance are the ones of the root (bias) plus the contributions of the features of the splits along its path.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Tabular data.
joint_contribution (bool, default=False) – If True, the contributions are given to the sets of features along the path.
- Returns:
prediction (numpy.ndarray of shape (n_samples, n_classes)) – Class probabilities of the leaf of each instance.
bias (numpy.ndarray of shape (n_samples, n_classes)) – Class probabilities of the root.
contributions (numpy.ndarray of shape (n_samples, n_features, n_classes) or list of dict) – Contribution of each feature (or set of features, with joint_contribution).
- get_balanced_stumps(current_node=None, stumps=None, p=0.2)[source]#
Internal nodes whose split is balanced: the smaller side has a fraction of the training set (
balance_score) abovep. The visit stops at the first unbalanced node of each path.- Parameters:
current_node (RuleTreeNode, default=None) – Root of the subtree. If None, the root of the tree.
stumps (dict, default=None) – Dictionary being filled.
p (float, default=0.2) – Minimum balance score.
- Returns:
stumps – Node id -> (node, balance score).
- 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:
- classmethod complete_tree(node, X, y, n_classes_)[source]#
Recompute recursively the predictions, class probabilities and number of instances of the nodes of a tree on new data, with classes
0, ..., n_classes_ - 1.- Parameters:
node (RuleTreeNode) – Root of the subtree.
X (numpy.ndarray) – Data reaching the node.
y (numpy.ndarray) – Target of the data reaching the node.
n_classes (int) – Number of classes.
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClassifier#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClassifier#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_proba_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeClassifier#
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict_proba.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict_proba.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict_proba.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeClassifier#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClassifier#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
RuleTree.tree.RuleTreeCluster module#
RuleTreeCluster and its variants: RuleTrees for clustering, also used to classify or regress with clusters found without looking at the target.
- class RuleTreeCluster(n_components: int = 2, clus_impurity: str = 'r2', bic_eps: float = 0.0, max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=False, base_stumps: RegressorMixin | list = None, random_state=None)[source]#
Bases:
ClusterMixin,RuleTreeRuleTree for clustering: each leaf is a cluster described by the rules of its path.
To split a node, the instances are projected on their principal components (PCA, MCA or FAMD, for numerical, categorical or mixed data); for each component, a regression stump is fitted to predict it, and the best split is kept according to
clus_impurity. A node is split only if the split decreases its BIC (seebic_eps). The tree grows from the largest node. Only tabular data are supported.After fit, the nodes predict integer labels assigned in breadth-first order (‘R’ -> 0, ‘Rl’ -> 1, ‘Rr’ -> 2, …);
predictreturns the label of the leaf of each instance.- Parameters:
n_components (int, default=2) – Number of principal components predicted by the stumps of each node.
clus_impurity ({'r2', 'bic'}, default='r2') – Criterion to choose the split among the ones on the different components: the variance of the component explained by the split (‘r2’) or the Bayesian Information Criterion of the two clusters (‘bic’).
bic_eps (float, default=0.0) – A split is kept only if it decreases the BIC of the node by more than bic_eps times its value.
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=False) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list, default=None) – Regression stumps used to split the nodes, fitted to predict a principal component. If None, DecisionTreeStumpRegressor().
random_state (int, RandomState instance or None, default=None) – Seed of the principal component analysis and of the random choice of the stumps, also given to the stumps without a random_state.
- Variables:
label_encoder (dict) – Node id -> integer label.
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()).
Examples
>>> from sklearn.datasets import make_blobs >>> from sklearn.metrics import adjusted_rand_score >>> from RuleTree import RuleTreeCluster >>> X, y = make_blobs(n_samples=300, centers=3, cluster_std=0.5, random_state=0) >>> labels = RuleTreeCluster(max_depth=3, random_state=0).fit_predict(X) >>> round(adjusted_rand_score(y, labels), 2) 0.95
- is_split_useless(data: dict, idx: ndarray, labels: ndarray)[source]#
Whether a split is useless: all the instances go to the same child, or the BIC of the two clusters is not lower than the BIC of the node (by bic_eps times its value).
- Parameters:
data (dict) – Whole data set, as
{data_type: array}.idx (numpy.ndarray) – Indices of the instances in the node.
labels (numpy.ndarray) – Result of the split on idx (1 left, 2 right).
- Returns:
useless
- Return type:
- fit_predict(X, y=None, **kwargs)[source]#
Fit the tree and return the cluster label of each instance.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Tabular data.
y (None) – Not used, present for API consistency.
**kwargs (dict) – Arguments passed to fit.
- Returns:
labels – Cluster labels.
- Return type:
numpy.ndarray of shape (n_samples,)
- queue_push(node: RuleTreeNode, idx: ndarray)[source]#
Push a node into the queue, with priority to the nodes with more instances.
- Parameters:
node (RuleTreeNode) – The node.
idx (numpy.ndarray) – Indices of the instances reaching the node.
- make_split(data: dict, y, idx: ndarray, sample_weight=None) RuleTreeBaseStump[source]#
Split a node on its principal components: the instances are projected with PCA, MCA or FAMD (numerical, categorical or mixed data), a stump is fitted to predict each component, and the best split according to
clus_impurityis kept.- Parameters:
data (dict) – Whole data set, as
{data_type: array}(only tabular data).y (numpy.ndarray or None) – Not used by the split (the stumps predict the components).
idx (numpy.ndarray) – Indices of the instances in the node.
sample_weight (None) – Not used, present for API consistency.
- Returns:
stump – The best stump.
- Return type:
- Raises:
NoSplitFoundWarning – If no component can be split.
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) whose prediction is its node id, replaced by an integer label in_post_fit_fix().- Parameters:
y (numpy.ndarray or None) – Not used.
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:
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeCluster#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeCluster#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeCluster#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
- class RuleTreeClusterClassifier(n_components: int = 2, clus_impurity: str = 'r2', bic_eps: float = 0.0, max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=False, base_stumps: RegressorMixin | list = None, random_state=None)[source]#
Bases:
ClassifierMixin,RuleTreeClusterRuleTreeCluster whose leaves predict a class: the tree is built as in RuleTreeCluster, without looking at the target, and each node predicts the majority class of its instances, as in RuleTreeClassifier.
- Parameters:
n_components (int, default=2) – Number of principal components predicted by the stumps of each node.
clus_impurity ({'r2', 'bic'}, default='r2') – Criterion to choose the split among the ones on the different components: the variance of the component explained by the split (‘r2’) or the Bayesian Information Criterion of the two clusters (‘bic’).
bic_eps (float, default=0.0) – A split is kept only if it decreases the BIC of the node by more than bic_eps times its value.
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=False) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list, default=None) – Regression stumps used to split the nodes, fitted to predict a principal component. If None, DecisionTreeStumpRegressor().
random_state (int, RandomState instance or None, default=None) – Seed of the principal component analysis and 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.
See also
RuleTreeClusterthe clustering tree.
RuleTreeClassifiera tree whose splits are chosen with the target.
Examples
>>> from sklearn.datasets import make_blobs >>> from RuleTree.tree.RuleTreeCluster import RuleTreeClusterClassifier >>> X, y = make_blobs(n_samples=300, centers=3, cluster_std=0.5, random_state=0) >>> model = RuleTreeClusterClassifier(max_depth=2, random_state=0).fit(X, y) >>> round(model.score(X, y), 2) 0.99
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) with the statistics of classification, as in RuleTreeClassifier.- 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 (default=None) – Not supported by clustering, present for API consistency.
X_img (default=None) – Not supported by clustering, present for API consistency.
X_txt (default=None) – Not supported by clustering, present for API consistency.
- Returns:
proba
- Return type:
numpy.ndarray of shape (n_samples, n_classes)
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterClassifier#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterClassifier#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_proba_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeClusterClassifier#
Configure whether metadata should be requested to be passed to the
predict_probamethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredict_probaif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict_proba.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict_proba.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict_proba.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict_proba.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeClusterClassifier#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterClassifier#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- class RuleTreeClusterRegressor(n_components: int = 2, clus_impurity: str = 'r2', bic_eps: float = 0.0, max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=False, base_stumps: RegressorMixin | list = None, random_state=None)[source]#
Bases:
RegressorMixin,RuleTreeClusterRuleTreeCluster whose leaves predict a value: the tree is built as in RuleTreeCluster, without looking at the target, and each node predicts the mean of the target of its instances, as in RuleTreeRegressor.
- Parameters:
n_components (int, default=2) – Number of principal components predicted by the stumps of each node.
clus_impurity ({'r2', 'bic'}, default='r2') – Criterion to choose the split among the ones on the different components: the variance of the component explained by the split (‘r2’) or the Bayesian Information Criterion of the two clusters (‘bic’).
bic_eps (float, default=0.0) – A split is kept only if it decreases the BIC of the node by more than bic_eps times its value.
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=False) – Whether to merge, after fit, the sibling leaves with the same prediction.
base_stumps (RuleTreeBaseStump or list, default=None) – Regression stumps used to split the nodes, fitted to predict a principal component. If None, DecisionTreeStumpRegressor().
random_state (int, RandomState instance or None, default=None) – Seed of the principal component analysis and of the random choice of the stumps, also given to the stumps without a random_state.
- Variables:
root (RuleTreeNode) – Root of the fitted tree.
See also
RuleTreeClusterthe clustering tree.
RuleTreeRegressora tree whose splits are chosen with the target.
Examples
>>> from sklearn.datasets import load_diabetes >>> from RuleTree.tree.RuleTreeCluster import RuleTreeClusterRegressor >>> X, y = load_diabetes(return_X_y=True) >>> model = RuleTreeClusterRegressor(max_depth=2, random_state=0).fit(X, y) >>> model.predict(X[:2]).round(1) array([177.6, 111.8])
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) with the statistics of regression, as in RuleTreeRegressor.- 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:
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterRegressor#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterRegressor#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeClusterRegressor#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeClusterRegressor#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
RuleTree.tree.RuleTreeNode module#
RuleTreeNode: a node of a RuleTree.
- 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:
objectA 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).
- Variables:
balance_score (float) – For internal nodes, fraction of the training set in the smaller side of the split.
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')
- 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:
- set_stump(stump: RuleTreeBaseStump)[source]#
Set the split of the node.
- Parameters:
stump (RuleTreeBaseStump) – The fitted stump.
- get_possible_outputs() tuple[set, set][source]#
Predictions of the nodes of the subtree.
- Returns:
leaf_predictions (set) – Predictions of the leaves.
internal_predictions (set) – Predictions of the internal nodes.
- get_depth()[source]#
Depth of the node, from its identifier (the root has depth 0).
- Returns:
depth
- Return type:
- get_rule(columns_names: list = None, scaler: TransformerMixin = None)[source]#
Rules of the subtree, as a nested dictionary (see RuleTree.get_rules).
- Parameters:
- 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:
- update_statistics(data, y, idx=None, context=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.
- 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
samplesand, for an internal node,balance_score), the ids of its children and, for an internal node, the dictionary of its stump.- Return type:
- 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:
- encode_node(index, parent, vector, stump, node_index=0)[source]#
Encode the subtree in arrays indexed as a complete binary tree (children of i at 2i+1 and 2i+2), for trees of univariate splits.
- Parameters:
index (dict) – Node id -> position, filled.
parent (numpy.ndarray) – Position of the parent of each position, filled.
vector (numpy.ndarray of shape (2, n_positions)) – Feature + 1 (-1 for leaves) and threshold (prediction for leaves) of each position, filled.
stump (object) – Not used.
node_index (int, default=0) – Position of this node.
- export_graphviz(graph=None, columns_names=None, scaler=None, float_precision=3)[source]#
Graphviz representation of the subtree (requires the graphviz package).
- Parameters:
graph (graphviz.Digraph, default=None) – Graph to add the nodes to. If None, a new graph.
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.
float_precision (int, default=3) – Number of decimals of the thresholds.
- Returns:
graph
- Return type:
graphviz.Digraph
- predict(data, idx: ndarray = None, prediction_step=inf)[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.
- 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_probabilityof 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:
- 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
RuleTree.tree.RuleTreeRegressor module#
RuleTreeRegressor: the RuleTree for regression.
- class RuleTreeRegressor(max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=False, base_stumps: RegressorMixin | list = None, stump_selection: str = 'random', random_state=None)[source]#
Bases:
RegressorMixin,RuleTreeRuleTree for regression.
The tree grows breadth-first. Each node predicts the mean of the target of its instances and stores its standard deviation in
prediction_probability.- 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=False) – 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 regression stumps. If None, DecisionTreeStumpRegressor().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:
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
RuleTreeClassifierthe same tree for classification.
RuleTree.stumps.regressionthe stumps for regression.
Examples
>>> from sklearn.datasets import load_diabetes >>> from RuleTree import RuleTreeRegressor >>> X, y = load_diabetes(return_X_y=True) >>> model = RuleTreeRegressor(max_depth=3, random_state=0).fit(X, y) >>> model.predict(X[:2]).round(1) array([208.6, 83.4])
- queue_push(node: RuleTreeNode, idx: ndarray)[source]#
Push a node into the queue, with priority to the shallowest nodes (breadth-first growth).
- Parameters:
node (RuleTreeNode) – The node.
idx (numpy.ndarray) – Indices of the instances reaching the node.
- prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) RuleTreeNode[source]#
Create a node (or update
node) with the mean (prediction) and the standard deviation of the target 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:
- local_interpretation(X, joint_contribution=False)[source]#
Contribution of each feature to the predictions, as in treeinterpreter: the prediction of an instance is the one of the root (bias) plus the contributions of the features of the splits along its path.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Tabular data.
joint_contribution (bool, default=False) – If True, the contributions are given to the sets of features along the path.
- Returns:
prediction (numpy.ndarray of shape (n_samples,)) – Prediction of the leaf of each instance.
bias (numpy.ndarray of shape (n_samples,)) – Prediction of the root.
contributions (numpy.ndarray of shape (n_samples, n_features) or list of dict) – Contribution of each feature (or set of features, with joint_contribution).
- set_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeRegressor#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter infit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter infit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type:
- set_partial_fit_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeRegressor#
Configure whether metadata should be requested to be passed to the
partial_fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topartial_fitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topartial_fit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpartial_fit.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpartial_fit.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpartial_fit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter inpartial_fit.
- Returns:
self – The updated object.
- Return type:
- set_predict_request(*, X_img: bool | None | str = '$UNCHANGED$', X_ts: bool | None | str = '$UNCHANGED$', X_txt: bool | None | str = '$UNCHANGED$') RuleTreeRegressor#
Configure whether metadata should be requested to be passed to the
predictmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed topredictif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it topredict.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
X_img (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_imgparameter inpredict.X_ts (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_tsparameter inpredict.X_txt (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
X_txtparameter inpredict.
- Returns:
self – The updated object.
- Return type:
- set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeRegressor#
Configure whether metadata should be requested to be passed to the
scoremethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed toscoreif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it toscore.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.