RuleTree.stumps.classification package#

Submodules#

RuleTree.stumps.classification.DecisionTreeStumpClassifier module#

DecisionTreeStumpClassifier: the split on one feature for classification.

class DecisionTreeStumpClassifier(criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: Stump

Decision tree stump for classification: a threshold on a numerical feature (x <= t) or a value of a categorical feature (x == v), chosen to maximize the information gain.

It works on tabular data, time series and images; time series and images are flattened into one feature per value. It is a Stump made of a MatrixTransformer and a ClassificationSplitter, and the default stump of RuleTreeClassifier.

Parameters:
  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MatrixTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Column of the data used by the split.

  • threshold_original (float or object) – Threshold of a numerical split, or value of a categorical split.

See also

DecisionTreeStumpRegressor

the same stump for regression.

sklearn.tree.DecisionTreeClassifier

the tree used to split the numerical features.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import DecisionTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = DecisionTreeStumpClassifier(criterion="entropy", random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]
'X_3 <= 0.8'
>>> stump.apply(X[[0, 100]])  # 1: left child, 2: right child
array([1, 2])

As base stump of a tree:

>>> model = RuleTreeClassifier(max_depth=2, base_stumps=DecisionTreeStumpClassifier(min_samples_leaf=5))
>>> model = model.fit(X, y)
get_rule(columns_names=None, scaler=None, float_precision=3)[source]#

Rule of the split, e.g. petal length <= 2.45 or color = red.

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, graphviz_rule) and its negation (not_textual_rule, not_blob_rule, not_graphviz_rule).

Return type:

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$') → DecisionTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.MultipleObliquePivotTreeStumpClassifier module#

MultipleObliquePivotTreeStumpClassifier: which of two pivots, found by oblique splits, is closer.

class MultipleObliquePivotTreeStumpClassifier(distance_measure='euclidean', oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: MultiplePivotTreeStump, DecisionTreeStumpClassifier

Multiple oblique pivot stump for classification of tabular data: as MultiplePivotTreeStumpClassifier (the instances closer to one pivot than to another one go left, P_a <= P_b), but the discriminative pivots of each class are the instances combined by an oblique direction on the distances (oblique_split_type), instead of the root split of a decision tree: up to max_oblique_features (Householder) or two (bivariate) for each class.

It is a Stump made of a MultiplePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions that find the discriminative pivots are chosen (see ObliqueDecisionTreeStumpClassifier).

  • max_oblique_features (int, default=2) – Householder only: maximum number of pivots combined by a direction.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MultiplePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the two pivots of the split.

See also

MultiplePivotTreeStumpClassifier

the discriminative pivots found by a decision tree.

ObliquePivotTreeStumpClassifier

an oblique split on the distances from the pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import MultipleObliquePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = MultipleObliquePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_pivots()["discriminatives"]  # up to 2 for each class
[15, 22, 83, 98, 106, 118]
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=MultipleObliquePivotTreeStumpClassifier()).fit(X, y)
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$') → MultipleObliquePivotTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.MultiplePivotTreeStumpClassifier module#

MultiplePivotTreeStumpClassifier: which of two pivots is closer to the instance, for classification.

class MultiplePivotTreeStumpClassifier(distance_measure='euclidean', criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: MultiplePivotTreeStump, DecisionTreeStumpClassifier

Multiple pivot stump for classification of tabular data: the instances closer to one pivot than to another one go left (P_a <= P_b, where P_i is the distance from the training instance i).

At each node, the candidate pivots are selected as in PivotTreeStumpClassifier (for each class, its medoid and its most discriminative instance), and the split is the pair of pivots of different classes whose comparison best splits the node. Distances are computed on the standardized numerical features; the distance matrix of the training set is computed once and cached in the tree.

It is a Stump made of a MultiplePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MultiplePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the two pivots of the split.

See also

PivotTreeStumpClassifier

a threshold on the distance from a single pivot.

MultipleObliquePivotTreeStumpClassifier

the same comparison, with pivots found by oblique splits.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import MultiplePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = MultiplePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]  # closer to the training instance a than to b
'P_27 <= P_77'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=MultiplePivotTreeStumpClassifier()).fit(X, y)
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$') → MultiplePivotTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.ObliqueDecisionTreeStumpClassifier module#

ObliqueDecisionTreeStumpClassifier: a threshold on a linear combination of features, for classification.

class ObliqueDecisionTreeStumpClassifier(oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: ObliqueTreeStump, DecisionTreeStumpClassifier

Oblique stump for classification of tabular data: a threshold on a linear combination of a few numerical features (0.8 * X_2 - 0.6 * X_3 <= t goes left), i.e. a hyperplane that is not parallel to the axes.

The directions are chosen at each node in one of two ways (oblique_split_type):

  • 'householder': the axes reflected by the Householder reflection that maps the principal component of the node onto an axis; each direction combines at most max_oblique_features features (see HouseholderTransformer);

  • 'bivariate': n_orientations rotations of the best pair of features (see BivariateTransformer).

The combinations are computed on the numerical features as they are: scale the data beforehand if the features have different scales. It is a Stump made of an oblique transformer and a ClassificationSplitter.

Parameters:
  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions are chosen.

  • max_oblique_features (int, default=2) – Householder only: maximum number of features in the reflection vector.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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).

  • min_weight_fraction_leaf (float, default=0.0) – Minimum fraction of the sample weights of the node in each child.

  • max_features (int, float, {'sqrt', 'log2'} or None, default=None) – Number of directions considered for the split, drawn at random; None for all.

  • random_state (int, RandomState instance or None, default=None) – Seed of the PCA (Householder), of the choice of the directions (max_features) and of the ties between directions with the same gain. With None, a tree gives the stump its own random_state.

  • min_impurity_decrease (float, default=0.0) – A split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:

See also

ObliqueDecisionTreeStumpRegressor

the same stump for regression.

DecisionTreeStumpClassifier

the axis-parallel splits.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ObliqueDecisionTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = ObliqueDecisionTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule(columns_names=["sl", "sw", "pl", "pw"])["textual_rule"]
'-0.085 * sw + 0.857 * pl <= 1.85'
>>> stump.feature_original, [round(c, 3) for c in stump.coefficients]
((1, 2), [-0.085, 0.857])
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=ObliqueDecisionTreeStumpClassifier("bivariate"))
>>> model = model.fit(X, y)
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$') → ObliqueDecisionTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.ObliquePivotTreeStumpClassifier module#

ObliquePivotTreeStumpClassifier: a threshold on a linear combination of distances from pivots.

class ObliquePivotTreeStumpClassifier(distance_measure='euclidean', oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: ObliquePivotTreeStump, DecisionTreeStumpClassifier

Oblique pivot stump for classification of tabular data: a threshold on a linear combination of the distances from a few pivots (0.7 * P_12 - 0.3 * P_57 <= t goes left, where P_i is the distance from the training instance i).

At each node, the candidate pivots are, for each class, its medoid and the instances combined by an oblique direction on the distances (oblique_split_type); then the distances of the instances of the node from the candidates are combined by oblique directions of the same type, and the split is the best threshold on one of them (see ObliquePivotTransformer). Distances are computed on the standardized numerical features.

It is a Stump made of an ObliquePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions are chosen (see ObliqueDecisionTreeStumpClassifier).

  • max_oblique_features (int, default=2) – Householder only: maximum number of pivots combined by a direction.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ObliquePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the pivots combined by the split.

  • coefficients (list of float) – Their coefficients.

See also

PivotTreeStumpClassifier

a threshold on the distance from a single pivot.

ObliqueDecisionTreeStumpClassifier

oblique splits on the features.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ObliquePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = ObliquePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]
'-0.447 * P_22 + 0.468 * P_118 <= 0.955'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=ObliquePivotTreeStumpClassifier("cosine")).fit(X, y)
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$') → ObliquePivotTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.PartialPivotTreeStumpClassifier module#

PartialPivotTreeStumpClassifier: the split on the distance from a partial pivot, for classification.

class PartialPivotTreeStumpClassifier(n_shapelets=8, n_ts_for_selection=100, n_features_strategy=2, selection='random', distance='euclidean', use_combination=True, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: DecisionTreeStumpClassifier

Partial pivot stump for classification of tabular data: a threshold on the distance from a partial pivot, an instance of the node restricted to a few features (distance <= t goes left).

At each node, n_shapelets partial pivots are selected among the instances of the node, each on a subset of the features (n_features_strategy), and the split is the best threshold on the distance from one of them. The distance is computed only on the features of the pivot, so the rule involves a few features. In the rules, SP<i>(...) lists the features and values of the pivot.

It is a Stump made of a PartialPivotTransformer and a ClassificationSplitter.

Parameters:
  • n_shapelets (int or numpy.inf, default=8) – Number of partial pivots selected at each node (numpy.inf with selection=’all’).

  • n_ts_for_selection (int, float or numpy.inf, default=100) – Number (int) or fraction (float) of the instances of the node used to select the pivots.

  • n_features_strategy (int, float, tuple or str, default=2) – Number of features of each partial pivot: an int, a fraction of the features (float), a range (min, max), or chosen from the feature importances of a random forest (‘elbow’, ‘drop’, ‘new’) or iteratively with depth-1 trees (‘iterative’).

  • selection ({'random', 'cluster', 'all'}, default='random') – How the pivots are selected among the candidates: at random, the instances closest to the k-means centroids, or all of them.

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between an instance and a partial pivot, on the features of the pivot. The data are used as they are: scale them beforehand if the features have different scales.

  • use_combination (bool, default=True) – With the strategies based on feature importances, whether to use all the combinations of the important features or only the set of all of them.

  • random_state (int, default=42) – Seed of the random choices.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PartialPivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index of the partial pivot of the split.

  • threshold_original (float) – Threshold of the split.

See also

PivotTreeStumpClassifier

pivots on all the features.

RuleTree.utils.shapelet_transform.TabularShapelets.TabularShapelets

the selection of the pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree.stumps.classification import PartialPivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PartialPivotTreeStumpClassifier(n_shapelets=10, n_features_strategy=2, random_state=0).fit(X, y)
>>> stump.get_rule(float_precision=2)["textual_rule"]
'SP0(d: Feat. 2=1.3, Feat. 3=0.2) <= 1.28'
property st#

The fitted transform of the partial pivots.

get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]#

Rule of the split, e.g. SP0(d: sepal length=5.1, petal width=0.2) <= 0.8: the distance from the partial pivot, described by the values of its features.

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 pivot are shown in the original scale.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

rule – See RuleTreeBaseStump.get_rule.

Return type:

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$') → PartialPivotTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

describe_partial_pivot(pivot, columns_names=None, scaler=None) → str[source]#

Description of a partial pivot by the values of its features, e.g. d: petal length=1.3, petal width=0.2.

Parameters:
  • pivot (numpy.ndarray of shape (n_features,)) – The partial pivot: nan for the features it does not use.

  • 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 are shown in the original scale.

Returns:

description – The features used by the pivot and their values; if it uses more than half of the features, the ones it does not use.

Return type:

str

RuleTree.stumps.classification.PartialProximityTreeStumpClassifier module#

PartialProximityTreeStumpClassifier: which of two partial pivots is closer to the instance, for classification.

class PartialProximityTreeStumpClassifier(n_shapelets=8, n_ts_for_selection=100, n_features_strategy=2, selection='random', distance='euclidean', proximity_on_same_features=True, use_combination=True, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: PartialProximityTreeStump, PartialPivotTreeStumpClassifier

Partial proximity stump for classification of tabular data: the instances closer to one partial pivot than to another one go left (euclidean(x, SP0(...)) <= euclidean(x, SP1(...))).

At each node, n_shapelets partial pivots (instances of the node restricted to a few features) are selected as in PartialPivotTreeStumpClassifier, and the split is the pair of pivots whose comparison best splits the node. In the rules, SP<i>(...) lists the features and values of the pivot.

It is a Stump made of a PartialProximityTransformer and a ClassificationSplitter.

Parameters:
  • n_shapelets (int or numpy.inf, default=8) – Number of partial pivots selected at each node (numpy.inf with selection=’all’).

  • n_ts_for_selection (int, float or numpy.inf, default=100) – Number (int) or fraction (float) of the instances of the node used to select the pivots.

  • n_features_strategy (int, float, tuple or str, default=2) – Number of features of each partial pivot: an int, a fraction of the features (float), a range (min, max), or chosen from the feature importances of a random forest (‘elbow’, ‘drop’, ‘new’) or iteratively with depth-1 trees (‘iterative’).

  • selection ({'random', 'cluster', 'all'}, default='random') – How the pivots are selected among the candidates: at random, the instances closest to the k-means centroids, or all of them.

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between an instance and a partial pivot, on the features of the pivot. The data are used as they are: scale them beforehand if the features have different scales.

  • proximity_on_same_features (bool, default=True) – Whether to compare only the partial pivots on the same features, whose distances are comparable.

  • use_combination (bool, default=True) – With the strategies based on feature importances, whether to use all the combinations of the important features or only the set of all of them.

  • random_state (int, default=42) – Seed of the random choices.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PartialProximityTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • st (TabularShapelets) – The partial pivots of the split: st.shapelets[0] and st.shapelets[1].

See also

PartialPivotTreeStumpClassifier

the distance from a single partial pivot.

ProximityTreeStumpClassifier

the same comparison with shapelets, for time series.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import PartialProximityTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PartialProximityTreeStumpClassifier(n_features_strategy=1, random_state=0).fit(X, y)
>>> stump.get_rule(columns_names=["sl", "sw", "pl", "pw"])["textual_rule"]
'euclidean(x, SP0(d: pw=1.8)) <= euclidean(x, SP1(d: pw=0.1))'
>>> model = RuleTreeClassifier(max_depth=3, base_stumps=PartialProximityTreeStumpClassifier()).fit(X, y)
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$') → PartialProximityTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.PivotTreeStumpClassifier module#

PivotTreeStumpClassifier: the split on the distance from a pivot instance, for classification.

class PivotTreeStumpClassifier(distance_measure='euclidean', k=None, reg_contrast_lambda=0.2, reg_max_thresholds=None, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: PivotTreeStump, DecisionTreeStumpClassifier

Pivot stump for classification: a threshold on the distance from a pivot, an instance of the training set (distance <= t goes left).

At each node, the candidate pivots are selected among the instances of the node: for each class, its medoid (descriptive pivot) and the instance whose distance best separates the classes (discriminative pivot). The split is the best threshold on the distance from one of the candidates. Distances are computed on the standardized numerical features; the distance matrix of the training set is computed once and cached in the tree. In the rules, P_<i> is the distance from the training instance i.

It is a Stump made of a PivotTransformer and a ClassificationSplitter; it supports only tabular data.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • k (int, default=None) – Regression only: number of nearest and farthest instances used to measure how well a candidate pivot contrasts close and distant instances. None for the square root of the number of instances of the node (between 5 and n - 1).

  • reg_contrast_lambda (float, default=0.2) – Regression only: weight of the contrast with respect to the gain of the split when the descriptive pivot is selected.

  • reg_max_thresholds (int, default=None) – Regression only: maximum number of thresholds tested on the distances from each candidate pivot (quantiles). None for max(256, sqrt(n)).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index (in the training set) of the pivot of the split.

  • threshold_original (float) – Threshold of the split.

  • X_split_instance (numpy.ndarray) – The pivot, as standardized numerical features.

See also

PivotTreeStumpRegressor

the same stump for regression.

PivotTransformer

the selection of the pivots and the distances.

PivotTreeStump

the methods shared by the pivot stumps, e.g. get_pivots.

Notes

The parameters of the split (criterion, min_samples_leaf, …) are also used by the decision trees that select the discriminative pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import PivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]  # distance from the training instance 112
'P_112 <= 3.692'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=PivotTreeStumpClassifier(distance_measure="cosine"))
>>> model = model.fit(X, y)
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$') → PivotTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.ProximityTreeStumpClassifier module#

ProximityTreeStumpClassifier: which of two shapelets is closer to the time series, for classification.

class ProximityTreeStumpClassifier(n_shapelets=8, n_shapelets_for_selection=500, n_ts_for_selection_per_class=100, sliding_window=50, selection='mi_clf', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: ProximityTreeStump, ShapeletTreeStumpClassifier

Proximity stump for classification of time series: the time series closer to one shapelet than to another one go left (euclidean(TS, green_shp) <= euclidean(TS, red_shp)).

At each node, n_shapelets shapelets are selected as in ShapeletTreeStumpClassifier, and the split is the pair of shapelets whose comparison best splits the node. Unlike the shapelet stump, the rule has no threshold: it only says which shapelet the time series resembles more. In the Graphviz drawing, the first shapelet is green and the second one red.

It is a Stump made of a ProximityTransformer and a ClassificationSplitter. The time series have shape (n_samples, n_signals, length) and are passed to the trees as X_ts.

Parameters:
  • n_shapelets (int, default=8) – Number of shapelets selected at each node (at least 2).

  • n_shapelets_for_selection (int, numpy.inf or 'stratified', default=500) – Number of candidate subsequences extracted from the time series, among which the shapelets are selected. ‘stratified’ draws them according to the class distribution.

  • n_ts_for_selection_per_class (int or numpy.inf, default=100) – Number of time series of each class from which the candidates are extracted.

  • sliding_window (int, default=50) – Length of the shapelets.

  • selection ({'random', 'mi_clf', 'cluster'}, default='mi_clf') – How the shapelets are selected among the candidates: at random, by mutual information with the target, or as the medoids of a k-medoids clustering (requires scikit-learn-extra).

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between a shapelet and a subsequence; the distance of a time series from a shapelet is the minimum over the positions of the shapelet.

  • mi_n_neighbors (int, default=100) – Number of neighbors used to estimate the mutual information.

  • random_state (int, default=42) – Seed of the random choices of the candidates and of the shapelets.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ProximityTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • st (Shapelets) – The shapelets of the split: st.shapelets[0] (green) and st.shapelets[1] (red).

See also

ProximityTreeStumpRegressor

the same stump for regression.

ShapeletTreeStumpClassifier

the distance from a single shapelet.

Examples

>>> import numpy as np
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ProximityTreeStumpClassifier
>>> rng = np.random.default_rng(0)
>>> X_ts = rng.normal(scale=0.3, size=(60, 1, 30))  # 60 time series of length 30
>>> y = rng.integers(0, 2, size=60)
>>> X_ts[y == 1, 0, 10:20] += 2  # a bump for the class 1
>>> stump = ProximityTreeStumpClassifier(n_shapelets=4, sliding_window=10, selection="random")
>>> model = RuleTreeClassifier(max_depth=1, base_stumps=stump).fit(X_ts=X_ts, y=y)
>>> model.get_rules()["textual_rule"]
'euclidean(TS, green_shp) <= euclidean(TS, red_shp)'
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$') → ProximityTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

RuleTree.stumps.classification.ShapeletTreeStumpClassifier module#

ShapeletTreeStumpClassifier: the split on the distance from a shapelet, for classification of time series.

class ShapeletTreeStumpClassifier(n_shapelets=8, n_shapelets_for_selection=500, n_ts_for_selection_per_class=100, sliding_window=50, selection='mi_clf', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]#

Bases: DecisionTreeStumpClassifier

Shapelet stump for classification of time series: a threshold on the distance of the time series from a shapelet (distance <= t goes left).

A shapelet is a subsequence of one of the time series of the node; the distance of a time series from it is the smallest distance between the shapelet and the subsequences of the series, so it does not depend on where the pattern occurs. At each node, n_shapelets shapelets are selected among candidate subsequences, and the split is the best threshold on the distance from one of them. In the rules, euclidean(TS, shp) is the distance from the shapelet.

It is a Stump made of a ShapeletTransformer and a ClassificationSplitter. The time series have shape (n_samples, n_signals, length) and are passed to the trees as X_ts.

Parameters:
  • n_shapelets (int, default=8) – Number of shapelets selected at each node.

  • n_shapelets_for_selection (int, numpy.inf or 'stratified', default=500) – Number of candidate subsequences extracted from the time series, among which the shapelets are selected. ‘stratified’ draws them according to the class distribution.

  • n_ts_for_selection_per_class (int or numpy.inf, default=100) – Number of time series of each class from which the candidates are extracted.

  • sliding_window (int, default=50) – Length of the shapelets.

  • selection ({'random', 'mi_clf', 'cluster'}, default='mi_clf') – How the shapelets are selected among the candidates: at random, by mutual information with the target, or as the medoids of a k-medoids clustering (requires scikit-learn-extra).

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between a shapelet and a subsequence; the distance of a time series from a shapelet is the minimum over the positions of the shapelet.

  • mi_n_neighbors (int, default=100) – Number of neighbors used to estimate the mutual information.

  • random_state (int, default=42) – Seed of the random choices of the candidates and of the shapelets.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ShapeletTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index of the shapelet of the split among the selected ones.

  • threshold_original (float) – Threshold of the split.

See also

ShapeletTreeStumpRegressor

the same stump for regression.

RuleTree.utils.shapelet_transform.Shapelets.Shapelets

the selection of the shapelets.

Examples

>>> import numpy as np
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ShapeletTreeStumpClassifier
>>> rng = np.random.default_rng(0)
>>> X_ts = rng.normal(size=(60, 1, 30))  # 60 time series of length 30
>>> y = rng.integers(0, 2, size=60)
>>> stump = ShapeletTreeStumpClassifier(n_shapelets=5, sliding_window=10, selection="random")
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=stump).fit(X_ts=X_ts, y=y)
>>> model.predict(X_ts=X_ts).shape
(60,)
property st#

The fitted shapelet transform.

get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]#

Rule of the split, e.g. euclidean(TS, shp) <= 1.2.

Parameters:
  • columns_names (list of str, default=None) – Not used.

  • scaler (TransformerMixin, default=None) – Not supported: must be None.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

rule – See RuleTreeBaseStump.get_rule.

Return type:

dict

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 shapelet, drawn into a temporary PNG file (requires matplotlib).

Parameters:
  • columns_names (list of str, default=None) – Not used.

  • scaler (TransformerMixin, default=None) – Not supported: must be None.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

attributes – Attributes of the Graphviz node.

Return type:

dict

Raises:

MissingOptionalDependencyError – If matplotlib is not installed.

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$') → ShapeletTreeStumpClassifier#

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

Module contents#

class DecisionTreeStumpClassifier(criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: Stump

Decision tree stump for classification: a threshold on a numerical feature (x <= t) or a value of a categorical feature (x == v), chosen to maximize the information gain.

It works on tabular data, time series and images; time series and images are flattened into one feature per value. It is a Stump made of a MatrixTransformer and a ClassificationSplitter, and the default stump of RuleTreeClassifier.

Parameters:
  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MatrixTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Column of the data used by the split.

  • threshold_original (float or object) – Threshold of a numerical split, or value of a categorical split.

See also

DecisionTreeStumpRegressor

the same stump for regression.

sklearn.tree.DecisionTreeClassifier

the tree used to split the numerical features.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import DecisionTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = DecisionTreeStumpClassifier(criterion="entropy", random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]
'X_3 <= 0.8'
>>> stump.apply(X[[0, 100]])  # 1: left child, 2: right child
array([1, 2])

As base stump of a tree:

>>> model = RuleTreeClassifier(max_depth=2, base_stumps=DecisionTreeStumpClassifier(min_samples_leaf=5))
>>> model = model.fit(X, y)
get_rule(columns_names=None, scaler=None, float_precision=3)[source]

Rule of the split, e.g. petal length <= 2.45 or color = red.

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, graphviz_rule) and its negation (not_textual_rule, not_blob_rule, not_graphviz_rule).

Return type:

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$') → DecisionTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class PivotTreeStumpClassifier(distance_measure='euclidean', k=None, reg_contrast_lambda=0.2, reg_max_thresholds=None, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: PivotTreeStump, DecisionTreeStumpClassifier

Pivot stump for classification: a threshold on the distance from a pivot, an instance of the training set (distance <= t goes left).

At each node, the candidate pivots are selected among the instances of the node: for each class, its medoid (descriptive pivot) and the instance whose distance best separates the classes (discriminative pivot). The split is the best threshold on the distance from one of the candidates. Distances are computed on the standardized numerical features; the distance matrix of the training set is computed once and cached in the tree. In the rules, P_<i> is the distance from the training instance i.

It is a Stump made of a PivotTransformer and a ClassificationSplitter; it supports only tabular data.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • k (int, default=None) – Regression only: number of nearest and farthest instances used to measure how well a candidate pivot contrasts close and distant instances. None for the square root of the number of instances of the node (between 5 and n - 1).

  • reg_contrast_lambda (float, default=0.2) – Regression only: weight of the contrast with respect to the gain of the split when the descriptive pivot is selected.

  • reg_max_thresholds (int, default=None) – Regression only: maximum number of thresholds tested on the distances from each candidate pivot (quantiles). None for max(256, sqrt(n)).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index (in the training set) of the pivot of the split.

  • threshold_original (float) – Threshold of the split.

  • X_split_instance (numpy.ndarray) – The pivot, as standardized numerical features.

See also

PivotTreeStumpRegressor

the same stump for regression.

PivotTransformer

the selection of the pivots and the distances.

PivotTreeStump

the methods shared by the pivot stumps, e.g. get_pivots.

Notes

The parameters of the split (criterion, min_samples_leaf, …) are also used by the decision trees that select the discriminative pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import PivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]  # distance from the training instance 112
'P_112 <= 3.692'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=PivotTreeStumpClassifier(distance_measure="cosine"))
>>> model = model.fit(X, y)
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$') → PivotTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class ShapeletTreeStumpClassifier(n_shapelets=8, n_shapelets_for_selection=500, n_ts_for_selection_per_class=100, sliding_window=50, selection='mi_clf', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: DecisionTreeStumpClassifier

Shapelet stump for classification of time series: a threshold on the distance of the time series from a shapelet (distance <= t goes left).

A shapelet is a subsequence of one of the time series of the node; the distance of a time series from it is the smallest distance between the shapelet and the subsequences of the series, so it does not depend on where the pattern occurs. At each node, n_shapelets shapelets are selected among candidate subsequences, and the split is the best threshold on the distance from one of them. In the rules, euclidean(TS, shp) is the distance from the shapelet.

It is a Stump made of a ShapeletTransformer and a ClassificationSplitter. The time series have shape (n_samples, n_signals, length) and are passed to the trees as X_ts.

Parameters:
  • n_shapelets (int, default=8) – Number of shapelets selected at each node.

  • n_shapelets_for_selection (int, numpy.inf or 'stratified', default=500) – Number of candidate subsequences extracted from the time series, among which the shapelets are selected. ‘stratified’ draws them according to the class distribution.

  • n_ts_for_selection_per_class (int or numpy.inf, default=100) – Number of time series of each class from which the candidates are extracted.

  • sliding_window (int, default=50) – Length of the shapelets.

  • selection ({'random', 'mi_clf', 'cluster'}, default='mi_clf') – How the shapelets are selected among the candidates: at random, by mutual information with the target, or as the medoids of a k-medoids clustering (requires scikit-learn-extra).

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between a shapelet and a subsequence; the distance of a time series from a shapelet is the minimum over the positions of the shapelet.

  • mi_n_neighbors (int, default=100) – Number of neighbors used to estimate the mutual information.

  • random_state (int, default=42) – Seed of the random choices of the candidates and of the shapelets.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ShapeletTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index of the shapelet of the split among the selected ones.

  • threshold_original (float) – Threshold of the split.

See also

ShapeletTreeStumpRegressor

the same stump for regression.

RuleTree.utils.shapelet_transform.Shapelets.Shapelets

the selection of the shapelets.

Examples

>>> import numpy as np
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ShapeletTreeStumpClassifier
>>> rng = np.random.default_rng(0)
>>> X_ts = rng.normal(size=(60, 1, 30))  # 60 time series of length 30
>>> y = rng.integers(0, 2, size=60)
>>> stump = ShapeletTreeStumpClassifier(n_shapelets=5, sliding_window=10, selection="random")
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=stump).fit(X_ts=X_ts, y=y)
>>> model.predict(X_ts=X_ts).shape
(60,)
property st

The fitted shapelet transform.

get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]

Rule of the split, e.g. euclidean(TS, shp) <= 1.2.

Parameters:
  • columns_names (list of str, default=None) – Not used.

  • scaler (TransformerMixin, default=None) – Not supported: must be None.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

rule – See RuleTreeBaseStump.get_rule.

Return type:

dict

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 shapelet, drawn into a temporary PNG file (requires matplotlib).

Parameters:
  • columns_names (list of str, default=None) – Not used.

  • scaler (TransformerMixin, default=None) – Not supported: must be None.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

attributes – Attributes of the Graphviz node.

Return type:

dict

Raises:

MissingOptionalDependencyError – If matplotlib is not installed.

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$') → ShapeletTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class PartialPivotTreeStumpClassifier(n_shapelets=8, n_ts_for_selection=100, n_features_strategy=2, selection='random', distance='euclidean', use_combination=True, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: DecisionTreeStumpClassifier

Partial pivot stump for classification of tabular data: a threshold on the distance from a partial pivot, an instance of the node restricted to a few features (distance <= t goes left).

At each node, n_shapelets partial pivots are selected among the instances of the node, each on a subset of the features (n_features_strategy), and the split is the best threshold on the distance from one of them. The distance is computed only on the features of the pivot, so the rule involves a few features. In the rules, SP<i>(...) lists the features and values of the pivot.

It is a Stump made of a PartialPivotTransformer and a ClassificationSplitter.

Parameters:
  • n_shapelets (int or numpy.inf, default=8) – Number of partial pivots selected at each node (numpy.inf with selection=’all’).

  • n_ts_for_selection (int, float or numpy.inf, default=100) – Number (int) or fraction (float) of the instances of the node used to select the pivots.

  • n_features_strategy (int, float, tuple or str, default=2) – Number of features of each partial pivot: an int, a fraction of the features (float), a range (min, max), or chosen from the feature importances of a random forest (‘elbow’, ‘drop’, ‘new’) or iteratively with depth-1 trees (‘iterative’).

  • selection ({'random', 'cluster', 'all'}, default='random') – How the pivots are selected among the candidates: at random, the instances closest to the k-means centroids, or all of them.

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between an instance and a partial pivot, on the features of the pivot. The data are used as they are: scale them beforehand if the features have different scales.

  • use_combination (bool, default=True) – With the strategies based on feature importances, whether to use all the combinations of the important features or only the set of all of them.

  • random_state (int, default=42) – Seed of the random choices.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PartialPivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (int) – Index of the partial pivot of the split.

  • threshold_original (float) – Threshold of the split.

See also

PivotTreeStumpClassifier

pivots on all the features.

RuleTree.utils.shapelet_transform.TabularShapelets.TabularShapelets

the selection of the pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree.stumps.classification import PartialPivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PartialPivotTreeStumpClassifier(n_shapelets=10, n_features_strategy=2, random_state=0).fit(X, y)
>>> stump.get_rule(float_precision=2)["textual_rule"]
'SP0(d: Feat. 2=1.3, Feat. 3=0.2) <= 1.28'
property st

The fitted transform of the partial pivots.

get_rule(columns_names=None, scaler=None, float_precision: int | None = 3)[source]

Rule of the split, e.g. SP0(d: sepal length=5.1, petal width=0.2) <= 0.8: the distance from the partial pivot, described by the values of its features.

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 pivot are shown in the original scale.

  • float_precision (int, default=3) – Number of decimals of the threshold. If None, it is not rounded.

Returns:

rule – See RuleTreeBaseStump.get_rule.

Return type:

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$') → PartialPivotTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class ObliqueDecisionTreeStumpClassifier(oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: ObliqueTreeStump, DecisionTreeStumpClassifier

Oblique stump for classification of tabular data: a threshold on a linear combination of a few numerical features (0.8 * X_2 - 0.6 * X_3 <= t goes left), i.e. a hyperplane that is not parallel to the axes.

The directions are chosen at each node in one of two ways (oblique_split_type):

  • 'householder': the axes reflected by the Householder reflection that maps the principal component of the node onto an axis; each direction combines at most max_oblique_features features (see HouseholderTransformer);

  • 'bivariate': n_orientations rotations of the best pair of features (see BivariateTransformer).

The combinations are computed on the numerical features as they are: scale the data beforehand if the features have different scales. It is a Stump made of an oblique transformer and a ClassificationSplitter.

Parameters:
  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions are chosen.

  • max_oblique_features (int, default=2) – Householder only: maximum number of features in the reflection vector.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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).

  • min_weight_fraction_leaf (float, default=0.0) – Minimum fraction of the sample weights of the node in each child.

  • max_features (int, float, {'sqrt', 'log2'} or None, default=None) – Number of directions considered for the split, drawn at random; None for all.

  • random_state (int, RandomState instance or None, default=None) – Seed of the PCA (Householder), of the choice of the directions (max_features) and of the ties between directions with the same gain. With None, a tree gives the stump its own random_state.

  • min_impurity_decrease (float, default=0.0) – A split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:

See also

ObliqueDecisionTreeStumpRegressor

the same stump for regression.

DecisionTreeStumpClassifier

the axis-parallel splits.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ObliqueDecisionTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = ObliqueDecisionTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule(columns_names=["sl", "sw", "pl", "pw"])["textual_rule"]
'-0.085 * sw + 0.857 * pl <= 1.85'
>>> stump.feature_original, [round(c, 3) for c in stump.coefficients]
((1, 2), [-0.085, 0.857])
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=ObliqueDecisionTreeStumpClassifier("bivariate"))
>>> model = model.fit(X, y)
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$') → ObliqueDecisionTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class ProximityTreeStumpClassifier(n_shapelets=8, n_shapelets_for_selection=500, n_ts_for_selection_per_class=100, sliding_window=50, selection='mi_clf', distance='euclidean', mi_n_neighbors=100, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: ProximityTreeStump, ShapeletTreeStumpClassifier

Proximity stump for classification of time series: the time series closer to one shapelet than to another one go left (euclidean(TS, green_shp) <= euclidean(TS, red_shp)).

At each node, n_shapelets shapelets are selected as in ShapeletTreeStumpClassifier, and the split is the pair of shapelets whose comparison best splits the node. Unlike the shapelet stump, the rule has no threshold: it only says which shapelet the time series resembles more. In the Graphviz drawing, the first shapelet is green and the second one red.

It is a Stump made of a ProximityTransformer and a ClassificationSplitter. The time series have shape (n_samples, n_signals, length) and are passed to the trees as X_ts.

Parameters:
  • n_shapelets (int, default=8) – Number of shapelets selected at each node (at least 2).

  • n_shapelets_for_selection (int, numpy.inf or 'stratified', default=500) – Number of candidate subsequences extracted from the time series, among which the shapelets are selected. ‘stratified’ draws them according to the class distribution.

  • n_ts_for_selection_per_class (int or numpy.inf, default=100) – Number of time series of each class from which the candidates are extracted.

  • sliding_window (int, default=50) – Length of the shapelets.

  • selection ({'random', 'mi_clf', 'cluster'}, default='mi_clf') – How the shapelets are selected among the candidates: at random, by mutual information with the target, or as the medoids of a k-medoids clustering (requires scikit-learn-extra).

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between a shapelet and a subsequence; the distance of a time series from a shapelet is the minimum over the positions of the shapelet.

  • mi_n_neighbors (int, default=100) – Number of neighbors used to estimate the mutual information.

  • random_state (int, default=42) – Seed of the random choices of the candidates and of the shapelets.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ProximityTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • st (Shapelets) – The shapelets of the split: st.shapelets[0] (green) and st.shapelets[1] (red).

See also

ProximityTreeStumpRegressor

the same stump for regression.

ShapeletTreeStumpClassifier

the distance from a single shapelet.

Examples

>>> import numpy as np
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ProximityTreeStumpClassifier
>>> rng = np.random.default_rng(0)
>>> X_ts = rng.normal(scale=0.3, size=(60, 1, 30))  # 60 time series of length 30
>>> y = rng.integers(0, 2, size=60)
>>> X_ts[y == 1, 0, 10:20] += 2  # a bump for the class 1
>>> stump = ProximityTreeStumpClassifier(n_shapelets=4, sliding_window=10, selection="random")
>>> model = RuleTreeClassifier(max_depth=1, base_stumps=stump).fit(X_ts=X_ts, y=y)
>>> model.get_rules()["textual_rule"]
'euclidean(TS, green_shp) <= euclidean(TS, red_shp)'
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$') → ProximityTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class PartialProximityTreeStumpClassifier(n_shapelets=8, n_ts_for_selection=100, n_features_strategy=2, selection='random', distance='euclidean', proximity_on_same_features=True, use_combination=True, random_state=42, n_jobs=1, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: PartialProximityTreeStump, PartialPivotTreeStumpClassifier

Partial proximity stump for classification of tabular data: the instances closer to one partial pivot than to another one go left (euclidean(x, SP0(...)) <= euclidean(x, SP1(...))).

At each node, n_shapelets partial pivots (instances of the node restricted to a few features) are selected as in PartialPivotTreeStumpClassifier, and the split is the pair of pivots whose comparison best splits the node. In the rules, SP<i>(...) lists the features and values of the pivot.

It is a Stump made of a PartialProximityTransformer and a ClassificationSplitter.

Parameters:
  • n_shapelets (int or numpy.inf, default=8) – Number of partial pivots selected at each node (numpy.inf with selection=’all’).

  • n_ts_for_selection (int, float or numpy.inf, default=100) – Number (int) or fraction (float) of the instances of the node used to select the pivots.

  • n_features_strategy (int, float, tuple or str, default=2) – Number of features of each partial pivot: an int, a fraction of the features (float), a range (min, max), or chosen from the feature importances of a random forest (‘elbow’, ‘drop’, ‘new’) or iteratively with depth-1 trees (‘iterative’).

  • selection ({'random', 'cluster', 'all'}, default='random') – How the pivots are selected among the candidates: at random, the instances closest to the k-means centroids, or all of them.

  • distance ({'euclidean', 'sqeuclidean', 'cosine', 'cityblock'} or callable, default='euclidean') – Distance between an instance and a partial pivot, on the features of the pivot. The data are used as they are: scale them beforehand if the features have different scales.

  • proximity_on_same_features (bool, default=True) – Whether to compare only the partial pivots on the same features, whose distances are comparable.

  • use_combination (bool, default=True) – With the strategies based on feature importances, whether to use all the combinations of the important features or only the set of all of them.

  • random_state (int, default=42) – Seed of the random choices.

  • n_jobs (int, default=1) – Number of threads used to compute the distances (-1: all the CPUs).

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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.

  • min_impurity_decrease (float, default=0.0) – A numerical split is kept only if it decreases the weighted impurity by at least this value.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (PartialProximityTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • st (TabularShapelets) – The partial pivots of the split: st.shapelets[0] and st.shapelets[1].

See also

PartialPivotTreeStumpClassifier

the distance from a single partial pivot.

ProximityTreeStumpClassifier

the same comparison with shapelets, for time series.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import PartialProximityTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = PartialProximityTreeStumpClassifier(n_features_strategy=1, random_state=0).fit(X, y)
>>> stump.get_rule(columns_names=["sl", "sw", "pl", "pw"])["textual_rule"]
'euclidean(x, SP0(d: pw=1.8)) <= euclidean(x, SP1(d: pw=0.1))'
>>> model = RuleTreeClassifier(max_depth=3, base_stumps=PartialProximityTreeStumpClassifier()).fit(X, y)
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$') → PartialProximityTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class MultiplePivotTreeStumpClassifier(distance_measure='euclidean', criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: MultiplePivotTreeStump, DecisionTreeStumpClassifier

Multiple pivot stump for classification of tabular data: the instances closer to one pivot than to another one go left (P_a <= P_b, where P_i is the distance from the training instance i).

At each node, the candidate pivots are selected as in PivotTreeStumpClassifier (for each class, its medoid and its most discriminative instance), and the split is the pair of pivots of different classes whose comparison best splits the node. Distances are computed on the standardized numerical features; the distance matrix of the training set is computed once and cached in the tree.

It is a Stump made of a MultiplePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MultiplePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the two pivots of the split.

See also

PivotTreeStumpClassifier

a threshold on the distance from a single pivot.

MultipleObliquePivotTreeStumpClassifier

the same comparison, with pivots found by oblique splits.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import MultiplePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = MultiplePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]  # closer to the training instance a than to b
'P_27 <= P_77'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=MultiplePivotTreeStumpClassifier()).fit(X, y)
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$') → MultiplePivotTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class ObliquePivotTreeStumpClassifier(distance_measure='euclidean', oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: ObliquePivotTreeStump, DecisionTreeStumpClassifier

Oblique pivot stump for classification of tabular data: a threshold on a linear combination of the distances from a few pivots (0.7 * P_12 - 0.3 * P_57 <= t goes left, where P_i is the distance from the training instance i).

At each node, the candidate pivots are, for each class, its medoid and the instances combined by an oblique direction on the distances (oblique_split_type); then the distances of the instances of the node from the candidates are combined by oblique directions of the same type, and the split is the best threshold on one of them (see ObliquePivotTransformer). Distances are computed on the standardized numerical features.

It is a Stump made of an ObliquePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions are chosen (see ObliqueDecisionTreeStumpClassifier).

  • max_oblique_features (int, default=2) – Householder only: maximum number of pivots combined by a direction.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (ObliquePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the pivots combined by the split.

  • coefficients (list of float) – Their coefficients.

See also

PivotTreeStumpClassifier

a threshold on the distance from a single pivot.

ObliqueDecisionTreeStumpClassifier

oblique splits on the features.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import ObliquePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = ObliquePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_rule()["textual_rule"]
'-0.447 * P_22 + 0.468 * P_118 <= 0.955'
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=ObliquePivotTreeStumpClassifier("cosine")).fit(X, y)
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$') → ObliquePivotTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object

class MultipleObliquePivotTreeStumpClassifier(distance_measure='euclidean', oblique_split_type='householder', max_oblique_features=2, tau=0.0001, n_orientations=10, criterion='gini', min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, min_impurity_decrease=0.0, class_weight=None)[source]

Bases: MultiplePivotTreeStump, DecisionTreeStumpClassifier

Multiple oblique pivot stump for classification of tabular data: as MultiplePivotTreeStumpClassifier (the instances closer to one pivot than to another one go left, P_a <= P_b), but the discriminative pivots of each class are the instances combined by an oblique direction on the distances (oblique_split_type), instead of the root split of a decision tree: up to max_oblique_features (Householder) or two (bivariate) for each class.

It is a Stump made of a MultiplePivotTransformer and a ClassificationSplitter.

Parameters:
  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized numerical features: any metric of sklearn.metrics.pairwise_distances.

  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – How the oblique directions that find the discriminative pivots are chosen (see ObliqueDecisionTreeStumpClassifier).

  • max_oblique_features (int, default=2) – Householder only: maximum number of pivots combined by a direction.

  • tau (float, default=1e-4) – Householder only: if the principal component is closer than tau to every axis, the directions are the axes.

  • n_orientations (int, default=10) – Bivariate only: number of angles, evenly spaced in [0, 180) degrees.

  • criterion ({'gini', 'entropy'}, default='gini') – Impurity measure.

  • 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 own random_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.

  • class_weight (dict, 'balanced' or None, default=None) – Weights of the classes in the impurity; ‘balanced’ weighs them inversely to their frequency.

Variables:
  • transformer (MultiplePivotTransformer) – The fitted transformer.

  • splitter (ClassificationSplitter) – The fitted splitter (feature, threshold, gain and impurity of the split).

  • data_type (str) – Data type the stump was fitted on.

  • feature_original (tuple of int) – Indices (in the training set) of the two pivots of the split.

See also

MultiplePivotTreeStumpClassifier

the discriminative pivots found by a decision tree.

ObliquePivotTreeStumpClassifier

an oblique split on the distances from the pivots.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree import RuleTreeClassifier
>>> from RuleTree.stumps.classification import MultipleObliquePivotTreeStumpClassifier
>>> X, y = load_iris(return_X_y=True)
>>> stump = MultipleObliquePivotTreeStumpClassifier(random_state=0).fit(X, y)
>>> stump.get_pivots()["discriminatives"]  # up to 2 for each class
[15, 22, 83, 98, 106, 118]
>>> model = RuleTreeClassifier(max_depth=2, base_stumps=MultipleObliquePivotTreeStumpClassifier()).fit(X, y)
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$') → MultipleObliquePivotTreeStumpClassifier

Configure whether metadata should be requested to be passed to the fit method.

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 (see sklearn.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 to fit if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to fit.

  • 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_input parameter in fit.

  • context (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for context parameter in fit.

  • data (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data parameter in fit.

  • data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for data_type parameter in fit.

  • idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for idx parameter in fit.

  • sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in fit.

Returns:

self – The updated object.

Return type:

object