sklearn.base
.TransformerMixin¶
- class sklearn.base.TransformerMixin[source]¶
Mixin class for all transformers in scikit-learn.
This mixin defines the following functionality:
a
fit_transform
method that delegates tofit
andtransform
;a
set_output
method to outputX
as a specific container type.
If get_feature_names_out is defined, then
BaseEstimator
will automatically wraptransform
andfit_transform
to follow theset_output
API. See the Developer API for set_output for details.OneToOneFeatureMixin
andClassNamePrefixFeaturesOutMixin
are helpful mixins for defining get_feature_names_out.Examples
>>> import numpy as np >>> from sklearn.base import BaseEstimator, TransformerMixin >>> class MyTransformer(TransformerMixin, BaseEstimator): ... def __init__(self, *, param=1): ... self.param = param ... def fit(self, X, y=None): ... return self ... def transform(self, X): ... return np.full(shape=len(X), fill_value=self.param) >>> transformer = MyTransformer() >>> X = [[1, 2], [2, 3], [3, 4]] >>> transformer.fit_transform(X) array([1, 1, 1])
Methods
fit_transform
(X[, y])Fit to data, then transform it.
set_output
(*[, transform])Set output container.
- fit_transform(X, y=None, **fit_params)[source]¶
Fit to data, then transform it.
Fits transformer to
X
andy
with optional parametersfit_params
and returns a transformed version ofX
.- Parameters:
- Xarray-like of shape (n_samples, n_features)
Input samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None
Target values (None for unsupervised transformations).
- **fit_paramsdict
Additional fit parameters.
- Returns:
- X_newndarray array of shape (n_samples, n_features_new)
Transformed array.
- set_output(*, transform=None)[source]¶
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”}, default=None
Configure output of
transform
andfit_transform
."default"
: Default output format of a transformer"pandas"
: DataFrame output"polars"
: Polars outputNone
: Transform configuration is unchanged
New in version 1.4:
"polars"
option was added.
- Returns:
- selfestimator instance
Estimator instance.
Examples using sklearn.base.TransformerMixin
¶
Approximate nearest neighbors in TSNE