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:
PivotTransformerTransformer 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_57whereP_iis the distance from the training instancei.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 (HouseholderTransformerorBivariateTransformer), 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,discriminativesandcandidatespivots of the node.scaler (StandardScaler) – Scaler of the encoded features, fitted on the whole data set.
See also
ObliquePivotTreeStumpClassifierthe 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:
- 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)
- 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.
- 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: