我正在尝试为 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 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
构造Pipeline
函数需要一个参数steps
,它是一个元组列表。
修正版:
pipeline = Pipeline([('text_length', AverageWordLengthExtractor()), ('scale', StandardScaler())])
官方文档中的更多信息。
添加回答
举报
0/150
提交
取消