FlatMatrixSplitter#
- class FlatMatrixSplitter(criterion, min_samples_leaf=1, gain_function=None)[source]#
Bases:
RuleTreeBaseSplitter,ABCBase class of the splitters that evaluate all the candidate splits of a node together: FlatMatrixClassificationSplitter and FlatMatrixRegressionSplitter.
The candidates 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). They are described by flat arrays (feature and threshold of each candidate). The impurity of the children comes from statistics of the target that can be summed: cumulative sums along each sorted numerical feature, sums by value along each categorical feature.It does not replace ClassificationSplitter and RegressionSplitter, which are faster: it is meant for the choice of the split that a scikit-learn tree does not allow, through
gain_function.A subclass defines
task,_statistics(),_impurity()and_weight(), and, if some children are not allowed,_valid_children().- Parameters:
criterion (str) – Impurity measure (see the subclasses).
min_samples_leaf (int or float, default=1) – Minimum number of instances in each child (a fraction of the instances of the node if float).
gain_function (callable, default=None) – Gain of all the candidates of a node, called once per node with their impurities, the data of the node and the conditions of the splits; None for
information_gain(). The signature isf(impurity, impurity_left, impurity_right, weight_left, weight_right, X, y, features, thresholds, is_categorical, sample_weight=None) -> gains: the arrays have one value per candidate, and X, y and sample_weight are the ones of the node. X is the matrix of the transformer of the stump, so with a transformer other than MatrixTransformer it may not contain the original columns. A gain of -inf or nan discards a candidate.candidate_mask()builds the mask of the candidates, for the functions that need it. To save a tree, the function must be importable, not a lambda.
- Variables:
feature (int) – Column of the feature matrix used by the split.
threshold (float or object) – Threshold of a numerical split, or value of a categorical split.
is_categorical (bool) – Whether the split is categorical.
gain (float) – Gain of the split, as returned by
gain_function.impurity (list of float) – Impurity of the node, of the left child and of the right child, not changed by
gain_function.
Examples
A gain that prefers the splits with children of similar size:
>>> from sklearn.datasets import load_iris >>> from RuleTree.stumps.splitters import FlatMatrixClassificationSplitter, candidate_mask, information_gain >>> def balanced_gain(impurity, impurity_left, impurity_right, weight_left, weight_right, X, y, features, ... thresholds, is_categorical, sample_weight=None): ... left_fraction = candidate_mask(X, features, thresholds, is_categorical).mean(axis=0) ... gains = information_gain(impurity, impurity_left, impurity_right, weight_left, weight_right) ... return gains - abs(left_fraction - 0.5) >>> X, y = load_iris(return_X_y=True) >>> splitter = FlatMatrixClassificationSplitter(gain_function=balanced_gain) >>> splitter = splitter.fit(X, y, numerical=[0, 1, 2, 3], categorical=[], context=None, node_id=None, cache=None) >>> splitter.feature_, round(splitter.threshold_, 2) (2, 4.35)
- fit(X, y, numerical, categorical, sample_weight=None, check_input=True, *, context, node_id, cache)[source]#
Evaluate all the candidate splits and keep the one with the highest gain. Among splits with the same gain, the first one: numerical before categorical, in the order of the columns.
- 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) – Not used.
context (RuleTree or None) – Tree being built, 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)).
- Returns:
self – The fitted splitter.
- Return type:
- Raises:
NoSplitFoundWarning – If the node is pure, no candidate has at least min_samples_leaf instances on each side, or gain_function discards all the candidates.
ValueError – If y has several targets, or gain_function does not return one gain per candidate.
- update_statistics(X, y, sample_weight=None, *, context, node_id, cache)[source]#
Recompute the impurity of the node and of the two children.
- 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)).