Function Transformer

When preprocessing sequential data, it is often preferable to apply certain transformations on each sequence independently rather than applying a single transformation to all of the data collectively.

For example, we might want to apply signal filters to each sequence independently.

IndependentFunctionTransformer allows for such transformations to be defined for arbitrary functions.

API reference

Class

IndependentFunctionTransformer

Constructs a transformer from an arbitrary callable, applying the transform independently to each sequence.

Methods

__init__([func, inverse_func, validate, ...])

See sklearn.preprocessing.FunctionTransformer.

fit(X[, y, lengths])

Fits the transformer to X.

fit_transform(X[, y, lengths])

Fits the transformer to the sequence(s) in X and returns a transformed version of X.

inverse_transform(X, *[, lengths])

Applies the inverse transformation to X.

transform(X, *[, lengths])

Applies the transformation to X, producing a transformed version of X.


class sequentia.preprocessing.transforms.IndependentFunctionTransformer

Constructs a transformer from an arbitrary callable, applying the transform independently to each sequence.

This transform forwards its X and lengths arguments to a user-defined function or function object and returns the result of this function. This is useful for stateless transformations such as taking the log of frequencies, doing custom scaling, etc. Note: If a lambda is used as the function, then the resulting transformer will not be pickleable.

This works conveniently with functions in sklearn.preprocessing such as scale() or normalize().

Note:

This is a stateless transform, meaning fit() and fit_transform() will not fit on any data.

See also

sklearn.preprocessing.FunctionTransformer

IndependentFunctionTransformer is based on this class, which applies the callable to the entire input array X as if it were a single sequence. Read more in the User Guide.

Examples

Using an IndependentFunctionTransformer with sklearn.preprocessing.minmax_scale() to scale features to the range [0, 1] independently for each sequence in the spoken digits dataset.

from sklearn.preprocessing import minmax_scale
from sequentia.preprocessing import IndependentFunctionTransformer
from sequentia.datasets import load_digits

# Fetch MFCCs of spoken digits
data = load_digits()

# Create an independent min-max transform
transform = IndependentFunctionTransformer(minmax_scale)

# Apply the transform to the data
Xt = transform.transform(data.X, lengths=data.lengths)
__init__(func=None, inverse_func=None, *, validate=False, accept_sparse=False, check_inverse=True, feature_names_out=None, kw_args=None, inv_kw_args=None)

See sklearn.preprocessing.FunctionTransformer.

fit(X, y=None, *, lengths=None)

Fits the transformer to X.

Parameters:
Returns:

The fitted transformer.

Return type:

IndependentFunctionTransformer

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

Fits the transformer to the sequence(s) in X and returns a transformed version of X.

Parameters:
Returns:

The transformed data.

Return type:

numpy.ndarray

inverse_transform(X, *, lengths=None)

Applies the inverse transformation to X.

Parameters:
Returns:

The inverse transformed array.

Return type:

numpy.ndarray

transform(X, *, lengths=None)

Applies the transformation to X, producing a transformed version of X.

Parameters:
Returns:

The transformed array.

Return type:

numpy.ndarray