ObliquePivotTransformer#

class ObliquePivotTransformer(ml_task='clf', distance_measure='euclidean', k=None, reg_contrast_lambda=0.2, reg_max_thresholds=None, tree_params=None, oblique_split_type='householder', 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 oblique directions on the distances from the candidate pivots, e.g. 0.7 * P_12 - 0.3 * P_57 where P_i is the distance from the training instance i.

The candidate pivots are selected as in PivotTransformer, but the discriminative ones are the instances combined by an oblique direction (oblique_split_type, see PivotSelection). Then the distances of the instances of the node from the candidates are transformed by an oblique transformer of the same type (HouseholderTransformer or BivariateTransformer), whose directions are the columns of the feature matrix. After the splitter has chosen a column, only its direction is kept (select); the candidates are all kept.

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

  • 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 pivots and the directions.

  • oblique_split_type ({'householder', 'bivariate'}, default='householder') – Type of the oblique directions.

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

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

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

  • oblique (HouseholderTransformer or BivariateTransformer) – The oblique transformer of the distances from the candidates.

  • 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

ObliquePivotTreeStumpClassifier

the stump that uses it.

Examples

>>> from sklearn.datasets import load_iris
>>> from RuleTree.stumps.transformers import ObliquePivotTransformer
>>> X, y = load_iris(return_X_y=True)
>>> transformer = ObliquePivotTransformer(tree_params={"random_state": 0})
>>> transformer = transformer.fit(X, y, context=None, node_id=None, cache=None)
>>> transformer.transform(X).shape[1] == len(transformer.candidate_names_)  # one direction per candidate
True
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), then the oblique directions on the distances of the instances of the node from them.

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:

ObliquePivotTransformer

Raises:

NoSplitFoundWarning – If the data have no numerical feature, or (bivariate) there are fewer than two candidates.

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

Projection of the distances from the candidates on the oblique directions (on the chosen one, 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

Return type:

numpy.ndarray of shape (n_samples, n_directions)

select(feature)[source]#

Keep only the chosen direction.

Parameters:

feature (int) – Column chosen by the splitter.

Returns:

feature – Always 0: the projection on the only direction kept.

Return type:

int

combination(feature)[source]#

Pivots and coefficients of a direction: the column is sum(c * P_i).

Parameters:

feature (int) – Column of the feature matrix.

Returns:

  • pivots (list of int) – Indices in the training set of the pivots with a non-zero coefficient.

  • coefficients (list of float) – Their coefficients.

feature_id(feature)[source]#

Identifier of a direction: the indices of the pivots it combines.

Parameters:

feature (int) – Column of the feature matrix.

Returns:

feature_id

Return type:

tuple of int

get_state()[source]#

Fitted state: the candidate pivots and their indices, the features, the categories, the scaler and the state of the oblique transformer.

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:

ObliquePivotTransformer