RuleTreeBaseStump#
- class RuleTreeBaseStump[source]#
Bases:
BaseEstimator,ABCAbstract base class of the stumps, the splits of the nodes of a RuleTree.
A stump divides the instances of a node into two children:
applyreturns 1 for the instances that go to the left child and 2 for the ones that go to the right child.Notes
Stumps receive the whole data set as a dictionary
data = {data_type: array}(seeRuleTree.utils.define.DATA_INPUT_TYPES) together withidx, the indices of the instances reaching the node. A fitted stump works on a single data type, chosen by the tree amongsupported_data_typesand stored indata_type_. For convenience,datacan also be a single array, interpreted as data of the stump’s data type.The tree passes its cache to every stump, transformer and splitter (
cache, a dictionary namespace -> dictionary of values that lives until the nextfitorclear_cache()), where they can store anything that can be shared among splits, e.g. embeddings computed on the whole data set. The code of each component decides whether and how to use it: the ones of the library keep their values under their own namespace,self._get_cache(cache)(cache_namespace(): the class and the hyperparameters). Everything needed byapplymust be stored in the stump itself.The tree also passes
node_id, the identifier of the node being split ("R","Rl", …): withcontext.get_node_by_id(node_id)the stump reaches the node, and from it its ancestors and their stumps. The node is a leaf while its stump is fitted; its ancestors are already split.context,node_idandcacheare required infitandupdate_statistics(None outside a tree), optional inapply.Subclasses that work on a 2D matrix can implement
_transform(data of the stump’s data type -> matrix) and_apply_matrix(matrix -> 1/2 labels) and reuseapply()andupdate_statistics(). The stumps of the library are made of a transformer and a splitter (seeRuleTree.stumps.Stump.Stump), which is the simplest way to write a new stump.- Variables:
supported_data_types (tuple of str) – Data types the stump can be fitted on.
task (str) – Kind of target, MODEL_TYPE_CLF (classification) or MODEL_TYPE_REG (regression).
data_type (str) – Data type the stump has been fitted on.
node_id (str or None) – Identifier of the node of the stump in its tree (set by
fitand by RuleTreeNode.set_stump), None for a stump outside a tree.
See also
RuleTree.stumps.Stump.Stumpa stump made of a transformer and a splitter, the simplest way to write a new stump (see Extending the library).
- supported_data_types = ('tabular',)#
- node_id_ = None#
- abstractmethod fit(data, y, idx=None, data_type=None, sample_weight=None, check_input=True, *, context, node_id, cache)[source]#
Fit the stump on the instances
idxofdata.- Parameters:
data (dict or numpy.ndarray) – 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.
data_type (str, default=None) – Data type to use. If None, the first supported data type available in
data.sample_weight (numpy.ndarray, default=None) – Sample weights of the whole data set.
check_input (bool, default=True) – Whether to check the input data.
context (RuleTree or None) – Tree being built, None outside a tree: it holds the cache shared among the splits, and it is passed to the transformer and the splitter.
node_id (str or None) – Identifier of the node being split, stored in
node_id_. None outside a tree.cache (dict or None) – Cache of the tree (namespace -> dictionary of values), None outside a tree: the component decides whether and how to use it (the ones of the library use
self._get_cache(cache)).
- Returns:
self – The fitted stump.
- Return type:
- Raises:
NoSplitFoundWarning – If the instances cannot be split.
- abstractmethod get_rule(columns_names: list = None, scaler: TransformerMixin = None, float_precision: int | None = 3) dict[source]#
Human-readable representation of the split.
- Parameters:
columns_names (list of str, default=None) – Names of the features, used instead of their indices.
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. If None, they are not rounded.
- Returns:
rule – The rule, with at least the keys
textual_ruleandnot_textual_rule(the condition of the left child and its negation),blob_ruleandnot_blob_rule.- Return type:
- get_plot_rule(columns_names: list = None, scaler: TransformerMixin = None, float_precision: int | None = 3) dict[source]#
What the node of the split shows in
RuleTree.plot_tree. The default implementation is the rule,get_rule(...)["textual_rule"]; stumps that draw something in the node, such as the shapelet and pivot stumps, add their media (see below).- Parameters:
- Returns:
rule –
label, the text of the node, and optionally its media:media_values, values saved in the stump (e.g. the shapelets, or the pivots as given to fit), drawn asmedia_kind(‘series’, ‘image’ or ‘text’) with theirmedia_namesandmedia_colors; only withshow_mediaifmedia_optional; below the rule or, ifmedia_on_edgesand they are two, on the arrows of the two children.- Return type:
- abstractmethod node_to_dict() dict[source]#
Dictionary representation of the stump, used to save the tree (see RuleTree.to_dict).
- Returns:
node_dict – JSON-friendly dictionary with the key
stump_type(module of the stump class) and all that is needed bydict_to_node()to rebuild the stump.- Return type:
- abstractmethod classmethod dict_to_node(node_dict: dict) RuleTreeBaseStump[source]#
Create a stump from a dictionary created by
node_to_dict().- Parameters:
node_dict (dict) – Dictionary representation of the stump.
- Returns:
stump – The stump, fitted if it was fitted when saved.
- Return type:
- property task: str#
MODEL_TYPE_CLF (classification) or MODEL_TYPE_REG (regression). A tree accepts only stumps of its task (see RuleTree._get_stumps_task).
- Type:
Kind of target of the stump
- apply(data, idx=None, context=None, node_id=None, cache=None) ndarray[source]#
Apply the split to the instances
idxofdata.- Parameters:
data (dict or numpy.ndarray) – Data set, as
{data_type: array}or a single array of the stump’s data type.idx (numpy.ndarray, default=None) – Indices of the instances to split. If None, all the instances.
context (RuleTree, default=None) – The tree, when called by it (fit, predictions); None otherwise.
node_id (str, default=None) – Identifier of the node of the stump, when called by the tree; None otherwise.
cache (dict, default=None) – Cache of the tree, when called by it; None otherwise.
- Returns:
labels – For each instance in
idx, 1 if it goes to the left child, 2 otherwise.- Return type:
numpy.ndarray of int
- update_statistics(data, y, idx=None, sample_weight=None, check_input=True, *, context, node_id, cache)[source]#
Recompute the statistics of the split (e.g. impurity) on the instances
idxofdata, without changing the split.- Parameters:
data (dict or numpy.ndarray) – 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.
sample_weight (numpy.ndarray, default=None) – Sample weights of the whole data set.
check_input (bool, default=True) – Not used, kept for symmetry with
fit().context (RuleTree or None) – The tree, None outside a tree.
node_id (str or None) – Identifier of the node, None outside a tree.
cache (dict or None) – Cache of the tree (namespace -> dictionary of values), None outside a tree: the component decides whether and how to use it (the ones of the library use
self._get_cache(cache)).
- cache_namespace() str[source]#
Name of the cache of the stump in the tree (see RuleTree.get_cache): the class name and a hash of the hyperparameters, also of the components (e.g. the transformer of a Stump). Stumps of the same class with different hyperparameters do not share cached values.
The tree gives its
random_stateto the stumps without one, so the namespace of a stump used by a tree is the one of its copy incandidate_splits_.- Returns:
namespace – E.g.
"PivotTreeStumpClassifier-1a2b3c4d".- Return type:
Examples
>>> from RuleTree.stumps.classification import DecisionTreeStumpClassifier >>> a, b = DecisionTreeStumpClassifier(), DecisionTreeStumpClassifier(criterion="entropy") >>> a.cache_namespace().startswith("DecisionTreeStumpClassifier-") True >>> a.cache_namespace() == DecisionTreeStumpClassifier().cache_namespace() != b.cache_namespace() True