PartialPivotTransformer#

class PartialPivotTransformer(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)[source]#

Bases: RuleTreeBaseTransformer

Transformer of tabular data into the distances from partial pivots, instances of the node restricted to subsets of the features.

The distance from a partial pivot is computed only on its features. After the splitter has chosen a column, only the chosen pivot is kept (select), and the splitter is fitted again on its distances (refit_after_select).

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

Variables:
  • numerical (list of int) – Numerical columns of the feature matrix.

  • categorical (list of int) – Categorical columns of the feature matrix.

  • st (TabularShapelets) – The fitted transform, with the partial pivots.

See also

PartialPivotTreeStumpClassifier

the stump that uses it.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree.stumps.transformers import PartialPivotTransformer
>>> X, y = load_iris(return_X_y=True)
>>> transformer = PartialPivotTransformer(n_shapelets=4, random_state=0)
>>> transformer.fit(X, y, context=None, node_id=None, cache=None).transform(X).shape
(150, 4)
supported_data_types = ('tabular',)#
refit_after_select = True#
fit(X, y, idx=None, data_type=None, sample_weight=None, *, context, node_id, cache)[source]#

Select the partial pivots among the instances in the node.

Parameters:
  • X (numpy.ndarray) – Data of the transformer’s type, all the instances.

  • y (numpy.ndarray) – Target, all the instances.

  • idx (numpy.ndarray, default=None) – Indices of the instances in the node. If None, all the instances.

  • data_type (str, default=None) – Type of X, used as key of the cached values.

  • sample_weight (numpy.ndarray, default=None) – Sample weights, all the instances.

  • context (RuleTree or None) – Tree being built, None outside a tree.

  • node_id (str or None) – Identifier of the node, e.g. "Rl" (context.get_node_by_id(node_id)), None outside a tree.

  • cache (dict or None) – Cache of the tree (namespace -> dictionary of values), None outside a tree: the component decides whether and how to use it (the ones of the library use self._get_cache(cache)).

Returns:

self

Return type:

PartialPivotTransformer

Warns:

Warning – If sample_weight is given: it is not supported and ignored.

transform(X, context=None, node_id=None, cache=None)[source]#

Distance of each instance from each partial pivot (from the chosen one, after select).

Parameters:
  • X (numpy.ndarray) – Tabular data.

  • context (RuleTree, default=None) – The tree, None outside a tree.

  • node_id (str, default=None) – Identifier of the node of the stump, None outside a tree.

  • cache (dict, default=None) – Cache of the tree, when called by it; None otherwise.

Returns:

X_transformed

Return type:

numpy.ndarray of shape (n_samples, n_pivots)

select(feature)[source]#

Keep only the chosen partial pivot.

Parameters:

feature (int) – Column chosen by the splitter.

Returns:

feature – Always 0: the distance from the only partial pivot kept.

Return type:

int

get_state()[source]#

Fitted state: the partial pivots.

Returns:

state – JSON-friendly dictionary, restored by set_state.

Return type:

dict

set_state(state)[source]#

Restore the fitted state saved by get_state.

Parameters:

state (dict) – Dictionary created by get_state.

Returns:

self

Return type:

PartialPivotTransformer