Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scikit-learn pipeline TypeError: zip argument #2 must support iteration

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
like image 768
anidev711 Avatar asked Apr 22 '19 11:04

anidev711


1 Answers

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.

like image 200
Jan K Avatar answered Nov 11 '22 05:11

Jan K