我正在尝试为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
Series
的结构和执行流程。 - sentenceFile "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) ValueError: too many values to unpack (expected 2)
- anidev711