我正在嘗試為 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
提交
取消