RuleTree.base.RuleTreeBaseTransformer.RuleTreeBaseTransformer#
- class RuleTreeBaseTransformer[source]#
Bases:
BaseEstimator,ABCAbstract base class of the transformers, the component of a stump that turns the data of one type into a feature matrix.
A new transformer implements
transform()and, if it learns something from the data,fit()(andget_state()/set_state()to be saved with the tree).A transformer is fitted on the whole data set of its type (
X, all the instances) and onidx, the instances reaching the node, so that values computed on the whole data set can be shared among the splits throughcache(a dictionary that lives as long as the tree’s cache).After the splitter has chosen a feature,
select()lets the transformer keep only what is needed to compute that feature (e.g. a single pivot); if the feature matrix changes,refit_after_selectis True and the stump fits the splitter again on the new matrix.Transformers are scikit-learn estimators: their parameters are the arguments of
__init__.- Variables:
supported_data_types (tuple of str) – Data types the transformer can be fitted on. Class attribute.
refit_after_select (bool) – Whether
select()changes the feature matrix. Class attribute.numerical (list of int) – Indices of the numerical columns of the feature matrix. Set by
fit().categorical (list of int) – Indices of the categorical columns of the feature matrix. Set by
fit().
See also
RuleTree.stumps.Stump.Stumpthe stump that combines a transformer and a splitter.
RuleTreeBaseSplitterthe other component of a stump.
Examples
A transformer without fitted state defines only
transform(see Extending the library):>>> import numpy as np >>> from RuleTree import RuleTreeClassifier >>> from RuleTree.base.RuleTreeBaseTransformer import RuleTreeBaseTransformer >>> from RuleTree.stumps.Stump import Stump >>> from RuleTree.stumps.splitters import ClassificationSplitter >>> from RuleTree.utils.define import DATA_TYPE_TS >>> class MeanTransformer(RuleTreeBaseTransformer): ... supported_data_types = (DATA_TYPE_TS,) ... ... def transform(self, X): ... return X.mean(axis=2) # mean of each signal ... ... def feature_id(self, feature): ... return f"mean_{feature}" >>> rng = np.random.default_rng(0) >>> y = rng.integers(0, 2, size=50) >>> X_ts = rng.normal(size=(50, 1, 20)) + 2 * y[:, None, None] >>> stump = Stump(MeanTransformer(), ClassificationSplitter()) >>> model = RuleTreeClassifier(max_depth=1, base_stumps=stump).fit(X_ts=X_ts, y=y) >>> model.get_rules()["textual_rule"].split()[0] 'mean_0'
- __init__()#
Methods
__init__()feature_id(feature)Identifier of a feature of the matrix in the rules, e.g. a column of the data or a pivot.
fit(X, y[, idx, data_type, cache, sample_weight])Fit the transformer and set
numerical_andcategorical_.get_metadata_routing()Get metadata routing of this object.
get_params([deep])Get parameters for this estimator.
Fitted state needed by
transform().select(feature)Keep only what is needed to compute
feature, the column chosen by the splitter.set_fit_request(*[, cache, data_type, idx, ...])Configure whether metadata should be requested to be passed to the
fitmethod.set_params(**params)Set the parameters of this estimator.
set_state(state)Restore the fitted state saved by
get_state().transform(X)Feature matrix of the instances in X.
Attributes
- supported_data_types = ('tabular',)#
- refit_after_select = False#
- fit(X: ndarray, y: ndarray, idx: ndarray = None, data_type: str = None, cache: dict = None, sample_weight=None)[source]#
Fit the transformer and set
numerical_andcategorical_.The default implementation, for transformers without fitted state, only sets the types of the columns of
transform(X): such a transformer implements justtransform().- 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.
cache (dict, default=None) – Values shared among the splits of the tree.
sample_weight (numpy.ndarray, default=None) – Sample weights, all the instances.
- Returns:
self – The fitted transformer.
- Return type:
- Raises:
NoSplitFoundWarning – If the data of the node cannot be transformed into features (e.g. no numerical feature).
- abstractmethod transform(X: ndarray) ndarray[source]#
Feature matrix of the instances in X.
- Parameters:
X (numpy.ndarray) – Data of the transformer’s type.
- Returns:
X_transformed – Feature matrix.
- Return type:
numpy.ndarray of shape (n_samples, n_features)
- select(feature: int) int[source]#
Keep only what is needed to compute
feature, the column chosen by the splitter. The default implementation keeps everything.- Parameters:
feature (int) – Column of the feature matrix chosen by the splitter.
- Returns:
feature – Index of the feature in the matrix returned by
transform()from now on.- Return type:
- feature_id(feature: int)[source]#
Identifier of a feature of the matrix in the rules, e.g. a column of the data or a pivot. The default implementation is the index of the column.
- get_state() dict[source]#
Fitted state needed by
transform(). The default implementation is an empty state.- Returns:
state – JSON-friendly dictionary, restored by
set_state().- Return type:
- set_state(state: dict)[source]#
Restore the fitted state saved by
get_state().- Parameters:
state (dict) – Dictionary created by
get_state().- Returns:
self
- Return type:
- set_fit_request(*, cache: bool | None | str = '$UNCHANGED$', data_type: bool | None | str = '$UNCHANGED$', idx: bool | None | str = '$UNCHANGED$', sample_weight: bool | None | str = '$UNCHANGED$') RuleTreeBaseTransformer#
Configure whether metadata should be requested to be passed to the
fitmethod.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(seesklearn.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 tofitif provided. The request is ignored if metadata is not provided.False: metadata is not requested and the meta-estimator will not pass it tofit.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:
cache (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
cacheparameter infit.data_type (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
data_typeparameter infit.idx (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
idxparameter infit.sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for
sample_weightparameter infit.
- Returns:
self – The updated object.
- Return type: