I’m attempting to put together a Custom Transformer for sklearn which returns either a dataframe or array on my X data. It inherits from sklearn and a library called tsmoothie. However, I’m not quite sure about the use of super() and inheritance. I’m getting this error:
JavaScript
x
2
1
TypeError: __init__() got an unexpected keyword argument 'smooth_fraction'
2
My code:
JavaScript
1
28
28
1
import pandas as pd
2
from tsmoothie.smoother import LowessSmoother
3
from sklearn.base import BaseEstimator, TransformerMixin
4
5
class LowessSmootherWrap(TransformerMixin, BaseEstimator, LowessSmoother):
6
def __init__(self, df=True):
7
super().__init__(smooth_fraction=0.01, iterations=2)
8
self.df = df
9
10
def fit(self, X, y=None):
11
self._is_fitted = True
12
if self.df == True:
13
self.feature_names_ = X.columns
14
self.index_ = X.index
15
return self
16
17
def transform(self, X, y=None, **kwargs):
18
self.smooth(X.T)
19
return pd.DataFrame(self.smooth_data.T,
20
index=self.index_,
21
columns=self.feature_names_)
22
if self.df == True else self.smooth_data.T
23
24
def fit_transform(self, X, y=None, **kwargs):
25
return self.fit(X).transform(X)
26
27
smoother = LowessSmootherWrap(smooth_fraction=0.01, iterations=2, df=False)
28
Advertisement
Answer
JavaScript
1
26
26
1
class LowessSmootherWrap(TransformerMixin, BaseEstimator):
2
def __init__(self, smoothing_fraction, n_iterations, df=True, **kwargs):
3
super().__init__(**kwargs)
4
self.smoothing_fraction = smoothing_fraction
5
self.n_iterations = n_iterations
6
self.df = df
7
8
def fit(self, X, y=None):
9
self._is_fitted = True
10
if self.df == True:
11
self.feature_names_ = X.columns
12
self.index_ = X.index
13
return self
14
15
def transform(self, X, y=None, **kwargs):
16
self.smoother = LowessSmoother(smooth_fraction=self.smoothing_fraction,
17
iterations=self.n_iterations)
18
self.smoother.smooth(X.copy().T)
19
return pd.DataFrame(self.smoother.smooth_data.T,
20
index=self.index_,
21
columns=self.feature_names_)
22
if self.df == True else self.smoother.smooth_data.T
23
24
def fit_transform(self, X, y=None, **kwargs):
25
return super().fit_transform(X)
26