DecisionTreeSplitter#
- class DecisionTreeSplitter(criterion, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0)[source]#
Bases:
RuleTreeBaseSplitter,ABCBase class of the splitters that maximize the decrease of impurity, ClassificationSplitter and RegressionSplitter.
The numerical features are split by a scikit-learn decision tree of depth 1 (
_sklearn_tree) with the same parameters (x <= thresholdgoes left); on a binary feature, the threshold is then moved halfway between its two values. Then each value of each categorical feature is tested (x == valuegoes left), skipping the values that would leave a child empty, and the best categorical split replaces the numerical one if its gain is higher.A subclass defines
task,_sklearn_treeand_impurities(), and, if the scikit-learn tree has more parameters, its__init__and_tree_params().- Parameters:
criterion (str or callable) – 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). Applies to the numerical features.
min_weight_fraction_leaf (float, default=0.0) – Minimum fraction of the sample weights of the node in each child. Applies to the numerical features.
max_features (int, float, {'sqrt', 'log2'} or None, default=None) – Number of numerical features considered for the split, drawn at random; None for all.
random_state (int, RandomState instance or None, default=None) – Seed of the choice of the features (
max_features) and of the ties between features with the same gain. With None, a tree gives the stump its ownrandom_state.min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.
- 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) – Decrease of impurity (information gain) of the split.
impurity (list of float) – Impurity of the node, of the left child and of the right child.
Notes
The categorical splits do not use
sample_weightand ignoremin_samples_leaf.- fit(X, y, numerical, categorical, sample_weight=None, check_input=True, *, context, node_id, cache)[source]#
Find the best split: the threshold of the scikit-learn tree on the numerical features (halfway between the two values of a binary feature), replaced by a categorical split if one has a higher decrease of impurity.
- 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,) or (n_samples, n_targets)) – 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, 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 split is found.
- 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) – 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)).