RuleTree.base.RuleTreeBaseSplitter.RuleTreeBaseSplitter

RuleTree.base.RuleTreeBaseSplitter.RuleTreeBaseSplitter#

class RuleTreeBaseSplitter[source]#

Bases: BaseEstimator, ABC

Abstract base class of the splitters, the component of a stump that finds the best split of a feature matrix: a threshold on a numerical feature (x <= threshold goes to the left child) or a value of a categorical feature (x == value goes to the left child).

A new splitter implements fit(), which sets the attributes below; apply(), get_state() and set_state() work for any split of this form. Splitters are scikit-learn estimators: their parameters are the arguments of __init__.

Variables:
  • task (str) – Kind of target: MODEL_TYPE_CLF (classification) or MODEL_TYPE_REG (regression). Class attribute.

  • feature (int) – Column of the matrix used by the split.

  • threshold (float or object) – Threshold (numerical split) or value (categorical split).

  • is_categorical (bool) – Whether the split is categorical.

  • gain (float) – Gain of the split: the stumps with the highest gain are chosen with stump_selection=’best’.

  • impurity (list of float or None) – Impurity of the node and of the two children, if computed.

See also

RuleTreeBaseGainSplitter

a simpler interface, where only the gain of a split is defined.

RuleTree.stumps.Stump.Stump

the stump that combines a transformer and a splitter.

Examples

A splitter that splits the first numerical feature at its median (see Extending the library):

>>> import numpy as np
>>> from RuleTree.base.RuleTreeBaseSplitter import RuleTreeBaseSplitter
>>> from RuleTree.utils.define import MODEL_TYPE_REG
>>> class MedianSplitter(RuleTreeBaseSplitter):
...     task = MODEL_TYPE_REG
...
...     def fit(self, X, y, numerical, categorical, sample_weight=None, check_input=True):
...         self.feature_ = numerical[0]
...         self.threshold_ = float(np.median(X[:, self.feature_]))
...         self.is_categorical_, self.gain_, self.impurity_ = False, 0.0, None
...         return self
>>> X = np.array([[1.0], [2.0], [3.0], [4.0]])
>>> MedianSplitter().fit(X, None, numerical=[0], categorical=[]).apply(X)
array([1, 1, 2, 2])
__init__()#

Methods

__init__()

apply(X)

Apply the split to a feature matrix.

fit(X, y, numerical, categorical[, ...])

Find the best split of X.

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

get_state()

Fitted state of the splitter: the attributes listed in _state.

set_fit_request(*[, categorical, ...])

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

set_params(**params)

Set the parameters of this estimator.

set_state(state)

Restore the fitted state saved by get_state().

update_statistics(X, y[, sample_weight])

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

Attributes

task = None#
abstractmethod fit(X: ndarray, y: ndarray, numerical: list, categorical: list, sample_weight=None, check_input=True)[source]#

Find the best split of X.

Parameters:
  • X (numpy.ndarray of shape (n_samples, n_features)) – Feature matrix of the instances in the node.

  • y (numpy.ndarray of shape (n_samples,)) – Target of the instances in the node.

  • numerical (list of int) – Indices of the numerical columns of X.

  • categorical (list of int) – Indices of the categorical columns of X.

  • sample_weight (numpy.ndarray of shape (n_samples,), default=None) – Sample weights of the instances in the node.

  • check_input (bool, default=True) – Whether to check the input data.

Returns:

self – The fitted splitter.

Return type:

RuleTreeBaseSplitter

Raises:

NoSplitFoundWarning – If no split is found.

apply(X: ndarray) → ndarray[source]#

Apply the split to a feature matrix.

Parameters:

X (numpy.ndarray of shape (n_samples, n_features)) – Feature matrix.

Returns:

labels – For each row, 1 if it goes to the left child, 2 otherwise.

Return type:

numpy.ndarray of int

update_statistics(X: ndarray, y: ndarray, sample_weight=None)[source]#

Recompute the statistics of the split (e.g. impurity) on new data, without changing the split. The default implementation does nothing.

Parameters:
  • X (numpy.ndarray of shape (n_samples, n_features)) – Feature matrix of the instances in the node.

  • y (numpy.ndarray of shape (n_samples,)) – Target of the instances in the node.

  • sample_weight (numpy.ndarray of shape (n_samples,), default=None) – Sample weights of the instances in the node.

get_state() → dict[source]#

Fitted state of the splitter: the attributes listed in _state.

Returns:

state – JSON-friendly dictionary, restored by set_state().

Return type:

dict

set_state(state: dict)[source]#

Restore the fitted state saved by get_state().

Parameters:

state (dict) – Dictionary created by get_state().

Returns:

self

Return type:

RuleTreeBaseSplitter

set_fit_request(*, categorical: bool | None | str = '$UNCHANGED$', check_input: bool | None | str = '$UNCHANGED$', numerical: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') → RuleTreeBaseSplitter#

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:
  • categorical (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for categorical parameter in fit.

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

  • numerical (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for numerical 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