RuleTreeBaseSplitter#
- class RuleTreeBaseSplitter[source]#
Bases:
BaseEstimator,ABCAbstract 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 <= thresholdgoes to the left child) or a value of a categorical feature (x == valuegoes to the left child).A new splitter implements
fit(), which sets the attributes below;apply(),get_state()andset_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
RuleTreeBaseGainSplittera simpler interface, where only the gain of a split is defined.
RuleTree.stumps.Stump.Stumpthe 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, *, context, node_id, ... cache): ... 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=[], context=None, node_id=None, cache=None).apply(X) array([1, 1, 2, 2])
- task = None#
- abstractmethod fit(X: ndarray, y: ndarray, numerical: list, categorical: list, sample_weight=None, check_input=True, *, context, node_id, cache)[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.
context (RuleTree or None) – Tree being built, None outside a tree.
node_id (str or None) – Identifier of the node being split, e.g.
"Rl":context.get_node_by_id(node_id)is the node (still a leaf; its ancestors are already split). 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 splitter.
- Return type:
- Raises:
NoSplitFoundWarning – If no split is found.
- apply(X: ndarray, context=None, node_id=None, cache=None) ndarray[source]#
Apply the split to a feature matrix.
- Parameters:
X (numpy.ndarray of shape (n_samples, n_features)) – Feature matrix.
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 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, *, context, node_id, cache)[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.
context (RuleTree or None) – The tree, None outside a tree.
node_id (str or None) – Identifier of the node, e.g.
"Rl"(context.get_node_by_id(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)).
- 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:
- 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: