RuleTree.base.RuleTreeBaseStump.RuleTreeBaseStump#

class RuleTreeBaseStump[source]#

Bases: BaseEstimator, ABC

Abstract base class of the stumps, the splits of the nodes of a RuleTree.

A stump divides the instances of a node into two children: apply returns 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} (see RuleTree.utils.define.DATA_INPUT_TYPES) together with idx, the indices of the instances reaching the node. A fitted stump works on a single data type, chosen by the tree among supported_data_types and stored in data_type_. For convenience, data can also be a single array, interpreted as data of the stump’s data type.

The tree passed as context holds a cache that lives until the next fit (or clear_cache()), where stumps can store anything that can be shared among splits, e.g. embeddings computed on the whole data set. The stumps of the same class with the same hyperparameters share a cache, named by cache_namespace(). Everything needed by apply must be stored in the stump itself.

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 reuse apply() and update_statistics(). The stumps of the library are made of a transformer and a splitter (see RuleTree.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.

See also

RuleTree.stumps.Stump.Stump

a stump made of a transformer and a splitter, the simplest way to write a new stump (see Extending the library).

__init__()#

Methods

__init__()

apply(data[, idx])

Apply the split to the instances idx of data.

cache_namespace()

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).

dict_to_node(node_dict)

Create a stump from a dictionary created by node_to_dict().

fit(data, y[, idx, context, data_type, ...])

Fit the stump on the instances idx of data.

get_graphviz_rule([columns_names, scaler, ...])

Attributes of the Graphviz node of the split (see RuleTree.export_graphviz).

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

get_rule([columns_names, scaler, ...])

Human-readable representation of the split.

node_to_dict()

Dictionary representation of the stump, used to save the tree (see RuleTree.to_dict).

set_fit_request(*[, check_input, context, ...])

Configure whether metadata should be requested to be passed to the fit method.

set_params(**params)

Set the parameters of this estimator.

supports(data_type)

Whether the stump can be fitted on a data type.

update_statistics(data, y[, idx, context, ...])

Recompute the statistics of the split (e.g. impurity) on the instances idx of data, without changing the split.

Attributes

supported_data_types

task

MODEL_TYPE_CLF (classification) or MODEL_TYPE_REG (regression).

supported_data_types = ('tabular',)#
abstractmethod fit(data, y, idx=None, context=None, data_type=None, sample_weight=None, check_input=True)[source]#

Fit the stump on the instances idx of data.

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.

  • context (RuleTree, default=None) – Tree being built, used to share cached values among the splits.

  • 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.

Returns:

self – The fitted stump.

Return type:

RuleTreeBaseStump

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_rule and not_textual_rule (the condition of the left child and its negation), blob_rule, not_blob_rule, graphviz_rule and not_graphviz_rule (attributes of the Graphviz node).

Return type:

dict

get_graphviz_rule(columns_names: list = None, scaler: TransformerMixin = None, float_precision: int | None = 3) → dict[source]#

Attributes of the Graphviz node of the split (see RuleTree.export_graphviz). The default implementation is get_rule(...)["graphviz_rule"]; stumps that draw something in the node, such as the shapelet stumps, override it.

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 threshold.

Returns:

attributes – Attributes of the Graphviz node, e.g. label.

Return type:

dict

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 by dict_to_node() to rebuild the stump.

Return type:

dict

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:

RuleTreeBaseStump

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

supports(data_type: str) → bool[source]#

Whether the stump can be fitted on a data type.

Parameters:

data_type (str) – A data type, e.g. "tabular" (see RuleTree.utils.define.DATA_INPUT_TYPES).

Returns:

supported – True if data_type is in supported_data_types.

Return type:

bool

apply(data, idx=None) → ndarray[source]#

Apply the split to the instances idx of data.

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.

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, context=None, sample_weight=None, check_input=True)[source]#

Recompute the statistics of the split (e.g. impurity) on the instances idx of data, 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.

  • context (RuleTree, default=None) – Not used, kept for symmetry with fit().

  • 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().

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_state to the stumps without one, so the namespace of a stump used by a tree is the one of its copy in candidate_splits_.

Returns:

namespace – E.g. "PivotTreeStumpClassifier-1a2b3c4d".

Return type:

str

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
set_fit_request(*, check_input: bool | None | str = '$UNCHANGED$', context: bool | None | str = '$UNCHANGED$', data: bool | None | str = '$UNCHANGED$', data_type: bool | None | str = '$UNCHANGED$', idx: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') → RuleTreeBaseStump#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to 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:
  • check_input (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for check_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object