我正在尝试为 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
12345678_0001
相关分类