我正在尝试为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
Pipeline
构造函数需要一个参数steps
,它是一个元组列表。
更正版本:
pipeline = Pipeline([('text_length', AverageWordLengthExtractor()),
('scale', StandardScaler())])
更多信息在官方docs。