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() >>> splitter = splitter.fit(X, np.array([0, 0, 1, 1]), numerical=[0], categorical=[], ... context=None, node_id=None, cache=None) >>> splitter.feature_, splitter.threshold_ (0, 2.5)
- 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, *, context, node_id, cache)[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.
context (RuleTree, default=None) – Tree being built, None outside a tree.
node_id (str, default=None) – Identifier of the node, e.g.
"Rl"(context.get_node_by_id(node_id)), None outside a tree.cache (dict, default=None) – Cache of the tree, when called by it; None otherwise.
- Returns:
self – The fitted splitter.
- Return type:
- Raises:
NoSplitFoundWarning – If no candidate split has at least min_samples_leaf instances on each side.