mlresearch.preprocessing.PipelineEncoder

class mlresearch.preprocessing.PipelineEncoder(features=None, encoder=None, **kwargs)[source]

Pipeline-compatible wrapper of Scikit-learn’s Transformer objects. Used to pass encoding of non-metric features and scalers (when there are categorical features) within a pipeline.

When encoder is None, sklearn.preprocessing.OneHotEncoder will be used. In that case, kwargs can be passed to define its parameters. Otherwise, it is ignored.

The fitted encoder object from Scikit-learn is stored in self.encoder_.

Parameters:
featuresndarray of shape (n_cat_features,) or (n_features,)

Specifies which features to transform. Can either be:

  • array of indices specifying the features to transform.

  • mask array of shape (n_features, ) and bool dtype for which True indicates the features to transform.

  • array of shape (n_transf_features,) and str dtype with the names of the features to transform. In this case, X must be a dataframe. Raises an error otherwise.

encoderencoder object, default=None

Encoder object to be used for transforming the features. If None, defaults to sklearn’s OneHotEncoder with default parameters, which can be modified with keyword arguments.

Warning

The encoder object must be compatible with sklearn’s API.

Notes

In most situations, sklearn.compose.ColumnTransformer can be used as an alternative to PipelineEncoder.

Attributes:
features_ndarray of shape (n_features,)

Mask array of shape (n_features, ) and bool dtype for which True indicates the features to transform.

encoded_features_names_out_ndarray of str objects

Output feature names after transformation.

encoded_features_idx_out_ndarray of int objects

Indices of encoded features after transformation.


fit(X, y=None)[source]

Fit PipelineEncoder to X.

Parameters:
Xarray-like of shape (n_samples, n_features)

The data to determine the categories of each feature.

yNone

Ignored. This parameter exists only for compatibility with Pipeline.

Returns:
self

Fitted encoder.

fit_transform(X, y=None, **fit_params)

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.

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.

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_output(*, transform=None)

Set output container.

See sphx_glr_auto_examples_miscellaneous_plot_set_output.py for an example on how to use the API.

Parameters:
transform{“default”, “pandas”}, default=None

Configure output of transform and fit_transform.

  • “default”: Default output format of a transformer

  • “pandas”: DataFrame output

  • “polars”: Polars output

  • None: Transform configuration is unchanged

New in version 1.4: “polars” option was added.

Returns:
selfestimator instance

Estimator instance.

set_params(**params)

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.

steps: List[Any]
transform(X)[source]

Transform X using the encoder object.

Parameters:
Xarray-like of shape (n_samples, n_features_to_encode + n_remaining)

Data containing the features to encode.

Returns:
X_out{ndarray, sparse matrix} of shape (n_samples, n_encoded_features + n_remaining)

Transformed input. Regardless of sparse_output, a dense matrix will be returned.