To use tsmoothie.smoother.LowessSmoother within a scikit-learn Pipeline, you must create a wrapper class that inherits from sklearn.base.BaseEstimator, sklearn.base.TransformerMixin, and LowessSmoother.
Because scikit-learn expects input data in the shape (n_samples, n_features) (where samples are timesteps and features are series), and tsmoothie typically operates on (n_series, timesteps), you must handle transposition within the transform method of your wrapper.
When using the wrapper in a pipeline, pass the data transposed (data.T) to fit_transform to ensure compatibility with standard scikit-learn transformers like StandardScaler.
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.base import TransformerMixin, BaseEstimator
from tsmoothie.smoother import LowessSmoother
class LowessSmootherWrap(TransformerMixin, BaseEstimator, LowessSmoother):
def fit(self, X, y=None):
self._is_fitted = True
return self
def transform(self, X, y=None):
# Transpose X to match tsmoothie expectations (n_series, timesteps)
self.smooth(X.T)
# Transpose result back to (timesteps, n_series)
return self.smooth_data.T
def fit_transform(self, X, y=None):
return self.fit(X).transform(X)
# Usage in a pipeline
smoother = LowessSmootherWrap(smooth_fraction=0.1, iterations=1)
pipe = make_pipeline(StandardScaler(), smoother)
# data.T shape: (timesteps, n_series)
smoothdata = pipe.fit_transform(data.T)