亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Scikit-learn 管道類型錯誤:zip 參數 #2 必須支持迭代

Scikit-learn 管道類型錯誤:zip 參數 #2 必須支持迭代

繁星coding 2022-01-18 16:22:39
我正在嘗試為 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())])

官方文檔中的更多信息。


查看完整回答
反對 回復 2022-01-18
  • 1 回答
  • 0 關注
  • 344 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號