RuleTree.base.RuleTreeBaseStump.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 passed as
contextholds a cache that lives until the nextfit(orclear_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 bycache_namespace(). Everything needed byapplymust 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 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:
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).
- __init__()#
Methods
__init__()apply(data[, idx])Apply the split to the instances
idxofdata.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
idxofdata.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.
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
fitmethod.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
idxofdata, without changing the split.Attributes
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
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.
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:
- 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_rule,not_blob_rule,graphviz_ruleandnot_graphviz_rule(attributes of the Graphviz node).- Return type:
- 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:
- Returns:
attributes – Attributes of the Graphviz node, e.g.
label.- 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) 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.
- 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
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.
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_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
- 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
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:
check_input (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
check_inputparameter infit.context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
contextparameter infit.data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
dataparameter infit.data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
data_typeparameter infit.idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
idxparameter 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: