RuleTreeRegressor#

class RuleTreeRegressor(max_leaf_nodes=inf, min_samples_split=2, max_depth=inf, prune_useless_leaves=True, base_stumps: RegressorMixin | list = None, stump_selection: str = 'random', random_state=None, leaf_prediction='mean', target_weights=None)[source]#

Bases: RegressorMixin, RuleTree

RuleTree for regression, with one target or several targets at once.

The tree grows breadth-first. Each node predicts the mean (or another statistic, see leaf_prediction) of the target of its instances and stores its standard deviation in prediction_probability.

With a target of shape (n_samples, n_targets), the tree is a multi-target (multi-output) tree: its splits are shared by all the targets, chosen by the average of the impurities of the targets (weighted by target_weights), and each node predicts one value per target, as the multi-output trees of scikit-learn and the predictive clustering trees.

Parameters:
  • max_leaf_nodes (int, default=inf) – Maximum number of leaves.

  • min_samples_split (int, default=2) – Minimum number of instances to split a node.

  • max_depth (int, default=inf) – Maximum depth of the tree (the root has depth 0).

  • prune_useless_leaves (bool, default=True) – Whether to merge, after fit, the sibling leaves with the same prediction.

  • base_stumps (RuleTreeBaseStump or list, default=None) – Stumps used to split the nodes (see RuleTree.tree.RuleTree.RuleTree); they must be regression stumps. If None, DecisionTreeStumpRegressor().

  • stump_selection ({'random', 'best'}, default='random') – How the stump of each node is chosen among base_stumps.

  • random_state (int, RandomState instance or None, default=None) – Seed of the random choice of the stumps, also given to the stumps without a random_state.

  • leaf_prediction ({'mean', 'median'} or callable, default='mean') – Prediction of each node, computed on the target of its instances (for each target): the mean, the median, or f(y_node) that returns a number (one target) or an array of shape (n_targets,).

  • target_weights (None, 'balanced' or array-like of shape (n_targets,), default=None) – Multi-target trees only (ignored with one target): weight of each target in the impurity of the splits, i.e. in the average of the squared errors of the targets. None gives every target the same weight, as scikit-learn: the targets with larger values count more. ‘balanced’ weighs each target by the inverse of its variance on the training set, as the predictive clustering trees, so that every target counts the same whatever its scale. The weights are applied by multiplying each target by the square root of its weight before the stumps are fitted; the predictions are in the original scale.

Variables:
  • root (RuleTreeNode) – Root of the fitted tree.

  • n_targets (int) – Number of targets (1 for a target of shape (n_samples,)).

  • target_scales (numpy.ndarray of shape (n_targets,) or None) – Multipliers of the targets seen by the stumps (see target_weights); None if not used.

  • candidate_splits (list of tuple) – (probability, stump, data_type): the stumps usable on the training data, with the data type they are used on.

  • cache (dict) – Values cached by the stumps during fit (see get_cache()).

See also

RuleTreeClassifier

the same tree for classification.

RuleTree.stumps.regression

the stumps for regression.

Examples

>>> from sklearn.datasets import load_diabetes
>>> from RuleTree import RuleTreeRegressor
>>> X, y = load_diabetes(return_X_y=True)
>>> model = RuleTreeRegressor(max_depth=3, random_state=0).fit(X, y)
>>> model.predict(X[:2]).round(1)
array([208.6,  83.4])
fit(X: ndarray | None = None, y: ndarray | None = None, X_ts=None, X_img=None, X_txt=None, sample_weight=None)[source]#

Fit the tree; see RuleTree.fit. The target can have shape (n_samples,) or (n_samples, n_targets).

Returns:

self

Return type:

RuleTreeRegressor

partial_fit(X: ndarray | None = None, y: ndarray | None = None, X_ts=None, X_img=None, X_txt=None, sample_weight=None)[source]#

Grow the fitted tree further; see RuleTree.partial_fit. The target weights of the first fit are kept (computed on y for a tree loaded with from_dict).

Returns:

self

Return type:

RuleTreeRegressor

prepare_node(y: ndarray, idx: ndarray, node_id: str, node: RuleTreeNode | None = None) → RuleTreeNode[source]#

Create a node (or update node) with the prediction (see leaf_prediction) and the standard deviation of the target and the number of the instances idx; with several targets, one value per target.

Parameters:
  • y (numpy.ndarray) – Target of the whole data set.

  • idx (numpy.ndarray) – Indices of the instances reaching the node.

  • node_id (str) – Identifier of the node.

  • node (RuleTreeNode, default=None) – Existing node to update instead of creating a new one.

Returns:

node

Return type:

RuleTreeNode