mlnext.pipeline.LengthTransformer

class mlnext.pipeline.LengthTransformer(pad_length: int | None = None, fill_value: int = 0, truncate: bool = False)[source]

Bases: BaseEstimator, TransformerMixin

Pad or truncates the input to an fixed length by either a set length or a fitted length.

Parameters:
  • pad_length (int | None) – Length to pad the data to. Default: None.

  • fill_value (int) – Value to pad data with. Default: 0.

  • truncate (bool) – Whether to truncate if the length exceeds pad_length. If False, an error is raised for an input longer than pad_length.

Added in version 0.6.0.

Example

>>> import pandas as pd
>>> from mlnext import LengthTransformer
>>> df = pd.DataFrame({'a': [0, 1, 2], 'b': [1, 2, 3]})
>>> t = LengthTransformer(pad_length=5, fill_value=-1)
>>> t.fit_transform(df)
    a   b
0       0       1
1       1       2
2       2       3
3  -1  -1
4  -1  -1

Methods

fit

Sets the pad_length to the length of the fitted dataframe (if pad_length is not defined).

fit_transform

Fit to data, then transform it.

get_metadata_routing

Get metadata routing of this object.

get_params

Get parameters for this estimator.

set_output

Set output container.

set_params

Set the parameters of this estimator.

transform

Pads or truncates X to pad_length_.

fit(X: DataFrame, y=None)[source]

Sets the pad_length to the length of the fitted dataframe (if pad_length is not defined).

Parameters:
  • X (pd.DataFrame) – Data.

  • y (_type_, optional) – Labels (ignored). Defaults to None.

Returns:

Returns self.

Return type:

LengthTransformer

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:
  • X (array-like of shape (n_samples, n_features)) – Input samples.

  • y (array-like of shape (n_samples,) or (n_samples, n_outputs), default=None) – Target values (None for unsupervised transformations).

  • **fit_params (dict) – Additional fit parameters.

Returns:

X_new – Transformed array.

Return type:

ndarray array of shape (n_samples, n_features_new)

get_metadata_routing()

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

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", "polars"}, 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

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

Returns:

self – Estimator instance.

Return type:

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:

**params (dict) – Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance

transform(X: DataFrame) DataFrame[source]

Pads or truncates X to pad_length_.

Parameters:

X (pd.DataFrame) – Data.

Raises:

ValueError – Raised if X is longer than pad_length and truncate is False.

Returns:

Returns the new dataframe of length pad_length.

Return type:

pd.DataFrame