Stump#
- class Stump(transformer=None, splitter=None)[source]#
Bases:
RuleTreeBaseStumpStump made of a transformer and a splitter: the transformer turns the data of one type into a feature matrix, the splitter finds the best split of that matrix.
Any transformer can be combined with any splitter. The data types supported by the stump are the ones of the transformer, and its task (classification or regression) is the one of the splitter. The stumps of the library are subclasses that build the two components from their own parameters (
_make_transformerand_make_splitter); see Extending the library to write new ones.- Parameters:
transformer (RuleTreeBaseTransformer, default=None) – Transformer of the data into a feature matrix; it is cloned at each fit.
splitter (RuleTreeBaseSplitter, default=None) – Splitter of the feature matrix; it is cloned at each fit.
- Variables:
transformer (RuleTreeBaseTransformer) – The fitted transformer.
splitter (RuleTreeBaseSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).
data_type (str) – Data type the stump was fitted on.
feature_original (int or str) – Feature of the split, as identified by the transformer: a column of the data, a pivot, …
threshold_original (float or object) – Threshold of a numerical split, or value of a categorical split.
Examples
>>> from sklearn.datasets import load_diabetes >>> from RuleTree import RuleTreeRegressor >>> from RuleTree.stumps.Stump import Stump >>> from RuleTree.stumps.splitters import FairSplitter, RegressionSplitter >>> from RuleTree.stumps.transformers import MatrixTransformer, PivotTransformer >>> from RuleTree.utils.define import MODEL_TYPE_REG >>> X, y = load_diabetes(return_X_y=True) >>> stump = Stump(PivotTransformer(MODEL_TYPE_REG), RegressionSplitter()) >>> stump.task, stump.supported_data_types ('reg', ('tabular', 'ts', 'image')) >>> model = RuleTreeRegressor(max_depth=2, base_stumps=stump).fit(X[:100], y[:100])
- property supported_data_types#
the ones of its transformer.
- Type:
Data types supported by the stump
- property task#
the one of its splitter (MODEL_TYPE_CLF or MODEL_TYPE_REG).
- Type:
Task of the stump
- fit(data, y, idx=None, data_type=None, sample_weight=None, check_input=True, *, context, node_id, cache)[source]#
Fit the stump on the instances
idxofdata: the transformer on the whole data set of the selected type (with the indices of the node; its cache is in the tree), the splitter on the feature matrix of the node; then the transformer keeps only what is needed by the chosen feature (and the splitter is fitted again if the feature matrix changes). The transformer and the splitter also receive the tree (context) andnode_id.- Parameters:
data (dict or numpy.ndarray) – Whole data set, as
{data_type: array}.y (numpy.ndarray) – Target of the whole data set.
idx (numpy.ndarray, default=None) – Indices of the instances reaching the node. If None, all the instances.
data_type (str, default=None) – Data type to use. If None, the first supported data type available in
data.sample_weight (numpy.ndarray, default=None) – Sample weights of the whole data set.
check_input (bool, default=True) – Whether to check the input data.
context (RuleTree or None) – Tree being built, None outside a tree.
node_id (str or None) – Identifier of the node being split, stored in
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 stump.
- Return type:
- Raises:
NoSplitFoundWarning – If the instances cannot be split.
- property feature_original#
a column of the data (int), or a name such as a pivot. None if not fitted.
- Type:
Feature of the split, as identified by the transformer
- property threshold_original#
Threshold (numerical split) or value (categorical split). None if not fitted.
- property is_categorical#
Whether the split is categorical (
x == value). None if not fitted.
- property impurity#
Impurity of the node and of the two children, if computed by the splitter. None if not fitted.
- property gain_#
Gain of the split, used by the trees with stump_selection=’best’.
- get_rule(columns_names=None, scaler=None, float_precision=3)[source]#
Rule of the split, e.g.
petal length <= 2.45orcolor = red: the feature is named after the transformer’s feature id.- Parameters:
columns_names (list of str, default=None) – Names of the features. If None,
X_<index>. Features named by the transformer (see RuleTreeBaseTransformer.feature_id) keep their name.scaler (TransformerMixin, default=None) – Scaler used to preprocess the data: the threshold is also given in the original scale (
threshold_scaled) and shown in the rule.float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.
- Returns:
rule –
feature_idx,threshold,is_categorical,feature_name,threshold_scaled(with a scaler), the condition of the left child (textual_rule,blob_rule) and its negation (not_textual_rule,not_blob_rule).- Return type:
- node_to_dict()[source]#
Dictionary representation of the stump: constructor arguments and, if fitted, the rule, the data type and the state of the transformer and of the splitter.
- classmethod dict_to_node(node_dict)[source]#
Create a stump from a dictionary created by
node_to_dict().