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¶
Constructs a transformer from an arbitrary callable, applying the transform independently to each sequence. |
Methods¶
|
|
|
Fits the transformer to |
|
Fits the transformer to the sequence(s) in |
|
Applies the inverse transformation to |
|
Applies the transformation to |
Definitions¶
- class sequentia.preprocessing.transforms.IndependentFunctionTransformer¶
Constructs a transformer from an arbitrary callable, applying the transform independently to each sequence.
This transform forwards its
X
andlengths
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 asscale()
ornormalize()
.- Note:
This is a stateless transform, meaning
fit()
andfit_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 arrayX
as if it were a single sequence. Read more in the User Guide.
Examples
Using an
IndependentFunctionTransformer
withsklearn.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)¶
- fit(X, y=None, *, lengths=None)¶
Fits the transformer to
X
.- Parameters:
X (ndarray[Any, dtype[float64]] | ndarray[Any, dtype[int64]]) – Sequence(s).
y (ndarray[Any, dtype[float64]] | ndarray[Any, dtype[int64]] | None) – Outputs corresponding to sequence(s) in
X
.lengths (ndarray[Any, dtype[int64]] | None) –
Lengths of the sequence(s) provided in
X
.If
None
, thenX
is assumed to be a single sequence.len(X)
should be equal tosum(lengths)
.
- Returns:
The fitted transformer.
- Return type:
- fit_transform(X, y=None, *, lengths=None)¶
Fits the transformer to the sequence(s) in
X
and returns a transformed version ofX
.- Parameters:
X (ndarray[Any, dtype[float64]] | ndarray[Any, dtype[int64]]) – Sequence(s).
y (ndarray[Any, dtype[float64]] | ndarray[Any, dtype[int64]] | None) – Outputs corresponding to sequence(s) in
X
.lengths (ndarray[Any, dtype[int64]] | None) –
Lengths of the sequence(s) provided in
X
.If
None
, thenX
is assumed to be a single sequence.len(X)
should be equal tosum(lengths)
.
- Returns:
The transformed data.
- Return type:
- inverse_transform(X, *, lengths=None)¶
Applies the inverse transformation to
X
.- Parameters:
- Returns:
The inverse transformed array.
- Return type:
- transform(X, *, lengths=None)¶
Applies the transformation to
X
, producing a transformed version ofX
.- Parameters:
- Returns:
The transformed array.
- Return type: