Scikit学习管道TypeError:zip参数2必须支持迭代

时间:2019-04-22 11:06:19

标签: python python-3.x scikit-learn pipeline

我正在尝试为sklearn管道创建一个自定义转换器,该转换器将提取特定文本的平均单词长度,然后对其应用标准缩放器以标准化数据集。我正在向管道传递一系列文本。

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))

然后我创建了这样的管道。

pipeline = Pipeline(['text_length', AverageWordLengthExtractor(), 
                         'scale', StandardScaler()])

在此管道上执行fit_transform时,我得到了错误,

 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

1 个答案:

答案 0 :(得分:1)

Pipeline构造函数需要一个参数steps,该参数是元组的列表。

更正的版本:

pipeline = Pipeline([('text_length', AverageWordLengthExtractor()), 
                     ('scale', StandardScaler())])

docs中的更多信息。