RuleTree.stumps package#
Subpackages#
- RuleTree.stumps.classification package
- Submodules
- RuleTree.stumps.classification.DecisionTreeStumpClassifier module
- RuleTree.stumps.classification.MultipleObliquePivotTreeStumpClassifier module
- RuleTree.stumps.classification.MultiplePivotTreeStumpClassifier module
- RuleTree.stumps.classification.ObliqueDecisionTreeStumpClassifier module
- RuleTree.stumps.classification.ObliquePivotTreeStumpClassifier module
- RuleTree.stumps.classification.PartialPivotTreeStumpClassifier module
- RuleTree.stumps.classification.PartialProximityTreeStumpClassifier module
- RuleTree.stumps.classification.PivotTreeStumpClassifier module
- RuleTree.stumps.classification.ProximityTreeStumpClassifier module
- RuleTree.stumps.classification.ShapeletTreeStumpClassifier module
- Module contents
- RuleTree.stumps.regression package
- Submodules
- RuleTree.stumps.regression.DecisionTreeStumpRegressor module
- RuleTree.stumps.regression.FairTreeStumpRegressor module
- RuleTree.stumps.regression.ObliqueDecisionTreeStumpRegressor module
- RuleTree.stumps.regression.PivotTreeStumpRegressor module
- RuleTree.stumps.regression.ProximityTreeStumpRegressor module
- RuleTree.stumps.regression.ShapeletTreeStumpRegressor module
- RuleTree.stumps.regression.XGBoostStumpRegressor module
- Module contents
- RuleTree.stumps.splitters package
- RuleTree.stumps.transformers package
- Submodules
- RuleTree.stumps.transformers.BivariateTransformer module
- RuleTree.stumps.transformers.HouseholderTransformer module
- RuleTree.stumps.transformers.MatrixTransformer module
- RuleTree.stumps.transformers.MultiplePivotTransformer module
- RuleTree.stumps.transformers.ObliquePivotTransformer module
- RuleTree.stumps.transformers.ObliqueTransformer module
- RuleTree.stumps.transformers.PartialPivotTransformer module
- RuleTree.stumps.transformers.PartialProximityTransformer module
- RuleTree.stumps.transformers.PivotTransformer module
- RuleTree.stumps.transformers.ProximityTransformer module
- RuleTree.stumps.transformers.ShapeletTransformer module
- Module contents
Submodules#
RuleTree.stumps.ObliqueTreeStump module#
ObliqueTreeStump: what the oblique stumps for classification and regression have in common.
- check_oblique_split_type(oblique_split_type: str)[source]#
Check the type of oblique split of a stump.
- Parameters:
oblique_split_type (str) – ‘householder’ or ‘bivariate’.
- Raises:
ValueError – For any other value.
- format_combination(names: list, coefficients: list, float_precision=3) str[source]#
Text of a linear combination, e.g.
0.8 * X_2 - 0.6 * X_3; a single term with coefficient 1 (an axis-parallel direction) is just its name.- Parameters:
- Returns:
text
- Return type:
Examples
>>> from RuleTree.stumps.ObliqueTreeStump import format_combination >>> format_combination(["X_0", "X_3"], [0.5, -0.25]) '0.5 * X_0 - 0.25 * X_3' >>> format_combination(["X_3"], [1.0]) 'X_3'
- combination_rule(feature_idx, names, coefficients, threshold, float_precision=3) dict[source]#
Rule of a threshold on a linear combination:
combination <= thresholdgoes left.- Parameters:
- Returns:
rule – See RuleTreeBaseStump.get_rule; also
coefficients.- Return type:
- class ObliqueTreeStump[source]#
Bases:
objectMixin of the oblique stumps (ObliqueDecisionTreeStumpClassifier and ObliqueDecisionTreeStumpRegressor): the oblique transformer built from the parameters of the stump, and the rule of the split.
The subclass defines the parameters and the splitter, by deriving also from the decision tree stump of its task (
class ObliqueDecisionTreeStumpClassifier(ObliqueTreeStump, DecisionTreeStumpClassifier)).- property coefficients#
Coefficients of the combination of the split, None if not fitted.
- get_rule(columns_names=None, scaler=None, float_precision=3)[source]#
Rule of the split, e.g.
0.8 * X_2 - 0.6 * X_3 <= 1.5.- Parameters:
- Returns:
rule –
feature_idx(the columns of the combination),coefficients,threshold,is_categorical,feature_name(the combination), the condition of the left child (textual_rule,blob_rule,graphviz_rule) and its negation. Empty if the stump is not fitted.- Return type:
- Raises:
ValueError – If a scaler is given.
RuleTree.stumps.PivotTreeStump module#
PivotTreeStump, MultiplePivotTreeStump and ObliquePivotTreeStump: what the pivot stumps have in common.
- class PivotTreeStump[source]#
Bases:
objectMixin of the pivot stumps (PivotTreeStumpClassifier and PivotTreeStumpRegressor): the PivotTransformer built from the parameters of the stump, the pivots of the split and its rule.
The subclass defines the parameters and the splitter, by deriving also from the decision tree stump of its task (
class PivotTreeStumpClassifier(PivotTreeStump, DecisionTreeStumpClassifier)).- property X_split_instance#
The pivot of the split (standardized numerical features), None if not fitted.
- get_pivots()[source]#
Pivots of the node, as indices of instances of the training set.
- Returns:
pivots –
descriptives(medoids),discriminativesandcandidates(both) pivots of the node, and the pivotusedby the split.- Return type:
Examples
>>> from sklearn.datasets import load_iris >>> from RuleTree.stumps.classification import PivotTreeStumpClassifier >>> X, y = load_iris(return_X_y=True) >>> pivots = PivotTreeStumpClassifier(random_state=0).fit(X, y).get_pivots() >>> pivots["descriptives"], pivots["discriminatives"], pivots["used"] ([7, 96, 112], [27, 77, 127], 112)
- get_rule(columns_names=None, scaler=None, float_precision=3)[source]#
Rule of the split,
P_<i> <= threshold, whereP_<i>is the distance from the training instancei.- Parameters:
columns_names (list of str, default=None) – Not used: the rule does not name the features.
scaler (TransformerMixin, default=None) – Not used: the distances are computed on the data standardized by the stump.
float_precision (int, default=3) – Number of decimals of the threshold; None for no rounding.
- Returns:
rule –
feature_idx(the pivot),threshold,is_categorical,feature_name, the condition of the left child (textual_rule,blob_rule,graphviz_rule) and its negation (not_textual_rule,not_blob_rule,not_graphviz_rule). Empty if the stump is not fitted.- Return type:
- class MultiplePivotTreeStump[source]#
Bases:
objectMixin of the multiple pivot stumps (MultiplePivotTreeStumpClassifier and MultipleObliquePivotTreeStumpClassifier): the MultiplePivotTransformer built from the parameters of the stump, the pivots of the split and its rule,
P_<a> <= P_<b>(closer to the training instanceathan tob).- get_pivots()[source]#
Pivots of the node, as indices of instances of the training set.
- Returns:
pivots –
descriptives,discriminativesandcandidatespivots of the node, and the two pivotsusedby the split.- Return type:
- class ObliquePivotTreeStump[source]#
Bases:
objectMixin of the oblique pivot stumps (ObliquePivotTreeStumpClassifier): the ObliquePivotTransformer built from the parameters of the stump, the pivots of the split and its rule, e.g.
0.7 * P_12 - 0.3 * P_57 <= 1.2.- property coefficients#
Coefficients of the combination of the split, None if not fitted.
- get_pivots()[source]#
Pivots of the node, as indices of instances of the training set.
- Returns:
pivots –
descriptives,discriminativesandcandidatespivots of the node, and the pivotsusedby the combination of the split.- Return type:
- get_rule(columns_names=None, scaler=None, float_precision=3)[source]#
Rule of the split, e.g.
0.7 * P_12 - 0.3 * P_57 <= 1.2, whereP_<i>is the distance from the training instancei.- Parameters:
columns_names (list of str, default=None) – Not used: the rule does not name the features.
scaler (TransformerMixin, default=None) – Not used: the distances are computed on the data standardized by the stump.
float_precision (int, default=3) – Number of decimals of the coefficients and of the threshold; None for no rounding.
- Returns:
rule – See RuleTreeBaseStump.get_rule; also
coefficients. Empty if the stump is not fitted.- Return type:
RuleTree.stumps.ProximityTreeStump module#
ProximityTreeStump and PartialProximityTreeStump: what the proximity stumps have in common.
- proximity_rule(feature_idx, first: str, second: str, threshold) dict[source]#
Rule of a proximity split:
first <= secondgoes left, where first and second are the distances from the two references of the split.- Parameters:
feature_idx (object) – Identifier of the pair, stored in
feature_idx.first (str) – Text of the distances from the first and from the second reference.
second (str) – Text of the distances from the first and from the second reference.
threshold (float) – Threshold of the split on the 0/1 comparison (between 0 and 1).
- Returns:
rule – See RuleTreeBaseStump.get_rule.
- Return type:
- class ProximityTreeStump[source]#
Bases:
objectMixin of the proximity stumps for time series (ProximityTreeStumpClassifier and ProximityTreeStumpRegressor): the ProximityTransformer built from the parameters of the stump, the rule and its drawing.
The subclass defines the parameters and the splitter, by deriving also from the shapelet stump of its task (
class ProximityTreeStumpClassifier(ProximityTreeStump, ShapeletTreeStumpClassifier)).- get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]#
Rule of the split,
euclidean(TS, green_shp) <= euclidean(TS, red_shp): the time series closer to the first shapelet (green in the drawing,st.shapelets[0]) than to the second one (red,st.shapelets[1]) go left.- Parameters:
- Returns:
rule – See RuleTreeBaseStump.get_rule.
- Return type:
- Raises:
ValueError – If a scaler is given.
- get_graphviz_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]#
Attributes of the Graphviz node of the split: the rule above a plot of the two shapelets, green and red, drawn into a temporary PNG file (requires matplotlib).
- Parameters:
- Returns:
attributes – Attributes of the Graphviz node.
- Return type:
- Raises:
MissingOptionalDependencyError – If matplotlib is not installed.
- class PartialProximityTreeStump[source]#
Bases:
objectMixin of the proximity stumps for tabular data (PartialProximityTreeStumpClassifier): the PartialProximityTransformer built from the parameters of the stump and the rule.
- get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]#
Rule of the split, e.g.
euclidean(x, SP0(d: petal length=1.3)) <= euclidean(x, SP1(d: petal length=5.1)): the instances closer to the first partial pivot than to the second one go left.- Parameters:
columns_names (list of str, default=None) – Names of the features. If None,
Feat. <index>.scaler (TransformerMixin, default=None) – Scaler used to preprocess the data: the values of the pivots are shown in the original scale.
float_precision (int, default=3) – Not used: the values of the pivots are not rounded.
- Returns:
rule – See RuleTreeBaseStump.get_rule.
- Return type:
RuleTree.stumps.Stump module#
File: Stump.py
The stump made of a transformer (how the data are represented, see RuleTreeBaseTransformer) and a splitter (how the split is chosen, see RuleTreeBaseSplitter).
- 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',)) >>> 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, context=None, data_type=None, sample_weight=None, check_input=True)[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 and the cache of 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).- 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.
context (RuleTree, default=None) – Tree being built, whose cache is shared by the stumps of the same class.
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.
- 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.
X_2 <= 4.5: the feature is named after the transformer’s feature id.- Parameters:
columns_names (list of str, default=None) – Names of the columns of the tabular data, used for the features that are columns.
scaler (TransformerMixin, default=None) – Scaler used to preprocess the data: the threshold is also given in the original scale.
float_precision (int, default=3) – Number of decimals of the threshold; None for no rounding.
- Returns:
rule – The textual, blob and Graphviz forms of the condition of the left child and of its negation (see
RuleTreeBaseStump.get_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().
- set_fit_request(*, check_input: bool | None | str = '$UNCHANGED$', context: bool | None | str = '$UNCHANGED$', data: bool | None | str = '$UNCHANGED$', data_type: bool | None | str = '$UNCHANGED$', idx: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') Stump#
Configure whether metadata should be requested to be passed to the
fitmethod.Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with
enable_metadata_routing=True(seesklearn.set_config()). Please check the User Guide on how the routing mechanism works.The options for each parameter are:
True: metadata is requested, and passed tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.Added in version 1.3.
- Parameters:
check_input (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
check_inputparameter infit.context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
contextparameter infit.data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
dataparameter infit.data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
data_typeparameter infit.idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
idxparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type: