RuleTree.base.RuleTreeBaseGainSplitter.RuleTreeBaseGainSplitter#
- class RuleTreeBaseGainSplitter[source]#
Bases:
RuleTreeBaseSplitter,ABCBase class of the splitters that test every candidate split and keep the one with the highest gain: a new splitter only defines the task (
task) and the gain of a split (gain()).The candidate splits are the thresholds halfway between consecutive distinct values of each numerical feature (
x <= thresholdgoes left) and the values of each categorical feature (x == valuegoes left). Splits with fewer thanmin_samples_leafinstances on a side are skipped. The search is exhaustive and not optimized:gainis called once per candidate.- Variables:
min_samples_leaf (int) – Minimum number of instances on each side of a split. 1 unless the subclass sets it (e.g. as a parameter of
__init__).
See also
RuleTreeBaseSplitterthe attributes set by fit.
Examples
>>> import numpy as np >>> from RuleTree.base.RuleTreeBaseGainSplitter import RuleTreeBaseGainSplitter >>> from RuleTree.utils.define import MODEL_TYPE_CLF >>> class MisclassificationSplitter(RuleTreeBaseGainSplitter): ... task = MODEL_TYPE_CLF ... ... def gain(self, y, left, sample_weight=None): ... # decrease of the number of errors when each side predicts its majority class ... errors = lambda labels: len(labels) - np.unique(labels, return_counts=True)[1].max() ... return errors(y) - errors(y[left]) - errors(y[~left]) >>> X = np.array([[1.0], [2.0], [3.0], [4.0]]) >>> splitter = MisclassificationSplitter().fit(X, np.array([0, 0, 1, 1]), numerical=[0], categorical=[]) >>> splitter.feature_, splitter.threshold_ (0, 2.5)
- __init__()#
Methods
__init__()apply(X)Apply the split to a feature matrix.
Thresholds tested on a numerical feature: halfway between its consecutive distinct values.
fit(X, y, numerical, categorical[, ...])Test every candidate split and keep the one with the highest gain.
gain(y, left[, sample_weight])Gain of a candidate split: the higher, the better.
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
fitmethod.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- min_samples_leaf = 1#
- abstractmethod gain(y: ndarray, left: ndarray, sample_weight=None) float[source]#
Gain of a candidate split: the higher, the better.
- Parameters:
y (numpy.ndarray of shape (n_samples,)) – Target of the instances in the node.
left (numpy.ndarray of bool of shape (n_samples,)) – Instances that go to the left child.
sample_weight (numpy.ndarray of shape (n_samples,), default=None) – Sample weights of the instances in the node.
- Returns:
gain
- Return type:
- candidate_thresholds(x: ndarray) ndarray[source]#
Thresholds tested on a numerical feature: halfway between its consecutive distinct values.
- Parameters:
x (numpy.ndarray of shape (n_samples,)) – Values of the feature.
- Returns:
thresholds
- Return type:
- fit(X, y, numerical, categorical, sample_weight=None, check_input=True)[source]#
Test every candidate split and keep the one with the highest gain.
- 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, passed to gain.
check_input (bool, default=True) – Not used.
- Returns:
self – The fitted splitter.
- Return type:
- Raises:
NoSplitFoundWarning – If no candidate split has at least min_samples_leaf instances on each side.
- 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$') RuleTreeBaseGainSplitter#
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:
categorical (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
categoricalparameter infit.check_input (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
check_inputparameter infit.numerical (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
numericalparameter 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: