I am trying to create a custom transformer for sklearn pipeline which will extract the average word length of a particular text and then apply standard scaler on it to standardize the dataset. I am passing a Series of texts to the pipeline.
class AverageWordLengthExtractor(BaseEstimator, TransformerMixin):
def __init__(self):
pass
def average_word_length(self, text):
return np.mean([len(word) for word in text.split( )])
def fit(self, x, y=None):
return self
def transform(self, x , y=None):
return pd.DataFrame(pd.Series(x).apply(self.average_word_length))
then I created a pipeline like this.
pipeline = Pipeline(['text_length', AverageWordLengthExtractor(),
'scale', StandardScaler()])
When I execute the fit_transform on this pipeline I am getting the error,
File "custom_transformer.py", line 48, in <module>
main()
File "custom_transformer.py", line 43, in main
'scale', StandardScaler()])
File "/opt/conda/lib/python3.6/site-packages/sklearn/pipeline.py", line 114, in __init__
self._validate_steps()
File "/opt/conda/lib/python3.6/site-packages/sklearn/pipeline.py", line 146, in _validate_steps
names, estimators = zip(*self.steps)
TypeError: zip argument #2 must support iteration
The Pipeline
constructor expects an argument steps
which is a list of tuples.
Corrected version:
pipeline = Pipeline([('text_length', AverageWordLengthExtractor()),
('scale', StandardScaler())])
More info in the official docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With