ProximityPivotTransformer#

class ProximityPivotTransformer(ml_task='clf', distance_measure='euclidean', k=None, reg_contrast_lambda=0.2, reg_max_thresholds=None, tree_params=None, discriminative='tree', max_oblique_features=2, tau=0.0001, n_orientations=10, standardize=True, embedder=None)[source]#

Bases: PivotTransformer

Transformer of tabular data, time series and images into pivot comparisons: for each pair of candidate pivots, whether the instance is closer to the second one than to the first.

The candidate pivots are selected as in PivotTransformer (with discriminative, also with oblique directions); for classification, only the pairs of pivots of different classes are compared. The feature matrix has one 0/1 column for each pair (see closer_to_second()). After the splitter has chosen a pair, only its two pivots are kept (select).

Parameters:
  • ml_task ({MODEL_TYPE_CLF, MODEL_TYPE_REG}, default=MODEL_TYPE_CLF) – Task of the stump, which determines how the pivots are selected and compared.

  • distance_measure (str or callable, default='euclidean') – Distance between the instances, computed on the standardized features (categorical ones one-hot encoded).

  • k – Regression only: see PivotTransformer.

  • reg_contrast_lambda – Regression only: see PivotTransformer.

  • reg_max_thresholds – Regression only: see PivotTransformer.

  • tree_params (dict, default=None) – Parameters of the decision trees that select the discriminative pivots.

  • discriminative ({'tree', 'householder', 'bivariate'}, default='tree') – How the discriminative pivots are found (see PivotSelection).

  • max_oblique_features (int, default=2) – Householder only: maximum number of discriminative pivots of each class.

  • tau (float, default=1e-4) – Householder only: threshold of the reflection.

  • n_orientations (int, default=10) – Bivariate only: number of angles.

  • standardize (bool, default=True) – Whether the features are standardized before the distances (see PivotTransformer).

  • embedder (transformer, default=None) – Transformer of the data into vectors, applied before the distances (see PivotTransformer).

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

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

  • candidates (numpy.ndarray of shape (n_candidates, n_numerical_features)) – The candidate pivots (the two of the chosen pair after select), standardized.

  • candidate_names (numpy.ndarray of shape (n_candidates,)) – Indices of the candidate pivots in the training set.

  • pairs (list of tuple) – Pairs of candidates of the columns of the feature matrix.

  • pivots (dict) – Indices in the training set of the descriptives, discriminatives and candidates pivots of the node.

  • scaler (StandardScaler) – Scaler of the encoded features, fitted on the whole data set.

See also

ProximityPivotTreeStumpClassifier, ObliqueProximityPivotTreeStumpClassifier

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree.stumps.transformers import ProximityPivotTransformer
>>> X, y = load_iris(return_X_y=True)
>>> transformer = ProximityPivotTransformer(tree_params={"random_state": 0})
>>> transformer = transformer.fit(X, y, context=None, node_id=None, cache=None)
>>> len(transformer.candidate_names_), len(transformer.pairs_)  # pairs of pivots of different classes
(6, 12)
fit(X, y, idx=None, data_type=None, sample_weight=None, *, context, node_id, cache)[source]#

Select the candidate pivots of the node (see PivotTransformer.fit) and the pairs to compare.

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:

ProximityPivotTransformer

Raises:

NoSplitFoundWarning – If the data have no numerical feature or there is no pair of pivots to compare.

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

Pivot comparisons of each instance for each pair of candidates (for the chosen pair, after select).

Parameters:
  • X (numpy.ndarray) – Data of the transformer’s type (time series and images are flattened).

  • 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 – 1 if the instance is closer to the second pivot of the pair, 0 otherwise.

Return type:

numpy.ndarray of shape (n_samples, n_pairs)

select(feature)[source]#

Keep only the two pivots of the chosen pair.

Parameters:

feature (int) – Column chosen by the splitter.

Returns:

feature – Always 0: the comparison of the only pair kept.

Return type:

int

feature_id(feature)[source]#

Identifier of a pair: the indices of its two pivots in the training set.

Parameters:

feature (int) – Column of the feature matrix.

Returns:

feature_id

Return type:

tuple of int

get_state()[source]#

Fitted state: the two pivots and their indices, the candidates, the features, the categories and the scaler.

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:

ProximityPivotTransformer